Files
warewulf/internal/pkg/batch/batch.go
Jonathon Anderson 88feb1a876 Testing for the batch package
* Remove Min function (unused)

Signed-off-by: Jonathon Anderson <janderson@ciq.co>
2023-02-24 22:47:12 -07:00

42 lines
621 B
Go

package batch
import (
"sync"
)
type BatchPool struct {
active int
jobs []func()
}
func New(active int) *BatchPool {
pool := &BatchPool{
active: active,
}
pool.jobs = []func(){}
return pool
}
func (pool *BatchPool) Submit(f func()) {
pool.jobs = append(pool.jobs, f)
}
func wrapper(wg *sync.WaitGroup, limiter chan struct{}, f func()) {
f()
<-limiter
wg.Done()
}
func (pool *BatchPool) Run() {
var wg sync.WaitGroup
limiter := make(chan struct{}, pool.active)
for i := range pool.jobs {
limiter <- struct{}{}
wg.Add(1)
go wrapper(&wg, limiter, pool.jobs[i])
}
wg.Wait()
close(limiter)
}