taskgroup

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2024 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package taskgroup provides a mix of multierr and errgroup See documentation for https://pkg.go.dev/go.uber.org/multierr and https://pkg.go.dev/golang.org/x/sync/errgroup

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group interface {
	Ctx() context.Context
	Wait() error
	Go(name string, f func(logger *zap.Logger) error)
}

Group manages goroutines and collect all the errors. See https://pkg.go.dev/golang.org/x/sync/errgroup#group for more information

Example
package main

import (
	"errors"
	"fmt"

	"go.uber.org/multierr"
	"go.uber.org/zap"

	"github.com/neondatabase/autoscaling/pkg/util/taskgroup"
)

func main() {
	g := taskgroup.NewGroup(zap.NewNop())
	g.Go("task1", func(_ *zap.Logger) error {
		return errors.New("error 1")
	})
	g.Go("task2", func(_ *zap.Logger) error {
		return errors.New("error 2")
	})
	err := g.Wait()
	// Using golang.org/x/sync/errgroup would return a return error depending on which goroutine was scheduled first

	errs := multierr.Errors(err)
	fmt.Println("Got", len(errs), "errors")
}
Output:

Got 2 errors

func NewGroup

func NewGroup(logger *zap.Logger, opts ...GroupOption) Group

NewGroup returns a new Group.

type GroupOption

type GroupOption func(*group)

func WithPanicHandler

func WithPanicHandler(f func(any)) GroupOption

WithPanicHandler sets a panic handler for the group.

func WithParentContext

func WithParentContext(ctx context.Context) GroupOption

WithParentContext sets the parent context for the group.

Jump to

Keyboard shortcuts

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