goro

package
v1.14.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package goro provides utilities for spawning and subsequently managing the liftime(s) of one or more goroutines. This package relies heavily on the context package to provide consistent cancellation semantics for long-lived goroutines. The goal of this package is to provide a unified way to cancel and wait on running goroutines as is often seen in "service" or "daemon" types with Start()/Stop() lifecycle functions and to unify the multiplicity of approaches that have been adopted over time.

Note: If you're looking for a short-lived (e.g.  request-scoped) group of
transient goroutines, you probably want `errgroup.Group` from
https://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 added in v1.13.0

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

Group manages a set of long-running goroutines. Goroutines are spawned individually with Group.Go and after that interrupted and waited-on as a single unit. The expected use-case for this type is as a member field of a `common.Daemon` (or similar) type that spawns one or more goroutines in its Start() function and then stops those same goroutines in its Stop() function. The zero-value of this type is valid. A Group must not be copied after first use.

Example
Output:

starting backgroundLoop1
starting backgroundLoop2
stopping backgroundLoop1
stopping backgroundLoop2

func (*Group) Cancel added in v1.13.0

func (g *Group) Cancel()

Cancel cancels the `context.Context` that was passed to all goroutines spawned via `Go` on this `Group`.

func (*Group) Go added in v1.13.0

func (g *Group) Go(f func(ctx context.Context) error)

Go spawns a goroutine whose lifecycle will be managed via this Group object. All functions passed to Go on the same instance of Group will be given the same `context.Context` object, which will be used to indicate cancellation. If the supplied func does not abide by `ctx.Done()` of the provided `context.Context` then `Wait()` on this `Group` will hang until all functions exit on their own (possibly never).

func (*Group) Wait added in v1.13.0

func (g *Group) Wait()

Wait blocks waiting for all goroutines spawned via `Go` on this `Group` instance to complete. If `Go` has not been called then this function returns immediately.

type Handle

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

Handle is a threadsafe and multi-stop safe handle to a single running goroutine.

Example
Output:

<nil>
shutting down
context canceled

func Go

func Go(ctx context.Context, f func(context.Context) error) *Handle

Go launches the supplied function in its own goroutine and returns back a *Handle that serves as a handle to the goroutine itself.

func (*Handle) Cancel

func (h *Handle) Cancel()

Cancel requests that this goroutine stop by cancelling the associated context object. This function is threadsafe and idempotent. Note that this function _requests_ termination, it does not forcefully kill the goroutine.

func (*Handle) Done

func (h *Handle) Done() <-chan struct{}

Done exposes a channel that allows outside goroutines to block on this goroutine's completion. Whatever time passes between a call to Cancel() and the Done() channel closing is the time taken by the goroutine to shut itself down.

func (*Handle) Err

func (h *Handle) Err() error

Error observes the error returned by the func passed to Go (if any). There is never any error (i.e. this function returns nil) while the goroutine is running.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳