Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "github.com/tniswong/workerpool/v2" "time" ) func NewCounterTask(name string, limit int) *CounterTask { return &CounterTask{name: name, limit: limit} } type CounterTask struct { name string count int limit int } func (c *CounterTask) Invoke(ctx context.Context) error { loop: for { select { case <-ctx.Done(): return ctx.Err() case <-time.Tick(1 * time.Second / 2): c.count++ fmt.Printf("name: %v, count:%v\n", c.name, c.count) if c.count >= c.limit { break loop } } } return nil } func main() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() pool := workerpool.New(2) done := pool.Run(ctx) // runs until context is cancelled pool.Push(NewCounterTask("task 1", 2)) pool.Push(NewCounterTask("task 2", 3)) <-done }
Output: name: task 1, count:1 name: task 2, count:1 name: task 2, count:2 name: task 1, count:2 name: task 2, count:3
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TaskOption ¶
type TaskOption func(j *job)
TaskOption allows for customization of task behavior
func Retry ¶
func Retry(b bool) TaskOption
Retry will cause a Task to be invoked again if an error is returned. By default, a Task will be retried indefinitely until either the Task terminates successfully or the WorkerPool is stopped by context cancellation. To place limits on this behavior, see the RetryMax(n int) TaskOption
func RetryMax ¶
func RetryMax(n int) TaskOption
RetryMax will cause a Task to be invoked again error is returned, with a limit of `n` total retries
type WorkerPool ¶
type WorkerPool struct {
// contains filtered or unexported fields
}
WorkerPool is a concurrent worker pool implementation based on a semaphore
func New ¶
func New(n int64) WorkerPool
New is a constructor function for WorkerPool. `n` specifies the number of parallel workers
func (*WorkerPool) Push ¶
func (p *WorkerPool) Push(t Task, opts ...TaskOption)
Push adds a Task to the queue with the provided options
func (*WorkerPool) Run ¶
func (p *WorkerPool) Run(ctx context.Context) <-chan struct{}
Run the WorkerPool. To stop processing, cancel the context