Files
go-simple-graphql/utils/concurrency/concurrency.go
T
lukaszraczylo e0913f8824 New minor release after complete rewrite.
Brings neat and testable codebase with minimum allocations in mind.
2023-04-07 15:27:44 +01:00

31 lines
418 B
Go

package concurrency
type Pool struct {
queue chan struct{}
jobs int
}
func NewPool(size int) *Pool {
if size < 0 {
size = 0
}
return &Pool{
jobs: size,
queue: make(chan struct{}, size),
}
}
func (p *Pool) Enqueue(job func(params ...any), params ...any) {
if p.jobs == 0 {
go job(params...)
return
}
p.queue <- struct{}{}
go func() {
defer func() {
<-p.queue
}()
job(params...)
}()
}