diff options
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/vsphere/scrape/throttled_caller.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/vsphere/scrape/throttled_caller.go b/src/go/collectors/go.d.plugin/modules/vsphere/scrape/throttled_caller.go new file mode 100644 index 000000000..5127c28c1 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/vsphere/scrape/throttled_caller.go @@ -0,0 +1,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() +} |