laborpool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 5 Imported by: 1

README

Labor pool

Workers pool implementation to reduce runtime.findrunnable pressure.

The main idea: reduce goroutines creation and release by storing existing goroutines to pool for further reuse.

Usage

pool := NewPool(500, 0.001)
in := make(chan interface{}, 10)
for {
	x, ok := <- in
	if !ok {
		break
	}
	worker := pool.Hire() // get existing worker from pool or create new one
	_ = worker.Do(func() error {
		// do something with x
		return nil
	})
	pool.Fire(worker) // put worker back to poll to use it later without creating
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWorkerStatus = errors.New("uninitialized worker, use pool.Hire()")
)

Functions

This section is empty.

Types

type DummyMetrics

type DummyMetrics struct{}

DummyMetrics is a stub metrics writer handler that uses by default and does nothing. Need just to reduce checks in code.

func (DummyMetrics) Fire

func (DummyMetrics) Fire()

func (DummyMetrics) Hire

func (DummyMetrics) Hire(bool)

func (DummyMetrics) Retire

func (DummyMetrics) Retire()

type JobFn

type JobFn func() error

JobFn describes worker job.

type MetricsWriter

type MetricsWriter interface {
	// Hire registers taking worker from the pool.
	Hire(unknown bool)
	// Fire registers returning worker to the pool.
	Fire()
	// Retire registers worker leak (with release).
	Retire()
}

MetricsWriter is an interface of metrics handler. See example of implementations https://github.com/koykov/metrics_writers/tree/master/laborpool.

type Pool

type Pool struct {
	// Size indicates how many workers may be stored in the pool.
	// Workers that overflows that limit will release immediately.
	Size uint
	// PensionFactor indicates the possibility to retire worker to the pension.
	PensionFactor float32
	// MetricsWriter writers pool metrics.
	MetricsWriter MetricsWriter
	// contains filtered or unexported fields
}

func NewPool

func NewPool(size uint, rate float32) *Pool

NewPool makes new pool instance with dummy MetricsWriter.

func NewPoolWM

func NewPoolWM(size uint, rate float32, mw MetricsWriter) *Pool

NewPoolWM makes new pool instance with given MetricsWriter.

func (*Pool) Fire

func (p *Pool) Fire(w *Worker)

func (*Pool) Hire

func (p *Pool) Hire() *Worker

Hire returns new worker from the underlying pool.

type Worker

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

func (*Worker) Do

func (w *Worker) Do(job JobFn) error

Do take a job in work.

func (*Worker) Release

func (w *Worker) Release()

Release stops worker.

Jump to

Keyboard shortcuts

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