worker

package
v0.15.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrShutdownTimeout = errors.New("worker: shutdown timed out")

ErrShutdownTimeout is returned when a shutdown operation times out.

Functions

This section is empty.

Types

type AllStrategy

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

AllStrategy creates a new goroutine for every submitted job. It implements the Strategy interface.

func NewAllStrategy

func NewAllStrategy(logger log.Logger, timeout time.Duration) *AllStrategy

NewAllStrategy creates a new AllStrategy with the specified logger and job timeout.

func (*AllStrategy) Shutdown

func (s *AllStrategy) Shutdown(timeout time.Duration) error

Shutdown waits for all currently running jobs to complete or until the specified timeout is reached. It returns an error if the shutdown times out.

func (*AllStrategy) Submit

func (s *AllStrategy) Submit(job Job) bool

Submit sends a job to the strategy. It always returns true as it creates a new goroutine for each job.

type Job

type Job func(ctx context.Context)

Job defines a unit of work to be executed asynchronously by a worker. This function should capture all necessary information for its execution as a closure.

type Manager

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

Manager manages worker strategies and dispatches jobs.

func NewManager

func NewManager(strategyType string, logger log.Logger, poolSize int, queueSize int, jobTimeout time.Duration) (*Manager, error)

NewManager creates a new worker manager with the specified strategy. jobTimeout is the maximum execution time applied to each background job.

func (*Manager) Shutdown

func (m *Manager) Shutdown(timeout time.Duration) error

Shutdown safely shuts down the worker manager. It calls the Shutdown method of the underlying strategy.

func (*Manager) Submit

func (m *Manager) Submit(job Job) bool

Submit dispatches a job to the currently configured strategy.

type PoolStrategy

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

PoolStrategy processes jobs using a fixed-size pool of workers. It implements the Strategy interface.

func NewPoolStrategy

func NewPoolStrategy(logger log.Logger, poolSize int, queueSize int, timeout time.Duration) *PoolStrategy

NewPoolStrategy creates a new PoolStrategy with the specified logger, pool size, queue size, and job timeout. If poolSize or queueSize are non-positive, default values are used.

func (*PoolStrategy) Shutdown

func (p *PoolStrategy) Shutdown(timeout time.Duration) error

Shutdown gracefully shuts down the worker pool. It closes the job channel and waits for all active workers to finish their current jobs or until the specified timeout is reached.

func (*PoolStrategy) Submit

func (s *PoolStrategy) Submit(job Job) bool

Submit attempts to submit a job to the worker pool. It returns true if the job was successfully submitted, false if the queue is full or the pool is shutting down.

type Strategy

type Strategy interface {
	// Submit attempts to submit a job for execution. It returns true if the job was accepted, false otherwise.
	Submit(job Job) bool
	// Shutdown gracefully shuts down the worker strategy within the given timeout.
	// It returns an error if the shutdown times out or encounters other issues.
	Shutdown(timeout time.Duration) error
}

Strategy defines the interface for a worker strategy.

Jump to

Keyboard shortcuts

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