Documentation
¶
Overview ¶
Package goroutine helps to manage goroutines safely.
It recovers panic with panichandle.
Index ¶
- func CancelWait(ctx context.Context, f func(ctx context.Context)) (cancelWait func())
- func Iter[In, Out any](ctx context.Context, in iter.Seq[In], workers int, ...) iter.Seq[Out]
- func IterOrdered[In, Out any](ctx context.Context, in iter.Seq[In], workers int, ...) iter.Seq[Out]
- func Map[MIn ~map[K]In, MOut map[K]Out, K comparable, In, Out any](ctx context.Context, in MIn, workers int, ...) MOut
- func MapError[MIn ~map[K]In, MOut map[K]Out, K comparable, In, Out any](ctx context.Context, in MIn, workers int, ...) (MOut, error)
- func N(ctx context.Context, n int, f func(ctx context.Context))
- func Services(ctx context.Context, services map[string]func(context.Context) error) error
- func Slice[SIn ~[]In, SOut []Out, In, Out any](ctx context.Context, in SIn, workers int, ...) SOut
- func SliceError[SIn ~[]In, SOut []Out, In, Out any](ctx context.Context, in SIn, workers int, ...) (SOut, error)
- func Start(ctx context.Context, f func(ctx context.Context))
- func Wait(ctx context.Context, f func(ctx context.Context)) (wait func())
- func WaitGroup(ctx context.Context, wg *sync.WaitGroup, f func(ctx context.Context))
- func WithError[In, Out any](f func(context.Context, In) (Out, error)) func(context.Context, In) ValErr[Out]
- type ValErr
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CancelWait ¶ added in v0.9.5
CancelWait executes a function in a new goroutine. It returns a function that cancels the context and blocks until the goroutine is terminated. The caller must call this function.
func Iter ¶ added in v0.8.0
func Iter[In, Out any](ctx context.Context, in iter.Seq[In], workers int, f func(context.Context, In) Out) iter.Seq[Out]
Iter runs a function for each values of an input iterator with concurrent workers. It returns an iterator with the unordered output values. For an ordered version, see IterOrdered.
If the context is canceled, it stops reading values from the input iterator. If the caller stops iterating the output iterator, the context is canceled.
Example ¶
ctx := context.Background() in := slices.Values([]int{1, 2, 3, 4, 5}) out := Iter(ctx, in, 2, func(ctx context.Context, v int) int { return v * 2 }) for v := range out { fmt.Println(v) }
Output: 2 4 6 8 10
func IterOrdered ¶ added in v0.9.0
func IterOrdered[In, Out any](ctx context.Context, in iter.Seq[In], workers int, f func(context.Context, In) Out) iter.Seq[Out]
IterOrdered is like Iter but it keeps the order of the output values.
Example ¶
ctx := context.Background() in := slices.Values([]int{1, 2, 3, 4, 5}) out := IterOrdered(ctx, in, 2, func(ctx context.Context, v int) int { return v * 2 }) for v := range out { fmt.Println(v) }
Output: 2 4 6 8 10
func Map ¶
func Map[MIn ~map[K]In, MOut map[K]Out, K comparable, In, Out any](ctx context.Context, in MIn, workers int, f func(ctx context.Context, k K, v In) Out) MOut
Map is a Iter wrapper for maps.
Example ¶
ctx := context.Background() out := Map(ctx, map[int]int{ 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, }, 2, func(ctx context.Context, k int, v int) int { return v * 2 }) fmt.Println(out)
Output: map[1:2 2:4 3:6 4:8 5:10]
func MapError ¶ added in v0.10.3
func MapError[MIn ~map[K]In, MOut map[K]Out, K comparable, In, Out any](ctx context.Context, in MIn, workers int, f func(ctx context.Context, k K, v In) (Out, error)) (MOut, error)
MapError is a Map wrapper that returns an error.
Example ¶
ctx := context.Background() out, err := MapError(ctx, map[int]int{ 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, }, 2, func(ctx context.Context, k int, v int) (int, error) { if v == 3 { return 0, errors.New("error") } return v * 2, nil }) fmt.Println(out) fmt.Println(err)
Output: map[1:2 2:4 3:0 4:8 5:10] error
func N ¶
N executes a function with multiple goroutines. It blocks until all goroutines are terminated.
func Services ¶ added in v0.8.5
Services runs multiple services in goroutines.
It waits for all services to finish.
If a service returns an error, it cancels the context. All errors are joined and returned.
Example ¶
ctx := context.Background() fmt.Println("start") err := Services(ctx, map[string]func(context.Context) error{ "a": func(ctx context.Context) error { fmt.Println("service A") return nil }, "b": func(ctx context.Context) error { fmt.Println("service B") return nil }, }) if err != nil { panic(err) } fmt.Println("stop")
Output: start service A service B stop
func Slice ¶
func Slice[SIn ~[]In, SOut []Out, In, Out any](ctx context.Context, in SIn, workers int, f func(ctx context.Context, i int, v In) Out) SOut
Slice is a Iter wrapper for slices.
Example ¶
ctx := context.Background() out := Slice(ctx, []int{1, 2, 3, 4, 5}, 2, func(ctx context.Context, i int, v int) int { return v * 2 }) fmt.Println(out)
Output: [2 4 6 8 10]
func SliceError ¶ added in v0.10.3
func SliceError[SIn ~[]In, SOut []Out, In, Out any](ctx context.Context, in SIn, workers int, f func(ctx context.Context, i int, v In) (Out, error)) (SOut, error)
SliceError is a Slice wrapper that returns an error.
Example ¶
ctx := context.Background() out, err := SliceError(ctx, []int{1, 2, 3, 4, 5}, 2, func(ctx context.Context, i int, v int) (int, error) { if v == 3 { return 0, errors.New("error") } return v * 2, nil }) fmt.Println(out) fmt.Println(err)
Output: [2 4 0 8 10] error
func Wait ¶ added in v0.4.0
Wait executes a function in a new goroutine. It returns a function that blocks until the goroutine is terminated. The caller must call this function.
func WaitGroup ¶
WaitGroup executes a function in a new goroutine with a sync.WaitGroup. It calls sync.WaitGroup.Add before starting it, and sync.WaitGroup.Done when the goroutine is terminated.
func WithError ¶ added in v0.8.0
func WithError[In, Out any](f func(context.Context, In) (Out, error)) func(context.Context, In) ValErr[Out]
WithError transforms a function that returns an error into a function that returns a ValErr.
Example ¶
ctx := context.Background() in := slices.Values([]int{1, 2, 3, 4, 5}) out := Iter(ctx, in, 2, WithError(func(ctx context.Context, v int) (int, error) { if v == 3 { return 0, errors.New("error") } return v * 2, nil })) for v := range out { if v.Err != nil { fmt.Println(v.Err) } else { fmt.Println(v.Val) } }
Output: 2 4 error 8 10