dynworker

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrChannelClosed = errors.Base("channel closed")
)
View Source
var (
	ErrNotTask error = errors.Base("task pool entity is not a task")
)

Functions

func InitPrometheus added in v0.5.9

func InitPrometheus(reg prometheus.Registerer)

Initialise Prometheus metrics for this module.

Types

type Config

type Config struct {

	// Custom queue to use.
	InputQueue TaskQueue

	// Prometheus registerer.
	Prometheus prometheus.Registerer

	// Function to use as the worker.
	WorkerFunc TaskFn

	// Function to use to determine scaling.
	ScalerFunc ScalerFn

	// Worker pool name for logger and metrics.
	Name string

	// Minimum number of workers.
	MinWorkers int64

	// Maximum number of workers.
	MaxWorkers int64

	// Idle timeout duration.
	IdleTimeout time.Duration

	// Drain target duration.
	DrainTarget time.Duration
}

func NewConfig

func NewConfig(name string, minw, maxw int64) *Config

func NewConfigWithQueue added in v1.0.6

func NewConfigWithQueue(name string, minw, maxw int64, queue TaskQueue) *Config

Create a new configuration with a custom queue.

func NewDefaultConfig

func NewDefaultConfig() *Config

Create a new default configuration.

func (*Config) SetIdleTimeout added in v1.0.6

func (obj *Config) SetIdleTimeout(timeout time.Duration)

Set the idle timeout value.

func (*Config) SetScalerFunction added in v1.0.6

func (obj *Config) SetScalerFunction(scalefn ScalerFn)

Set the scaler function.

func (*Config) SetWorkerFunction added in v1.0.6

func (obj *Config) SetWorkerFunction(workfn TaskFn)

Set the worker function.

type ScalerFn added in v1.0.6

type ScalerFn func() int

Scaler callback function type.

type Task

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

Task structure.

func NewTask added in v0.5.9

func NewTask(ctx context.Context, lgr logger.Logger, data UserData) *Task

func (*Task) Data added in v0.5.9

func (obj *Task) Data() UserData

Get the user-supplied data for the task.

func (*Task) Logger added in v0.5.9

func (obj *Task) Logger() logger.Logger

Get the logger instance for the task.

func (*Task) Parent added in v0.5.9

func (obj *Task) Parent() context.Context

Get the parent context for the task.

type TaskFn

type TaskFn func(*Task) error

Task callback function type.

type TaskQueue added in v1.0.6

type TaskQueue interface {
	Put(context.Context, *Task) error
	Get(context.Context) (*Task, error)
	Len() int
}

func NewChanTaskQueue added in v1.0.6

func NewChanTaskQueue(size int) TaskQueue

func NewQueueTaskQueue added in v1.0.6

func NewQueueTaskQueue(q *types.Queue) TaskQueue

type UserData added in v0.5.9

type UserData any

User-supplied data.

type WorkerPool

type WorkerPool interface {
	// Start the worker pool.
	Start()

	// Stop the worker pool.
	Stop()

	// Submit a task to the worker pool.
	Submit(UserData) error

	// Return the number of current workers in the pool.
	WorkerCount() int64

	// Return the minimum number of workers in the pool.
	MinWorkers() int64

	// Return the maximum number of workers in the pool.
	MaxWorkers() int64

	// Set the minimum number of workers to the given value.
	SetMinWorkers(int64)

	// Set the maximum number of workers to the given value.
	SetMaxWorkers(int64)

	// Set the task callback function.
	SetTaskFunction(TaskFn)

	// Set the task scaler function.
	SetScalerFunction(ScalerFn)

	// Return the name of the pool.
	Name() string
}

Worker pool interface.

func NewWorkerPool

func NewWorkerPool(ctx context.Context, config *Config) WorkerPool

Create a new worker pool.

The provided context must have `logger.Logger` in its user value. See `contextdi` and `logger.SetLogger`.

Jump to

Keyboard shortcuts

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