Documentation
¶
Overview ¶
Package stop represents a pattern for types that need to do some work when stopping. The StopChan method returns a <-chan stop.Signal which is closed when the operation has completed.
Stopper types when implementing the stop channel pattern should use stop.Make to create and store a stop channel, and close the channel once stopping has completed:
func New() Type { t := new(Type) t.stopChan = stop.Make() return t } func (t Type) Stop() { go func(){ // TODO: tear stuff down close(t.stopChan) }() } func (t Type) StopChan() <-chan stop.Signal { return t.stopChan }
Stopper types can be stopped in the following ways:
// stop and forget t.Stop(1 * time.Second) // stop and wait t.Stop(1 * time.Second) <-t.StopChan() // stop, do more work, then wait t.Stop(1 * time.Second); // do more work <-t.StopChan() // stop and timeout after 1 second t.Stop(1 * time.Second) select { case <-t.StopChan(): case <-time.After(1 * time.Second): } // stop.All is the same as calling Stop() then StopChan() so // all above patterns also work on many Stopper types, // for example; stop and wait for many things: <-stop.All(1 * time.Second, t1, t2, t3)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NoWait time.Duration
NoWait represents a time.Duration with zero value. Logically meaning no grace wait period when stopping.
Functions ¶
func All ¶
All stops all Stopper types and returns another channel which will close once all things have finished stopping.
Types ¶
type Stopper ¶
type Stopper interface { // Stop instructs the type to halt operations and close // the stop channel when it is finished. Stop(wait time.Duration) // StopChan gets the stop channel which will block until // stopping has completed, at which point it is closed. // Callers should never close the stop channel. // The StopChan should exist from the point at which operations // begun, not the point at which Stop was called. StopChan() <-chan Signal }
Stopper represents types that implement the stop channel pattern.