group

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2017 License: Apache-2.0 Imports: 0 Imported by: 479

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