syncs

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParallelMap

func ParallelMap[V, T any](values []V, maxWait time.Duration, mapper func(context.Context, V) (T, error)) ([]T, []error)

ParallelMap applies the given mapper to each element of the given slice in parallel. The results and errors are returned in the same order as the slice. If the context expires before all the mappers are finished, the remaining mappers are cancelled.

If a mapper fails, the error is returned in the errs slice.
If a mapper succeeds, the result is returned in the results slice.

Example:

 values := []int{1, 2, 3, 4, 5}
 results, errs := ParallelMap(values, time.Second, func(ctx context.Context, v V) (T, error) {
	    return v * v, nil
 })

func Sleep

func Sleep(ctx context.Context, delay time.Duration) bool

Sleep waits for the given duration. Returns true if the context was canceled.

func WaitAll

func WaitAll[T any](ctx context.Context, waitables ...Waitable[T]) ([]T, []error)

WaitAll waits for all Waitable to complete successfully or to fail. Cancelling the context will cause all Waitable to fail.

The results and errors are returned in the same order as the Waitable. If a Waitable fails, the error is returned in the errs slice. If a Waitable succeeds, the result is returned in the results slice. If a Waitable is cancelled, the result is returned in the results slice and the error is returned in the errs slice.

ctx (context) can be used to cancel the WaitAll. If nil is provided, a context with a deadline of 1 second is used. Remember to cancel the context when you're done with it.

Example:

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second * 3))
defer cancel()
results, errs := WaitAll(ctx, funcs...)

Types

type Waitable

type Waitable[T any] func(ctx context.Context) (T, error)

Waitable is a function that can be waited on.

Jump to

Keyboard shortcuts

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