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