task

package
v2.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 7 Imported by: 3

Documentation

Overview

Package task provides a global task scheduling pool and a task group.

It is built on the ants goroutine pool and supports concurrency control, error aggregation and graceful degradation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v2.6.0

func Add(task func())

Add runs task.

The task is submitted to the global task pool first; it degrades to running in a directly created goroutine when the pool is full or unavailable.

func AddTask deprecated

func AddTask(task func())

AddTask adds task to the global task pool.

Deprecated: As of due v2.6.0+, this function simply calls Add.

func NewPool

func NewPool(opts ...Option) *defaultPool

NewPool returns a new task pool.

It creates an ants goroutine pool from the configured options and supports features such as the pool size, non-blocking mode and disabled purging.

func Release

func Release()

Release releases the global task pool.

func SetPool

func SetPool(pool Pool)

SetPool sets the global task pool, releasing the old pool first.

Types

type Group added in v2.6.0

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

Group aggregates the errors of multiple tasks and supports context cancellation and a concurrency limit.

func WithContext added in v2.6.0

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext returns a new Group and a context derived from ctx.

The derived context is canceled with the first task error as its cause when the first task fails or when Wait returns.

func (*Group) Go added in v2.6.0

func (g *Group) Go(f func() error)

Go runs f as a task.

The task runs in the global task pool and is subject to the concurrency limit. When the task fails, its error is recorded if it is the first one and the context is canceled.

func (*Group) SetLimit added in v2.6.0

func (g *Group) SetLimit(n int)

SetLimit sets the concurrency limit to n.

The limit must be set before any task is started. A negative n means no limit. Changing the limit while tasks are running panics.

func (*Group) TryGo added in v2.6.0

func (g *Group) TryGo(f func() error) bool

TryGo tries to run f as a task.

It starts the task immediately and reports true when the concurrency limit has not been reached; otherwise it reports false.

func (*Group) Wait added in v2.6.0

func (g *Group) Wait() error

Wait blocks until all tasks have finished and cancels the derived context. It returns the error of the first failing task, or nil when no task failed.

type Option

type Option func(o *options)

Option configures the task pool.

func WithDisablePurge

func WithDisablePurge(disablePurge bool) Option

WithDisablePurge returns an Option that sets whether purging is disabled.

When purging is disabled, idle goroutines are not reclaimed periodically.

func WithNonblocking

func WithNonblocking(nonblocking bool) Option

WithNonblocking returns an Option that sets whether the pool is non-blocking.

In non-blocking mode, a task submission returns an error immediately when the pool is full instead of blocking and waiting.

func WithSize

func WithSize(size int) Option

WithSize returns an Option that sets the task pool size.

type Pool

type Pool interface {
	// AddTask adds task to the pool and returns any error.
	AddTask(task func()) error
	// Release releases the task pool.
	Release()
}

Pool is a task pool.

func GetPool

func GetPool() Pool

GetPool returns the global task pool.

Jump to

Keyboard shortcuts

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