Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
44
plugins/inputs/vsphere/throttled_exec.go
Normal file
44
plugins/inputs/vsphere/throttled_exec.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package vsphere
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// throttledExecutor provides a simple mechanism for running jobs in separate
|
||||
// goroutines while limit the number of concurrent jobs running at any given time.
|
||||
type throttledExecutor struct {
|
||||
limiter chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// newThrottledExecutor creates a new ThrottlesExecutor with a specified maximum
|
||||
// number of concurrent jobs
|
||||
func newThrottledExecutor(limit int) *throttledExecutor {
|
||||
if limit == 0 {
|
||||
panic("Limit must be > 0")
|
||||
}
|
||||
return &throttledExecutor{limiter: make(chan struct{}, limit)}
|
||||
}
|
||||
|
||||
// run schedules a job for execution as soon as possible while respecting the maximum concurrency limit.
|
||||
func (t *throttledExecutor) run(ctx context.Context, job func()) {
|
||||
t.wg.Add(1)
|
||||
go func() {
|
||||
defer t.wg.Done()
|
||||
select {
|
||||
case t.limiter <- struct{}{}:
|
||||
defer func() {
|
||||
<-t.limiter
|
||||
}()
|
||||
job()
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// wait blocks until all scheduled jobs have finished
|
||||
func (t *throttledExecutor) wait() {
|
||||
t.wg.Wait()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue