advanced

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HighPriority   = 2
	MediumPriority = 1
	LowPriority    = 0
)

Priority levels

Variables

View Source
var (
	ErrPoolClosed      = errors.New("worker pool is closed")
	ErrTaskTimeout     = errors.New("task timed out")
	ErrQueueFull       = errors.New("task queue is full")
	ErrInvalidPriority = errors.New("invalid task priority")
)

Error definitions

Functions

This section is empty.

Types

type Option

type Option func(*WorkerPool)

Option configures the WorkerPool

func WithMaxWorkers

func WithMaxWorkers(n int) Option

WithMaxWorkers sets the maximum number of workers

func WithMinWorkers

func WithMinWorkers(n int) Option

WithMinWorkers sets the minimum number of workers

func WithPriorityPercentages

func WithPriorityPercentages(high, medium int) Option

WithPriorityPercentages sets worker allocation percentages

func WithQueueSize

func WithQueueSize(size int) Option

WithQueueSize sets the queue capacity

func WithTaskTimeout

func WithTaskTimeout(timeout time.Duration) Option

WithTaskTimeout sets the default task timeout

type PoolStats

type PoolStats struct {
	TotalWorkers   int32
	ActiveWorkers  int32
	HighPriority   int32
	MediumPriority int32
	QueueLengths   map[int]int
	Throughput     float64
	AvgTaskTime    time.Duration
	RejectedTasks  int32
	TimeoutTasks   int32
	HungerEvents   int32
}

PoolStats contains runtime statistics

type Task

type Task struct {
	Priority    int               // Task priority (High/Medium/Low)
	Job         func() error      // The work function
	SubmittedAt time.Time         // When task was submitted
	Timeout     time.Duration     // Timeout for this task
	ResultChan  chan<- TaskResult // Channel for results
}

Task represents a unit of work with priority

type TaskResult

type TaskResult struct {
	Err     error
	Runtime time.Duration
}

TaskResult contains task execution results

type WorkerPool

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

WorkerPool manages workers and task processing

func New

func New(options ...Option) *WorkerPool

New creates a new WorkerPool with default configuration

func (*WorkerPool) Resize

func (p *WorkerPool) Resize(n int)

Resize adjusts the worker pool size

func (*WorkerPool) Shutdown

func (p *WorkerPool) Shutdown()

Shutdown gracefully shuts down the pool

func (*WorkerPool) Stats

func (p *WorkerPool) Stats() PoolStats

Stats returns current pool statistics

func (*WorkerPool) Submit

func (p *WorkerPool) Submit(task *Task) error

Submit adds a task to the work pool

func (*WorkerPool) SubmitWithContext

func (p *WorkerPool) SubmitWithContext(ctx context.Context, task *Task) error

SubmitWithContext adds a task with context support

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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