https://github.com/dc0d/spool
simple worker pool
https://github.com/dc0d/spool
go golang pool worker-pool workerpool workers
Last synced: 18 days ago
JSON representation
simple worker pool
- Host: GitHub
- URL: https://github.com/dc0d/spool
- Owner: dc0d
- License: mit
- Created: 2021-05-06T22:00:09.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-11-01T22:00:29.000Z (over 3 years ago)
- Last Synced: 2025-01-20T14:57:25.228Z (over 1 year ago)
- Topics: go, golang, pool, worker-pool, workerpool, workers
- Language: Go
- Homepage:
- Size: 29.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# spool
[](http://opensource.org/licenses/MIT)
[](https://pkg.go.dev/github.com/dc0d/spool)
[](https://goreportcard.com/report/github.com/dc0d/spool)
**spool** is a simple worker-pool for Go. Each job is a `func()` which is sent to the worker-pool. A worker will pick up the job from the mailbox and execute it.
First, initialize the worker-poll with a mailbox of a certain size:
```go
pool := New(n)
defer pool.Stop()
```
The mailbox is just a `chan func()`. In fact the worker-pool itself is defined as:
```go
type WorkerPool chan func()
```
Jobs can be sent to the worker-pool in two different manners, blocking and nonblocking. To send a job to the worker-pool and block until it's completed:
```go
pool.Blocking(ctx, func() {
// ...
})
```
And to send a job to the worker-pool and then move on:
```go
pool.SemiBlocking(ctx, func() {
// ...
})
```
As long as there is an empty space in the mailbox, `SemiBlocking` will just queue the job, and moves on. When there are no more empty spaces in the mailbox, `SemiBlocking` becomes blocking.
A worker-pool by default has no workers and they should be added explicitly. To add workers to the worker-pool:
```go
pool.Grow(ctx, 10)
```
Now, the worker-pool has `10` workers.
It's possible to add temporary workers to the worker-pool:
```go
pool.Grow(ctx, 9, WithAbsoluteTimeout(time.Minute * 5))
```
Also, instead of using and absolute timeout, an idle timeout can be used. In this case, added workers will stop, if they are idle for a certain duration:
```go
pool.Grow(ctx, 9, WithIdleTimeout(time.Minute * 5))
```
The `Blocking` and `SemiBlocking` methods will panic if the worker-pool is stopped - to enforce visibility on job execution.
## spool serializes the jobs in single worker mode
- `TODO`