mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 04:04:00 -06:00
d567f199dd
* Move the grabpl build-backend command and clean it up a bit
44 lines
785 B
Go
44 lines
785 B
Go
package syncutil
|
|
|
|
import (
|
|
"log"
|
|
"runtime"
|
|
)
|
|
|
|
func worker(jobs chan func()) {
|
|
for j := range jobs {
|
|
j()
|
|
}
|
|
}
|
|
|
|
// WorkerPool represents a concurrent worker pool.
|
|
type WorkerPool struct {
|
|
NumWorkers int
|
|
jobs chan func()
|
|
}
|
|
|
|
// NewWorkerPool constructs a new WorkerPool.
|
|
func NewWorkerPool(numWorkers int) WorkerPool {
|
|
if numWorkers <= 0 {
|
|
numWorkers = runtime.NumCPU()
|
|
}
|
|
log.Printf("Creating worker pool with %d workers", numWorkers)
|
|
jobs := make(chan func(), 100)
|
|
for i := 0; i < numWorkers; i++ {
|
|
go worker(jobs)
|
|
}
|
|
return WorkerPool{
|
|
NumWorkers: numWorkers,
|
|
jobs: jobs,
|
|
}
|
|
}
|
|
|
|
// Schedule schedules a job to be executed by a worker in the pool.
|
|
func (p WorkerPool) Schedule(job func()) {
|
|
p.jobs <- job
|
|
}
|
|
|
|
func (p WorkerPool) Close() {
|
|
close(p.jobs)
|
|
}
|