Add dynamic worker pool with bounded queue and concurrency tests
This commit is contained in:
34
README.md
Normal file
34
README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# worker
|
||||
|
||||
`worker` is a small Go pool for `func()` tasks. Create it with a minimum and
|
||||
maximum worker count, then submit tasks:
|
||||
|
||||
```go
|
||||
pool, err := worker.New(2, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := pool.Submit(func() { doWork() }); err != nil {
|
||||
return err
|
||||
}
|
||||
```
|
||||
|
||||
The pool creates the minimum workers immediately and grows on demand up to the
|
||||
maximum. Its task queue has a fixed capacity of `maxWorkers/2`; once all workers
|
||||
and queue slots are occupied, `Submit` waits for room. `Close` stops new
|
||||
submissions and waits for accepted tasks to finish. A successful `Submit` means
|
||||
the task was accepted; it does not mean the task has started or finished. With
|
||||
`maxWorkers == 1`, the queue has zero capacity and submissions wait for a worker.
|
||||
|
||||
After a task finishes, a worker exits immediately only when `total > min` and
|
||||
the number of *other* idle workers is greater than `min/2`. There is no idle
|
||||
timeout. A task panic is logged and that worker continues with later tasks.
|
||||
|
||||
Tasks should return normally or panic. Do not call `runtime.Goexit` from a task:
|
||||
it terminates the worker and may leave `Close` waiting indefinitely. Call
|
||||
`Close` from outside pool tasks so it does not wait for itself.
|
||||
|
||||
A task may submit another task to the same pool. If all workers block submitting
|
||||
to a full queue, the caller must resolve that deadlock in its task logic.
|
||||
Reference in New Issue
Block a user