Documentation
¶
Index ¶
Constants ¶
const ( // Constant for an unbounded queue Unbounded = math.MaxInt DefaultQueueSize = Unbounded DefaultNonBlocking = false LinkedBufferInitialSize = 1024 LinkedBufferMaxCapacity = 100 * 1024 )
Variables ¶
var ( ErrQueueFull = errors.New("queue is full") ErrQueueEmpty = errors.New("queue is empty") ErrPoolStopped = errors.New("pool stopped") ErrMaxConcurrencyReached = errors.New("max concurrency reached") )
var ErrContextCanceled = errors.New("context canceled")
var ErrGroupStopped = errors.New("task group stopped")
var ErrPanic = errors.New("task panicked")
Functions ¶
This section is empty.
Types ¶
type BasePool ¶ added in v2.7.1
type BasePool interface {
// Returns the number of worker goroutines that are currently active (executing a task) in the pool.
RunningWorkers() int64
// Returns the total number of tasks submitted to the pool since its creation.
SubmittedTasks() uint64
// Returns the number of tasks that are currently waiting in the pool's queue.
WaitingTasks() uint64
// Returns the number of tasks that have completed with an error.
FailedTasks() uint64
// Returns the number of tasks that have completed successfully.
SuccessfulTasks() uint64
// Returns the total number of tasks that have completed (either successfully or with an error).
// Tasks accepted by the pool but canceled before execution are excluded.
CompletedTasks() uint64
// Returns the number of tasks that have been dropped because the queue was full.
DroppedTasks() uint64
// Returns the number of tasks accepted by the pool that were canceled
// before executing user code due to pool context cancellation.
CanceledTasks() uint64
// Returns the maximum concurrency of the pool.
MaxConcurrency() int
// Returns the size of the task queue.
QueueSize() int
// Returns true if the pool is non-blocking, meaning that it will not block when the task queue is full.
// In a non-blocking pool, tasks that cannot be submitted to the queue will be dropped.
// By default, pools are blocking, meaning that they will block when the task queue is full.
NonBlocking() bool
// Returns the context associated with this pool.
Context() context.Context
// Stops the pool and returns a future that can be used to wait for all tasks pending to complete.
// The pool will not accept new tasks after it has been stopped.
Stop() Task
// Stops the pool and waits for all tasks to complete.
StopAndWait()
// Returns true if the pool has been stopped or its context has been cancelled.
Stopped() bool
// Resizes the pool by changing the maximum concurrency (number of workers) of the pool.
// The new max concurrency must be greater than 0.
// If the new max concurrency is less than the current number of running workers, the pool will continue to run with the new max concurrency.
Resize(maxConcurrency int)
}
BasePool defines methods common to all pool types.
type Option ¶
type Option func(*pool)
func WithContext ¶
WithContext sets the context for the pool.
func WithNonBlocking ¶ added in v2.2.0
WithNonBlocking sets the pool to be non-blocking when the queue is full. This option is only effective when the queue size is set.
func WithQueueSize ¶ added in v2.2.0
WithQueueSize sets the max number of elements that can be queued in the pool.
func WithoutPanicRecovery ¶ added in v2.6.0
func WithoutPanicRecovery() Option
WithoutPanicRecovery disables panic interception inside worker goroutines. When this option is enabled, panics inside tasks will propagate just like regular goroutines.
type Pool ¶
type Pool interface {
BasePool
// Submits a task to the pool without waiting for it to complete.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, this method will return ErrPoolStopped.
Go(task func()) error
// Submits a task to the pool and returns a future that can be used to wait for the task to complete.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, the returned future will resolve to ErrPoolStopped.
Submit(task func()) Task
// Submits a task to the pool and returns a future that can be used to wait for the task to complete.
// The task function must return an error.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, the returned future will resolve to ErrPoolStopped.
SubmitErr(task func() error) Task
// Attempts to submit a task to the pool and returns a future that can be used to wait for the task to complete
// and a boolean indicating whether the task was submitted successfully.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, the returned future will resolve to ErrPoolStopped.
TrySubmit(task func()) (Task, bool)
// Attempts to submit a task to the pool and returns a future that can be used to wait for the task to complete
// and a boolean indicating whether the task was submitted successfully.
// The task function must return an error.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, the returned future will resolve to ErrPoolStopped.
TrySubmitErr(task func() error) (Task, bool)
// Creates a new subpool with the specified maximum concurrency and options.
NewSubpool(maxConcurrency int, options ...Option) Pool
// Creates a new task group.
NewGroup() TaskGroup
// Creates a new task group with the specified context.
NewGroupContext(ctx context.Context) TaskGroup
}
Represents a pool of goroutines that can execute tasks concurrently.
func NewPool ¶
NewPool creates a new pool with the given maximum concurrency and options. The new maximum concurrency must be greater than or equal to 0 (0 means no limit).
func NewSubpool ¶
NewSubpool creates a new subpool with the default pool.
type Result
deprecated
type Result[R any] interface { // Done returns a channel that is closed when the task is complete or has failed. Done() <-chan struct{} // Wait waits for the task to complete and returns the result and any error that occurred. Wait() (R, error) }
Result is deprecated. Use ResultTask instead. This interface is maintained for backward compatibility.
Deprecated: Use ResultTask instead.
type ResultPool ¶
type ResultPool[R any] interface { BasePool // Submits a task to the pool and returns a future that can be used to wait for the task to complete and get the result. // The pool will not accept new tasks after it has been stopped. // If the pool has been stopped, this method will return ErrPoolStopped. Submit(task func() R) ResultTask[R] // Submits a task to the pool and returns a future that can be used to wait for the task to complete and get the result. // The task function must return a result and an error. // The pool will not accept new tasks after it has been stopped. // If the pool has been stopped, this method will return ErrPoolStopped. SubmitErr(task func() (R, error)) ResultTask[R] // Attempts to submit a task to the pool and returns a future that can be used to wait for the task to complete // and a boolean indicating whether the task was submitted successfully. // The pool will not accept new tasks after it has been stopped. // If the pool has been stopped, this method will return ErrPoolStopped. TrySubmit(task func() R) (ResultTask[R], bool) // Attempts to submit a task to the pool and returns a future that can be used to wait for the task to complete // and a boolean indicating whether the task was submitted successfully. // The task function must return a result and an error. // The pool will not accept new tasks after it has been stopped. // If the pool has been stopped, this method will return ErrPoolStopped. TrySubmitErr(task func() (R, error)) (ResultTask[R], bool) // Creates a new subpool with the specified maximum concurrency and options. NewSubpool(maxConcurrency int, options ...Option) ResultPool[R] // Creates a new task group. NewGroup() ResultTaskGroup[R] // Creates a new task group with the specified context. NewGroupContext(ctx context.Context) ResultTaskGroup[R] }
ResultPool is a pool that can be used to submit tasks that return a result.
func NewResultPool ¶
func NewResultPool[R any](maxConcurrency int, options ...Option) ResultPool[R]
NewResultPool creates a new result pool with the given maximum concurrency and options. Result pools are generic pools that can be used to submit tasks that return a result. The new maximum concurrency must be greater than or equal to 0 (0 means no limit).
type ResultTask ¶ added in v2.5.0
type ResultTask[R any] interface { // Done returns a channel that is closed when the task is complete or has failed. Done() <-chan struct{} // Wait waits for the task to complete and returns the result and any error that occurred. Wait() (R, error) }
ResultTask represents a task that yields a result. If the task fails, the error can be retrieved.
type ResultTaskGroup ¶
type ResultTaskGroup[O any] interface { // Submits a task to the group. Submit(tasks ...func() O) ResultTaskGroup[O] // Submits a task to the group that can return an error. SubmitErr(tasks ...func() (O, error)) ResultTaskGroup[O] // Waits for all tasks in the group to complete and returns the results of each task in the order they were submitted. // If any of the tasks return an error, the group will return the first error encountered. // If the context is cancelled, the group will return the context error. // If the group is stopped, the group will return ErrGroupStopped. // If a task is running when the context is cancelled or the group is stopped, the task will be allowed to complete before returning. Wait() ([]O, error) // Returns a channel that is closed when all tasks in the group have completed, a task returns an error, or the group is stopped. Done() <-chan struct{} // Stops the group and cancels all remaining tasks. Running tasks are not interrupted. Stop() }
ResultTaskGroup represents a group of tasks that can be executed concurrently. As opposed to TaskGroup, the tasks in a ResultTaskGroup yield a result. The group can be waited on to block until all tasks have completed. If any of the tasks return an error, the group will return the first error encountered.
type Task ¶
type Task interface {
// Done returns a channel that is closed when the task is complete or has failed.
Done() <-chan struct{}
// Wait waits for the task to complete and returns any error that occurred.
Wait() error
}
Task represents a task that can be waited on. If the task fails, the error can be retrieved.
type TaskGroup ¶
type TaskGroup interface {
// Submits a task to the group.
Submit(tasks ...func()) TaskGroup
// Submits a task to the group that can return an error.
SubmitErr(tasks ...func() error) TaskGroup
// Waits for all tasks in the group to complete.
// If any of the tasks return an error, the group will return the first error encountered.
// If the context is cancelled, the group will return the context error.
// If the group is stopped, the group will return ErrGroupStopped.
// If a task is running when the context is cancelled or the group is stopped, the task will be allowed to complete before returning.
Wait() error
// Returns a channel that is closed when all tasks in the group have completed, a task returns an error, or the group is stopped.
Done() <-chan struct{}
// Stops the group and cancels all remaining tasks. Running tasks are not interrupted.
Stop()
// Returns the context associated with this group.
// This context will be cancelled when either the parent context is cancelled
// or any task in the group returns an error, whichever comes first.
Context() context.Context
}
TaskGroup represents a group of tasks that can be executed concurrently. The group can be waited on to block until all tasks have completed. If any of the tasks return an error, the group will return the first error encountered.