worker
packageAPI reference for the worker
package.
Imports
(4)Task
Task is a work function executed by the pool.
type Task func(context.Context) error
Future
Future holds the result of an asynchronous task.
type Future struct
Methods
Fields
| Name | Type | Description |
|---|---|---|
| ch | chan Result |
Result
Result holds the outcome of a completed Task.
type Result struct
Fields
| Name | Type | Description |
|---|---|---|
| Err | error |
Pool
Pool is a fixed-size worker pool for concurrent task execution.
type Pool struct
Methods
worker pulls tasks from the tasks channel until done is closed.
func (*Pool) worker()
{
defer p.wg.Done()
for {
select {
case item := <-p.tasks:
if p.closed.Load() {
item.accepted <- false
continue
}
item.accepted <- true
_ = runTask(p.ctx, item.task)
case <-p.done:
return
}
}
}
Submit enqueues a task for execution. Returns false if the pool is shut down or shutting down; the task is not executed in that case.
Parameters
Returns
func (*Pool) Submit(task Task) bool
{
if task == nil || p.closed.Load() {
return false
}
item := work{
task: task,
accepted: make(chan bool, 1),
}
select {
case p.tasks <- item:
return <-item.accepted
case <-p.done:
return false
}
}
Shutdown stops accepting new tasks and waits for in-progress work to finish. The task channel is unbuffered, so no tasks can be pending pickup when done is closed; any Submit in progress will observe done and return false.
func (*Pool) Shutdown()
{
p.once.Do(func() {
p.closed.Store(true)
p.cancel()
close(p.done)
p.wg.Wait()
})
}
Fields
| Name | Type | Description |
|---|---|---|
| wg | sync.WaitGroup | |
| tasks | chan work | |
| done | chan struct{} | |
| cancel | context.CancelFunc | |
| ctx | context.Context | |
| once | sync.Once | |
| closed | atomic.Bool |
work
type work struct
Fields
| Name | Type | Description |
|---|---|---|
| task | Task | |
| accepted | chan bool |
Uses
NewPool
NewPool creates a fixed-size worker pool of n goroutines.
Parameters
Returns
func NewPool(n int) *Pool
{
if n <= 0 {
n = 1
}
ctx, cancel := context.WithCancel(context.Background())
p := &Pool{
tasks: make(chan work),
done: make(chan struct{}),
cancel: cancel,
ctx: ctx,
}
for i := 0; i < n; i++ {
p.wg.Add(1)
go p.worker()
}
return p
}
Example
pool := worker.NewPool(4)
defer pool.Shutdown()
pool.Submit(func(ctx context.Context) error {
return doWork(ctx)
})
runTask
Parameters
Returns
func runTask(ctx context.Context, task Task) (err error)
{
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("worker: task panic: %v", recovered)
}
}()
return task(ctx)
}