Back to godoc.org

Package xsync

v0.0.0-...-3cf7f3f
Latest Go to latest

The latest major version is .

Published: May 27, 2020 | License: GPL3 | Module: lab.nexedi.com/kirr/go123

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.

Index

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.

Package Files

  • xsync.go
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier