xsync

package
Version: v0.0.0-...-c9eb28d Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: GPL-3.0 Imports: 2 Imported by: 7

Documentation

Overview

Package xsync complements standard package sync.

- `WorkGroup` allows to spawn group of goroutines working on a common task.

Functionality provided by xsync package is also provided by Pygolang(*) in its standard package sync.

(*) https://pypi.org/project/pygolang

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WorkGroup

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

WorkGroup represents group of goroutines working on a common task.

Use .Go() to spawn goroutines, and .Wait() to wait for all of them to complete, for example:

wg := xsync.NewWorkGroup(ctx)
wg.Go(f1)
wg.Go(f2)
err := wg.Wait()

Every spawned function accepts context related to the whole work and derived from ctx used to initialize WorkGroup, for example:

func f1(ctx context.Context) error {
    ...
}

Whenever a function returns error, the work context is canceled indicating to other spawned goroutines that they have to cancel their work. .Wait() waits for all spawned goroutines to complete and returns error, if any, from the first failed subtask.

NOTE if spawned function panics, the panic is currently _not_ propagated to .Wait().

WorkGroup is modelled after https://godoc.org/golang.org/x/sync/errgroup but is not equal to it.

func NewWorkGroup

func NewWorkGroup(ctx context.Context) *WorkGroup

NewWorkGroup creates new WorkGroup working under ctx.

See WorkGroup documentation for details.

func (*WorkGroup) Go

func (g *WorkGroup) Go(f func(context.Context) error)

Go spawns new worker under workgroup.

See WorkGroup documentation for details.

func (*WorkGroup) Wait

func (g *WorkGroup) Wait() error

Wait waits for all spawned workers to complete.

It returns the error, if any, from the first failed worker. See WorkGroup documentation for details.

Source Files

Jump to

Keyboard shortcuts

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