Files
libredesk/internal/workerpool/workerpool.go
T
Abhinav Raut caf8e7d34d WIP: MVP with shadcn sidebar
- csat
- SLA
- email notification templates
2025-01-06 02:39:44 +05:30

47 lines
780 B
Go

// package workerpool contains a single goroutine worker pool that executes arbitrary
// encapsulated functions.
package workerpool
import (
"sync"
)
// Pool is a single goroutine worker pool.
type Pool struct {
num int
q chan func()
wg sync.WaitGroup
}
// New returns a new goroutine workerpool.
func New(num, queueSize int) *Pool {
return &Pool{
num: num,
q: make(chan func(), queueSize),
wg: sync.WaitGroup{},
}
}
// Run initializes the goroutine worker pool.
func (w *Pool) Run() {
for i := 0; i < w.num; i++ {
w.wg.Add(1)
go func() {
for f := range w.q {
f()
}
w.wg.Done()
}()
}
}
// Push pushes a job to the worker queue to execute.
func (w *Pool) Push(f func()) {
w.q <- f
}
func (w *Pool) Close() {
close(w.q)
w.wg.Wait()
}