blob: 5127c28c112ea87f98e3cc423a58ab6c6e99cd06 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
// SPDX-License-Identifier: GPL-3.0-or-later
package scrape
import "sync"
type throttledCaller struct {
limit chan struct{}
wg sync.WaitGroup
}
func newThrottledCaller(limit int) *throttledCaller {
if limit <= 0 {
panic("limit must be > 0")
}
return &throttledCaller{limit: make(chan struct{}, limit)}
}
func (t *throttledCaller) call(job func()) {
t.wg.Add(1)
go func() {
defer t.wg.Done()
t.limit <- struct{}{}
defer func() {
<-t.limit
}()
job()
}()
}
func (t *throttledCaller) wait() {
t.wg.Wait()
}
|