Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPrematurelyClosed notifies that Close() method // was called before Open() ErrPrematurelyClosed = errors.New("prematurely closed") )
var ( // ErrTimeout returned by Timeout if timeout exceeded ErrTimeout = errors.New("timeout exceeded") )
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain supervises Components in order. All supervised components are open in FIFO order and closed in LIFO order.
Chain collects and returns error from corresponding Component methods. If more than one Components returns errors they will be wrapped in errslice.Error.
func NewChain ¶
NewChain creates new Chain. Provided context manages whole Chain. Close Context is equivalent to call Chain.Close().
func (Chain) Close ¶
func (c Chain) Close() (err error)
Close initialises shutdown for all Components. This method may be called many times and will return equal results. It's guaranteed that Close() method of all components will be called only once.
func (Chain) Open ¶
func (c Chain) Open() (err error)
Open blocks until all components are opened. This method should be called before Close(). Otherwise Open() will return error. If Open() method of one of components returns error all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Open() method of all components will be called only once.
func (Chain) Wait ¶
func (c Chain) Wait() (err error)
Wait blocks until all components are exited. If one of Wait() method of one of Components is exited before Close() all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Wait() method of all components will be called only once.
type Component ¶
type Component interface { // Open runs Component initialisation and should blocks until // Component is initialised. Open() (err error) // Close initialises Component shutdown. Close() (err error) // Wait should blocks until Component shutdown. Wait() (err error) }
Component is basic building block to build supervisor trees
type Control ¶
type Control struct {
// contains filtered or unexported fields
}
Control is simplest embeddable component
Example (Layered) ¶
package main import ( "context" "fmt" "github.com/akaspin/supervisor" ) // LayeredControl uses two controls to fully control component lifecycle. // Use this pattern only if you need complex logic because all supervisor // primitives guarantees that Open(), Close() and Wait() methods of supervised // components will be called only once. type LayeredControl struct { *supervisor.Control // managed externally doneControl *supervisor.Control } func NewLayeredControl(ctx context.Context) (c *LayeredControl) { c = &LayeredControl{ Control: supervisor.NewControl(ctx), doneControl: supervisor.NewControl(context.Background()), } go func() { <-c.Ctx().Done() fmt.Println("shutting down") c.doneControl.Close() }() return c } func (c *LayeredControl) Open() (err error) { if c.Control.IsOpen() { return nil } fmt.Println("opening") c.Control.Open() c.doneControl.Open() return nil } func (c *LayeredControl) Close() (err error) { if c.Control.IsClosed() { return nil } fmt.Println("closing") return c.Control.Close() } func (c *LayeredControl) Wait() (err error) { if c.doneControl.IsClosed() { return nil } c.doneControl.Wait() fmt.Println("exited") return } func main() { c := NewLayeredControl(context.Background()) c.Open() c.Close() c.Wait() }
Output: opening closing shutting down exited
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group supervises Components in parallel. All supervised components are open and closed concurrently.
Group collects and returns error from corresponding Component methods. If more than one Components returns errors they will be wrapped in errslice.Error.
func NewGroup ¶
NewGroup creates new Group. Provided context manages whole Group. Close Context is equivalent to call Group.Close().
func (Group) Close ¶
func (c Group) Close() (err error)
Close initialises shutdown for all Components. This method may be called many times and will return equal results. It's guaranteed that Close() method of all components will be called only once.
func (Group) Open ¶
func (c Group) Open() (err error)
Open blocks until all components are opened. This method should be called before Close(). Otherwise Open() will return error. If Open() method of one of components returns error all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Open() method of all components will be called only once.
func (Group) Wait ¶
func (c Group) Wait() (err error)
Wait blocks until all components are exited. If one of Wait() method of one of Components is exited before Close() all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Wait() method of all components will be called only once.
type Timeout ¶
type Timeout struct {
// contains filtered or unexported fields
}
Timeout supervises shutdown process of own descendant
func NewTimeout ¶
NewTimeout creates new Timeout
type Trap ¶
type Trap struct {
// contains filtered or unexported fields
}
Trap can be used as watchdog in supervisor tree.
func (*Trap) Wait ¶
Wait returns last accepted error
Example ¶
package main import ( "context" "errors" "fmt" "github.com/akaspin/supervisor" ) func main() { trap := supervisor.NewTrap(context.Background()) trap.Open() go func() { if err := trap.Wait(); err != nil && err.Error() != "bang" { fmt.Println(trap.Wait()) } }() trap.Trap(errors.New("bang")) fmt.Println(trap.Wait()) }
Output: bang