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 ¶
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()
})