Improve scalability of vsphere input (#5113)
This commit is contained in:
committed by
Daniel Nelson
parent
1d6ff4fe4c
commit
78c1ffbf27
45
plugins/inputs/vsphere/throttled_exec.go
Normal file
45
plugins/inputs/vsphere/throttled_exec.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user