Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Parallel ¶ added in v1.2.56
type Parallel[T any] struct { // contains filtered or unexported fields }
Parallel provides a mechanism to process multiple items in parallel using a fixed number of workers, each applying the same processing function. This is useful for scenarios where you have a large number of similar items to process and want to leverage concurrency to speed up the work.
Type Parameters:
T: The type of the items to be processed.
func NewParallel ¶ added in v1.2.56
NewParallel creates and returns a new Parallel instance.
Parameters:
maxWorkers: The maximum number of concurrent workers. capacity: The capacity of the buffered queue for items to be processed.
Returns:
A new Parallel instance.
func (*Parallel[T]) Enqueue ¶ added in v1.2.56
Enqueue adds an item to the buffered queue to be processed. This method is non-blocking and will return `false` if the queue is full.
Parameters:
item: The item to be enqueued.
Returns:
`true` if the item was successfully enqueued, `false` otherwise.
func (*Parallel[T]) EnqueueWithTimeout ¶ added in v1.2.56
EnqueueWithTimeout adds an item to the buffered queue with a timeout. It will wait for the specified duration for space to become available.
Parameters:
item: The item to be enqueued. timeout: The maximum time to wait.
Returns:
`true` if the item was successfully enqueued, `false` if the timeout was reached.
func (*Parallel[T]) Start ¶ added in v1.2.56
Start initializes and starts the parallel processing pool. It creates the workers and starts the dispatcher to distribute items to them.
Parameters:
processor: The function that will be applied to each item.
Returns:
An error if the pool has already been started.
type Task ¶
type Task[T any] interface { // Run executes the task and returns a result of type T. Run() T }
Task represents a unit of work that can be executed by a worker in the pool. It is a generic interface, allowing for any type of task that returns a value of type T.
Type Parameters:
T: The type of the result returned by the task.
type WorkerPool ¶
type WorkerPool[T any] struct { // contains filtered or unexported fields }
WorkerPool manages a pool of workers to execute tasks concurrently. It provides methods for submitting tasks and managing the lifecycle of the pool.
Type Parameters:
T: The type of the result returned by the tasks.
func NewWorkerPool ¶
func NewWorkerPool[T any](maxWorkers int, capacity int) *WorkerPool[T]
NewWorkerPool creates and returns a new WorkerPool.
Parameters:
maxWorkers: The maximum number of workers in the pool. capacity: The capacity of the buffered job queue.
Returns:
A new WorkerPool instance.
func (*WorkerPool[T]) Enqueue ¶ added in v1.2.53
func (q *WorkerPool[T]) Enqueue(task Task[T]) bool
Enqueue adds a task to the buffered job queue. This method is non-blocking and will return `false` if the queue is full.
Parameters:
task: The task to be enqueued.
Returns:
`true` if the task was successfully enqueued, `false` otherwise.
func (*WorkerPool[T]) EnqueueWithTimeout ¶ added in v1.2.53
func (q *WorkerPool[T]) EnqueueWithTimeout(task Task[T], timeout time.Duration) bool
EnqueueWithTimeout adds a task to the buffered job queue with a timeout. It will wait for the specified duration for space to become available in the queue.
Parameters:
task: The task to be enqueued. timeout: The maximum time to wait for the task to be enqueued.
Returns:
`true` if the task was successfully enqueued, `false` if the timeout was reached.
func (*WorkerPool[T]) Start ¶ added in v1.2.53
func (q *WorkerPool[T]) Start(callback func(T)) error
Start initializes and starts the worker pool. It creates the specified number of workers and starts the dispatcher to distribute tasks.
Parameters:
callback: An optional function to be called with the result of each completed task.
Returns:
An error if the pool has already been started.
func (*WorkerPool[T]) Stop ¶ added in v1.2.53
func (q *WorkerPool[T]) Stop()
Stop gracefully shuts down the worker pool. It signals the dispatcher to stop, waits for all workers to finish their current tasks, and then stops the dispatcher.
func (*WorkerPool[T]) Submit ¶ added in v1.2.53
func (q *WorkerPool[T]) Submit(task Task[T])
Submit adds a task to the job queue. This method will block until a worker is available to process the task.
Parameters:
task: The task to be submitted.