goroutines

package
v2.0.19 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: Apache-2.0 Imports: 4 Imported by: 17

Documentation

Index

Constants

View Source
const (
	TimeoutError     = strErr("timed out")
	QueueFullError   = strErr("queue full")
	PoolStoppedError = strErr("pool shutdown")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool interface {
	// Queue submits a unit of work to the pool. It will return an error if the pool is shutdown
	Queue(func()) error

	// QueueWithTimeout submits a unit of work to the pool. It will return an error if the pool is shutdown or
	// if the work cannot be submitted to the work queue before the given timeout elapses
	QueueWithTimeout(func(), time.Duration) error

	// QueueOrError submits a unit of work to the pool. It will return an error if the pool is shutdown or
	// if the work cannot be submitted to the work queue immediately
	QueueOrError(func()) error

	// GetWorkerCount returns the current number of goroutines servicing the work queue
	GetWorkerCount() uint32

	// GetQueueSize returns the current number of work items in the work queue
	GetQueueSize() uint32

	// GetBusyWorkers returns the current number workers busy doing work from the work queue
	GetBusyWorkers() uint32

	// Shutdown stops all workers as they finish work and prevents new work from being submitted to the queue
	Shutdown()
}

Pool represents a goroutine worker pool that can be configured with a queue size and min and max sizes.

The pool will start with min size goroutines and will add more if the queue isn't staying empty.
After a worker has been idle for a configured time, it will stop

func NewPool

func NewPool(config PoolConfig) (Pool, error)

type PoolConfig

type PoolConfig struct {
	// The size of the channel feeding the worker pool
	QueueSize uint32
	// The minimum number of goroutines
	MinWorkers uint32
	// The maximum number of workers
	MaxWorkers uint32
	// IdleTime how long a goroutine should be idle before exiting
	IdleTime time.Duration
	// Provides a way to join shutdown of the pool with other components.
	// The pool also be shut down independently using the Shutdown method
	CloseNotify <-chan struct{}
	// Provides a way to specify what happens if a worker encounters a panic
	// if no PanicHandler is provided, panics will not be caught
	PanicHandler func(err interface{})
	// Optional callback which is called whenever work completes, with the
	// time the work took to complete
	OnWorkCallback func(workTime time.Duration)
	// Optional callback which is called when the pool is created
	OnCreate func(Pool)
}

PoolConfig is used to configure a new Pool

func (*PoolConfig) Validate

func (self *PoolConfig) Validate() error

Jump to

Keyboard shortcuts

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