group

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2017 License: Apache-2.0 Imports: 0 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package group implements an actor-runner with deterministic teardown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

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

Group collects actors (functions) and runs them concurrently. When one actor (function) returns, the others are interrupted. The zero value of a Group is useful.

func (*Group) Add

func (g *Group) Add(execute func() error, interrupt func(error))

Add an actor (function) to the group. Each actor must be pre-emptable by an interrupt function. That is, if interrupt is invoked, execute should return. Also, it must be safe to call interrupt even after execute has returned.

To add a general processing function, you can use a cancel chan.

cancel := make(chan struct{})
g.Add(func() error {
    select {
    case <-time.After(5 * time.Second):
        return errors.New("time elapsed")
    case <-cancel:
        return errors.New("canceled")
    }
}, func(error) {
    close(cancel)
})

To add an e.g. HTTP server, you'll need to provide an explicit listener, so that it may be interrupted.

ln, _ := net.Listen("tcp", "0.0.0.0:8080")
g.Add(func() error {
    return http.Serve(ln, http.NewServeMux())
}, func(error) {
    ln.Close()
})

func (*Group) Run

func (g *Group) Run() error

Run all actors (functions) concurrently. When the first actor returns, all others are interrupted. Run only returns when all actors (functions) have exited.

Jump to

Keyboard shortcuts

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