hofp

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 17

Documentation

Overview

Package hofp exposes a generic higher order function pool which abstracts aways the logic/error handling required to perform tasks concurrently by wrapping complex tasks into a common 'func(context.Context) error' interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Function

type Function func(ctx context.Context) error

Function is a higher order function to be executed by the worker pool, where possible, the function should honor the cancellation of the given context and return as quickly/cleanly as possible.

type Options

type Options struct {
	// Context used by the worker pool, if omitted a background context will be used.
	Context context.Context

	// Size dictates the number of goroutines created to process incoming functions. Defaults to the number of vCPUs.
	Size int

	// BufferMultiplier is the multiplier used when determining how may functions can be buffered for processioning
	// before calls to 'Queue' block. This value is multiplied by the number of goroutines, and defaults to one.
	BufferMultiplier int

	// Logger is the passed Logger struct that implements the Log method for logger the user wants to use.
	Logger *slog.Logger
}

Options encapsulates the available options which can be used when creating a worker pool.

type Pool

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

Pool is a generic higher order function worker pool which executes the provided functions concurrently using a configurable number of workers.

NOTE: Fails fast in the event of an error, subsequent attempts to use the worker pool will return the error which caused the pool to stop processing requests.

func NewPool

func NewPool(opts Options) *Pool

NewPool returns a new higher order function worker pool with the provided number of workers.

func (*Pool) Queue

func (p *Pool) Queue(fn Function) error

Queue a function for execution by the worker pool, returns an error if the worker pool has encountered an error and is tearing down. This return value should be used to prematurely stop queuing work, or to initiate teardown of the wrapping workload.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the number of workers in the pool.

func (*Pool) Stop

func (p *Pool) Stop() error

Stop the worker pool gracefully executing any remaining functions. Subsequent calls to 'Stop' will only return the error which caused the pool to tear down (if there was one).

Jump to

Keyboard shortcuts

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