async

package
v0.5.22 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoEg

func GoEg[T any](
	w EgWaiter,
	f func(context.Context) (T, error),
	pResult *T,
)

GoEg launches f as a saparate goroutine and puts its non-error result in the struct pointed to by pResult. Before using the result, a caller must wait on the Waiter passed into this function. If f panics, the panic value is converted to an error and set in the result.

func GoWg

func GoWg[T any](
	w WgWaiter,
	f func(context.Context) (T, error),
	pResult *ResultWithError[T],
)

GoWg launches f as a saparate goroutine and puts its result in the struct pointed to by pResult. Before using the result, a caller must wait on the Waiter passed into this function. If f panics, the panic value is converted to an error and set in the result.

func RunConcurrent2Eg

func RunConcurrent2Eg[T1, T2 any](
	ctx context.Context,
	f1 func(context.Context) (T1, error),
	f2 func(context.Context) (T2, error),
) (util.Tuple2[T1, T2], error)

RunConcurrent2Eg runs funcs concurrently and returns a slice containing the non-error results of the function executions if all functions complete normaly. If any of the functions returns an error or panics, this function returns early, with the first error encountered. Panics in function executions are converted to errors. In case of a context timeout or cancellation, this functionn returns early with a TimeoutError or CancellationError.

func RunConcurrent2Wg

func RunConcurrent2Wg[T1, T2 any](
	ctx context.Context,
	f1 func(context.Context) (T1, error),
	f2 func(context.Context) (T2, error),
) (util.Tuple2[ResultWithError[T1], ResultWithError[T2]], error)

RunConcurrent2Wg runs funcs concurrently and returns a slice containing the results of the function executions once all functions complete normaly, with an error, or with a panic. Panics in function executions are converted to errors. In case of a context timeout or cancellation, this functionn returns early with a TimeoutError or CancellationError.

func RunConcurrentsEg

func RunConcurrentsEg[T any](
	ctx context.Context,
	funcs ...func(context.Context) (T, error),
) ([]T, error)

RunConcurrentsEg runs funcs concurrently and returns a slice containing the non-error results of the function executions if all functions complete normaly. If any of the functions returns an error or panics, this function returns early, with the first error encountered. Panics in function executions are converted to errors. In case of a context timeout or cancellation, this functionn returns early with a TimeoutError or CancellationError.

func RunInBackground

func RunInBackground[T any](
	ctx context.Context,
	f func(context.Context) (T, error),
	errorHandler func(error),
) (T, error)

RunInBackground runs a function in the background as a goroutine. This function returns early if the context ctx is cancelled or times-out. In all cases, errorHandler is executed if f returns an error, even if f completes after the context ctx is cancelled.

Types

type EgWaiter

type EgWaiter struct {
	P     preWaiter // embedded structs not supported by go2go
	Eg    *errgroup.Group
	EgCtx context.Context
}

EgWaiter supports waiting on the completion of a group of goroutines associated with an errgroup.Group.

func MakeEgWaiter

func MakeEgWaiter(parentCtx context.Context) EgWaiter

MakeEgWaiter constructs an EgWaiter.

func (EgWaiter) Wait

func (w EgWaiter) Wait() error

Wait waits on both the WaitCh channel field in the receiver and the Done() channel for the Ctx context field in the receiver. If Ctx.Done() happens first then this method returns a DeadlineExceeded or a Canceled error. Otherwise, it returns the error received in WaitCh. This function may be called multiple times and always returns the same results.

type Promise

type Promise[T any] interface {
	Await() (T, error)
}

Promise supports awaiting on and receiving the result of an asynchronous computation.

func Async

func Async[T any](
	ctx context.Context,
	f func(context.Context) (T, error),
) Promise[T]

Async constructs a Promise for the asynchronous execution of f.

func Async2Eg

func Async2Eg[T1 any, T2 any](
	ctx context.Context,
	f1 func(ctx context.Context) (T1, error),
	f2 func(ctx context.Context) (T2, error),
) Promise[util.Tuple2[T1, T2]]

Async2Eg returns a Promise for the concurrent execution of the functions f1 and f2 in an errgroup.Group. The Promise completes when the error group Wait method returns.

func Async2Wg

func Async2Wg[T1 any, T2 any](
	ctx context.Context,
	f1 func(ctx context.Context) (T1, error),
	f2 func(ctx context.Context) (T2, error),
) Promise[util.Tuple2[ResultWithError[T1], ResultWithError[T2]]]

Async2Wg returns a Promise for the concurrent execution of the functions f1 and f2 in a sync.WaitGroup. The Promise completes when the wait group Wait method returns.

func AsyncsEg

func AsyncsEg[T any](
	ctx context.Context,
	funcs ...func(ctx context.Context) (T, error),
) Promise[[]T]

AsyncsEg returns a Promise for the concurrent execution of the functions funcs in an errgroup.Group. The Promise completes when the error group Wait method returns.

func AsyncsWg

func AsyncsWg[T any](
	ctx context.Context,
	funcs ...func(ctx context.Context) (T, error),
) Promise[[]ResultWithError[T]]

AsyncsWg returns a Promise for the concurrent execution of the functions funcs in a sync.WaitGroup. The Promise completes when the wait group Wait method returns.

type ResultWithError

type ResultWithError[T any] struct {
	Value T
	Error error
}

ResultWithError encapsulates a normal result value and an error.

func RunConcurrentsWg

func RunConcurrentsWg[T any](
	ctx context.Context,
	funcs ...func(context.Context) (T, error),
) ([]ResultWithError[T], error)

RunConcurrentsWg runs funcs concurrently and returns a slice containing the results of the function executions once all functions complete normaly, with an error, or with a panic. Panics in function executions are converted to errors. In case of a context timeout or cancellation, this functionn returns early with a TimeoutError or CancellationError.

type SingleWaiter

type SingleWaiter struct {
	P       preWaiter // embedded structs not supported by go2go
	ComplCh chan Unit
}

SingleWaiter supports waiting on the completion of a single goroutine.

func Go

func Go[T any](
	ctx context.Context,
	f func(context.Context) (T, error),
	pResult *ResultWithError[T],
) SingleWaiter

Go launches f as a saparate goroutine and puts its result in the struct pointed to by pResult. Before using the result, a caller must wait on the Waiter returned by this function. If f panics, the panic value is converted to an error and set in the result.

func MakeSingleWaiter

func MakeSingleWaiter(ctx context.Context) SingleWaiter

MakeSingleWaiter constructs a SingleWaiter.

func (SingleWaiter) Wait

func (w SingleWaiter) Wait() error

Wait waits on both the WaitCh channel field in the receiver and the Done() channel for the Ctx context field in the receiver. If Ctx.Done() happens first then this method returns a DeadlineExceeded or a Canceled error. Otherwise, it returns the error received in WaitCh. This function may be called multiple times and always returns the same results.

type Unit

type Unit = struct{}

Unit is a type alias

type WgWaiter

type WgWaiter struct {
	P  preWaiter // embedded structs not supported by go2go
	Wg *sync.WaitGroup
}

WgWaiter supports waiting on the completion of a group of goroutines associated with a sync.WaitGroup.

func MakeWgWaiter

func MakeWgWaiter(ctx context.Context) WgWaiter

MakeWgWaiter constructs a WgWaiter.

func (WgWaiter) Wait

func (w WgWaiter) Wait() error

Wait waits on both the WaitCh channel field in the receiver and the Done() channel for the Ctx context field in the receiver. If Ctx.Done() happens first then this method returns a DeadlineExceeded or a Canceled error. Otherwise, it returns the error received in WaitCh. This function may be called multiple times and always returns the same results.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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