worker

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2021 License: MIT Imports: 5 Imported by: 0

README

go-worker

Go Reference

Resizable worker goroutine pool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultIDFunc

func DefaultIDFunc() string

func IDFromContext

func IDFromContext(ctx context.Context) (string, bool)

IDFromContext retrieves worker id from context.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

type IDFunc

type IDFunc func() string

type Option

type Option func(*Config)

func RestartAfter

func RestartAfter(d time.Duration) Option

RestartAfter sets worker restart delay.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets parent context of workers.

func WithIDFunc

func WithIDFunc(f IDFunc) Option

WithIDFunc sets worker id generator function.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(f func(context.Context), options ...Option) *Pool

NewPool creates a new pool of given worker function. Since it starts with zero workers, the pool does not run workers until the first call of Resize. Workers can retrieve their id from the context using IDFromContext.

func (*Pool) Close

func (p *Pool) Close()

Close cancels all running workers' context and wait for all workers to stop.

func (*Pool) KillWorker

func (p *Pool) KillWorker(id string) bool

KillWorker kills the worker with given id, causing the pool to respawn a new worker. KillWorker returns false if there was no such worker.

func (*Pool) Resize

func (p *Pool) Resize(n int)

Resize changes active worker count. If given n is greater than current worker count, it starts new workers. If given n is less than current worker count, it cancels running workers' context. This method is concurrent-safe.

func (*Pool) WorkerIDs

func (p *Pool) WorkerIDs() []string

WorkerIDs returns all worker ids.

Jump to

Keyboard shortcuts

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