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 ErrGroupStopped = errors.New("task group stopped")
var ErrPanic = errors.New("task panicked")
Functions ¶
This section is empty.
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 {
// 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
// contains filtered or unexported methods
}
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 { // 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] // contains filtered or unexported methods }
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.