Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
Server runs synchronously and returns any errors. The Run method is expected to block until finished, returning any error, or until Stop is called. used with oklog/run.Group, where the first Server.Run to return will cancel the Group (regardless of the error returned). Use NewContextServer to create a Server that can block on a Context until Stop is called.
TODO[freeformz]: Document why Stop takes an error and what to do with is.
func MultiServer ¶
MultiServer which, when Run, will run all of the servers until one of them returns or is itself Stopped.
Example ¶
s := MultiServer( ServerFunc( func() error { fmt.Println("A") return nil }), ) if err := s.Run(); err != nil { panic(err) }
Output: A
Example (Stop) ¶
done := make(chan struct{}) s := MultiServer( ServerFuncs{ RunFunc: func() error { fmt.Println("A") <-done fmt.Println("B") return nil }, StopFunc: func(err error) { fmt.Println(err) close(done) }, }, ) go func() { time.Sleep(1 * time.Second) s.Stop(io.EOF) }() if err := s.Run(); err != nil && err != context.Canceled { panic(err) }
Output: A context canceled B
func NewContextServer ¶
NewContextServer that when Run(), calls the given function with a context that is canceled when Stop() is called.
Example ¶
s := NewContextServer( func(ctx context.Context) error { // do something that doesn't block ex: fmt.Println("A") <-ctx.Done() // block waiting for context to be canceled // can do any cleanup after this, or just return nil // return cleanup() return nil }, ) var exitFast Server = ServerFunc( // doesn't do anything, just returns func() error { return nil }, ) var g run.Group g.Add(s.Run, s.Stop) g.Add(exitFast.Run, exitFast.Stop) if err := g.Run(); err != nil { panic(err) }
Output: A
type ServerFunc ¶
type ServerFunc func() error
ServerFunc adapts a function to the Server interface. This is useful to adapt a closure to being a Server.
Example ¶
var a Server = ServerFunc( func() error { fmt.Println("A") return nil }, ) var g run.Group g.Add(a.Run, a.Stop) if err := g.Run(); err != nil { panic(err) }
Output: A
func (ServerFunc) Stop ¶
func (fn ServerFunc) Stop(error)
Stop is a noop for gradual compatibility with oklog run.Group.
type ServerFuncs ¶
ServerFuncs adapts two functions, one for Run, one for Stop, to the Server interface. This is useful for adapting closures so they can be used as a Server.
Example ¶
var a Server = ServerFuncs{ RunFunc: func() error { fmt.Println("A") return nil }, StopFunc: func(err error) { }, } var g run.Group g.Add(a.Run, a.Stop) if err := g.Run(); err != nil { panic(err) }
Output: A