pgroup

package
v0.0.0-...-cbe1ced Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2017 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package pgroup lets you manage a heterogeneous 'group' of goroutines as a single unit.

The "app" package uses this for its underlying code, adding a few more features.

Rationale

Group offers these advantages over sync.WaitGroup based goroutine running:

  • Can add goroutines whenever; no race condition on starting (see caveat in https://golang.org/pkg/sync/#WaitGroup.Add )
  • No manual management of counting goroutines launched.
  • Supports registering shutdown handlers to do cleanup and early shutdown scenarios.
  • Captures panics in goroutines without you having to write a bunch of panic handlers.
  • Will run shutdown in a last-in, first-out manner when either a panic/error occurs, or when all goroutines are complete.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {

	// FilterError is called when an error occurs during running a runnable started by Go().
	// By default, it's expected to return the error back but it can be used to suppress errors
	// by returning nil; this will suppress the shutdown process from starting.
	FilterError func(err error) error

	// ErrorHandler is called whenever a function run by Go or a closer returns an error value.
	// It is not called if an error is suppressed by FilterError.
	ErrorHandler func(crash.ErrorInfo)

	// PanicHandler is called whenever a function run by Go, GoF, or a closer panics.
	PanicHandler func(crash.PanicInfo)

	DebugHandler func(string, ...interface{})

	// The amount of time, after which, parallel closer operations may end up running in more goroutines.
	// In other words, if some other instance of Wait is
	StopUnblockTime time.Duration
	// contains filtered or unexported fields
}

Group is a heterogeneous group of goroutines managed together.

func New

func New() *Group

func (*Group) AddCloser

func (g *Group) AddCloser(closer func() error)

Add a closer to do some sort of cleanup. Closers are run last-in first-out.

func (*Group) Go

func (g *Group) Go(runnable func() error) *Task

Go runs func runnable in a goroutine. If the runnable panics, captures the panic and starts the shutdown process. If the runnable returns a non-nil error, then also starts the shutdown process.

The returned task can be used for waiting on; it will have already been Started

func (*Group) GoF

func (g *Group) GoF(f func())

GoF runs func f in a goroutine. This is a handy shortcut to running Go when you have a nullary function.

Exactly equivalent to:

group.Go(func() error {
    f()
    return nil
})

func (*Group) StartN

func (g *Group) StartN(n int, runnable func() error)

Start N copies of the same goroutine.

Basically equivalent to

for i := 0; i < n; i++ { group.Go(runnable ) }

func (*Group) Stop

func (g *Group) Stop()

Stop signals the group to begin stopping. It fires a goroutine to begin running the closers and returns to its caller immediately.

func (*Group) Stopping

func (g *Group) Stopping() bool

Return true if we're in the stop loop (running closers) Needs to acquire a lock, so it is not wise to hit this in a tight loop.

func (*Group) Wait

func (g *Group) Wait()

Wait until all goroutines run with Go complete, and then run all closers in reverse order.

Wait is the key component of the process group. If Wait is not run, then the process group will never get a chance to clean itself up and will leak resources.

It is allowable to run Wait simultaneously in multiple goroutines, and when this is done, all of the Wait will end at some point shortly after all goroutines have completed.

type Task

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

Task is used to collect results of one single invocation.

func (*Task) Err

func (t *Task) Err() (err error)

Err returns the error associated after running this task.

func (*Task) Failed

func (t *Task) Failed() bool

Failed returns true if the task has failed. Will acquire a lock, so do not run this on a tight loop.

func (*Task) Start

func (t *Task) Start()

Start the task. Only call this once!

func (*Task) Wait

func (t *Task) Wait() (err error)

Wait on this task.

Jump to

Keyboard shortcuts

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