workers

package module
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2023 License: Apache-2.0 Imports: 6 Imported by: 7

README

WORKERS

workers pool for Golang

Install

go get -u github.com/aacfactory/workers

Usage

Create worker task

type Task struct {}

func (task *Task) Execute(ctx context.Context) {
    // todo: handle task
}

New workers and execute command

ws := workers.New()

// dispatch task to worker
ctx := context.TODO()
if ok := ws.Dispatch(ctx, &Task{}); !ok {
    // todo: handle 'no workers remain' or 'closed' or 'context timeout'
}

// must dispatch task to worker 
ws.MustDispatch(ctx, &Task{})

ws.Close()

Benchmark

Handler used in benchmark is sleeping 50 millisecond as handling command.

goos: windows
goarch: amd64
pkg: github.com/aacfactory/workers
cpu: AMD Ryzen 9 3950X 16-Core Processor
BenchmarkNewWorkers
    worker_benchmark_test.go:37: total 1 accepted 1
    worker_benchmark_test.go:37: total 100 accepted 100
    worker_benchmark_test.go:37: total 10000 accepted 10000
    worker_benchmark_test.go:37: total 266420 accepted 266420
    worker_benchmark_test.go:37: total 834958 accepted 834958
    worker_benchmark_test.go:37: total 1104766 accepted 1104766
BenchmarkNewWorkers-32           1104766               953.7 ns/op            25
 B/op          1 allocs/op
PASS

Thanks

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Options) error

func MaxIdleWorkerDuration added in v1.4.0

func MaxIdleWorkerDuration(d time.Duration) Option

func MaxWorkers added in v1.4.0

func MaxWorkers(max int) Option

type Options added in v1.1.0

type Options struct {
	MaxWorkers            int
	MaxIdleWorkerDuration time.Duration
}

type Task added in v1.4.0

type Task interface {
	Execute(ctx context.Context)
}

type Workers

type Workers interface {
	Dispatch(ctx context.Context, task Task) (ok bool)
	MustDispatch(ctx context.Context, task Task)
	Close()
}

func New added in v1.2.0

func New(options ...Option) (w Workers)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL