worker

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: Apache-2.0 Imports: 3 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parallel added in v0.1.1

func Parallel(ctx context.Context, workers ...Worker) error

Parallel executes a set of Workers and waits for them to finish.

Types

type Worker

type Worker func(context.Context) error

Worker is a function that WorkerGroup will run in a new goroutine.

type WorkerGroup

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

WorkerGroup is similar in principle to sync.WaitGroup but manages the Workers itself. This allows it to provide a few helpful features:

  • Integration with the context library.
  • Limit the number of concurrent Workers.
  • Capture the errors returned by each Worker.
  • Abort everything after a single Worker reports an error.

func NewWorkerGroup

func NewWorkerGroup(ctx context.Context, workerLimit int) *WorkerGroup

NewWorkerGroup creates a new group.

func (*WorkerGroup) Start

func (wg *WorkerGroup) Start(worker Worker) error

Start launches a new worker, blocking if too many workers are already running. An error indicates the group's context is closed.

func (*WorkerGroup) Wait

func (wg *WorkerGroup) Wait() error

Wait blocks until all running workers have finished. An error indicates if at least one worker returned an error or was canceled.

func (*WorkerGroup) WaitError

func (wg *WorkerGroup) WaitError(err error) error

Wait with a default error value that will be returned if no worker failed.

if err := wg.Start(worker); err != nil {
	return wg.WaitError(err)
}

Jump to

Keyboard shortcuts

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