execution

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TaskGroup

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

A TaskGroup manages a collection of cooperating goroutines. Add new tasks to the group with the Run method. Call the Wait method to wait for the tasks to complete. A zero value is ready for use, but must not be copied after its first use.

The group collects any errors returned by the tasks in the group. The first non-nil error reported by any execution and not filtered is returned from the Wait method.

func NewTaskGroup

func NewTaskGroup(handler func(error) error) *TaskGroup

NewTaskGroup constructs a new empty group with the specified error handler. See TaskGroup.OnError for a description of how errors are filtered. If handler is nil, no filtering is performed. Main properties of the TaskGroup are: - Cancel propagation. - Error propagation. - Waiting for all tasks to finish.

func (*TaskGroup) OnError

func (g *TaskGroup) OnError(handler func(error) error) *TaskGroup

OnError sets the error handler for TaskGroup. If handler is nil, the error handler is removed and errors are no longer filtered. Otherwise, each non-nil error reported by an execution running in g is passed to handler.

Then handler is called with each reported error, and its result replaces the reported value. This permits handler to suppress or replace the error value selectively.

Calls to handler are synchronized so that it is safe for handler to manipulate local data structures without additional locking. It is safe to call OnError while tasks are active in TaskGroup.

func (*TaskGroup) Run

func (g *TaskGroup) Run(execute func() error)

Run starts an [execute] function in a new goroutine in TaskGroup. The execution is not interrupted by TaskGroup, so the [execute] function should include the interruption logic.

func (*TaskGroup) Wait

func (g *TaskGroup) Wait() error

Wait blocks until all the goroutines currently active in the TaskGroup have returned, and all reported errors have been delivered to the handler. It returns the first non-nil error reported by any of the goroutines in the group and not filtered by an OnError handler.

As with sync.WaitGroup, new tasks can be added to TaskGroup during a Wait call only if the TaskGroup contains at least one active execution when Wait is called and continuously thereafter until the last concurrent call to Run returns.

Wait may be called from at most one goroutine at a time. After Wait has returned, the group is ready for reuse.

Jump to

Keyboard shortcuts

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