group

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoAsync

func DoAsync[R any](ctx context.Context, g *Group, fn func() (R, error)) *async.Future[R]

DoAsync calls the given function in a new goroutine.

If there is a limit on active goroutines within the group, it blocks until it can be spawned without surpassing the limit. If the passed context is canceled, a failed future is returned.

If the group was created with Option WithCancel, the first call that returns a non-nil error will cancel the group's context. This error will subsequently be returned by Group.Wait.

Types

type ExecutionError

type ExecutionError struct {
	Value any
}

func (ExecutionError) Error

func (e ExecutionError) Error() string

type Group

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

A Group represents a set of collaborating goroutines within a common overarching task.

Should this group have a limit of active goroutines or cancel on the first error it needs to be created with New.

func New

func New(opts ...Option) *Group

New creates a new Group with the given options.

func (*Group) Do

func (g *Group) Do(fn func() error)

Do calls the given function synchronously.

If the group was created with Option WithCancel, the first call that returns a non-nil error will cancel the group's context. This error will subsequently be returned by Group.Wait.

func (*Group) Go

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

Go calls the given function in a new goroutine.

It is here for compatibility reasons and calls Group.GoCtx with context.TODO. You should consider calling that function instead.

func (*Group) GoCtx

func (g *Group) GoCtx(ctx context.Context, fn func() error)

GoCtx calls the given function in a new goroutine.

If there is a limit on active goroutines within the group, it blocks until it can be spawned without surpassing the limit. If the passed context is canceled, an error is returned.

If the group was created with Option WithCancel, the first call that returns a non-nil error will cancel the group's context. This error will subsequently be returned by Group.Wait.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all goroutines spawned from Group.Go and DoAsync are finished and returns the first non-nil error from them.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option defines configurations for New.

func WithCancel

func WithCancel(cancel context.CancelCauseFunc) Option

WithCancel is an Option to cancel a context on the first error.

cancel is a function retrieved from context.WithCancelCause.

func WithLimit

func WithLimit(limit int) Option

WithLimit is an Option to configure the limit of active goroutines.

Jump to

Keyboard shortcuts

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