worker

package
v0.82.2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package worker provides a concurrent task execution system with a configurable number of workers.

It allows for controlled parallel execution of tasks while managing resources efficiently through a semaphore-based worker pool. Key features include:

- Configurable maximum number of concurrent workers - Non-blocking task submission - Graceful shutdown capabilities - Error collection and aggregation - Thread-safe operations

The Pool struct manages a pool of workers that can execute tasks concurrently while limiting the number of goroutines running simultaneously. This prevents resource exhaustion while maximizing throughput.

This implementation is particularly useful for scenarios where you need to process many independent tasks with controlled parallelism, such as in infrastructure management tools, batch processing systems, or any application requiring concurrent execution with resource constraints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

Pool manages concurrent task execution with a configurable number of workers

func NewWorkerPool

func NewWorkerPool(maxWorkers int) *Pool

NewWorkerPool creates a new worker pool with the specified maximum number of concurrent workers

func (*Pool) GracefulStop

func (wp *Pool) GracefulStop() error

GracefulStop waits for all tasks to complete before stopping the pool

func (*Pool) IsRunning

func (wp *Pool) IsRunning() bool

IsRunning returns whether the pool is currently running

func (*Pool) IsStopping

func (wp *Pool) IsStopping() bool

IsStopping returns whether the pool is in the process of stopping

func (*Pool) Start

func (wp *Pool) Start()

Start initializes the worker pool

func (*Pool) Stop

func (wp *Pool) Stop()

Stop shuts down the worker pool after current tasks are completed

func (*Pool) Submit

func (wp *Pool) Submit(task Task)

Submit adds a new task and starts a goroutine to execute it when a worker is available

func (*Pool) Wait

func (wp *Pool) Wait() error

Wait blocks until all tasks are completed and returns any errors

type Task

type Task func() error

Task represents a unit of work that can be executed

Jump to

Keyboard shortcuts

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