Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChannelStopper ¶
type ChannelStopper struct {
// contains filtered or unexported fields
}
ChannelStopper is an implementation of Stopper using channels.
Example ¶
package main import ( "fmt" "github.com/upsight/stop" ) func main() { s := stop.NewChannelStopper() go func() { <-s.StopChannel() fmt.Println("Stopper is stopping") s.Stopped() }() s.Stop() s.WaitForStopped() fmt.Println("Stopper has stopped") }
Output: Stopper is stopping Stopper has stopped
func NewChannelStopper ¶
func NewChannelStopper() *ChannelStopper
NewChannelStopper returns a new ChannelStopper.
func (*ChannelStopper) IsStopped ¶
func (s *ChannelStopper) IsStopped() bool
IsStopped returns true if Stopped has been called.
func (*ChannelStopper) IsStopping ¶
func (s *ChannelStopper) IsStopping() bool
IsStopping returns true if Stop has been called.
func (*ChannelStopper) Stop ¶
func (s *ChannelStopper) Stop()
Stop notifies that work should stop by closing the stop channel.
func (*ChannelStopper) StopChannel ¶
func (s *ChannelStopper) StopChannel() chan bool
StopChannel returns a channel that will be closed when Stop is called.
func (*ChannelStopper) Stopped ¶
func (s *ChannelStopper) Stopped()
Stopped notifies that work has stopped by closing the stopped channel.
func (*ChannelStopper) StoppedChannel ¶
func (s *ChannelStopper) StoppedChannel() chan bool
StoppedChannel returns a channel that will be closed when Stopped is called.
func (*ChannelStopper) WaitForStopped ¶
func (s *ChannelStopper) WaitForStopped()
WaitForStopped blocks until Stopped is called.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group behaves like a WaitGroup, but it also coordinates shutting down the things attached to it when the Group is stopped.
Example ¶
package main import ( "fmt" "time" "github.com/upsight/stop" ) func main() { type Service struct { *stop.ChannelStopper t *time.Timer } runService := func(s *Service, sg *stop.Group, n int) { defer s.Stopped() select { case <-s.StopChannel(): fmt.Println("Service", n, "stopping") case <-s.t.C: fmt.Println("Service", n, "timer expired") sg.Stop() } } // Add two services to a stop group. The timer for the first // service will finish first. It will stop the group, which will // cause the stop for the other service to be called. sg := stop.NewGroup() s1 := &Service{ ChannelStopper: stop.NewChannelStopper(), t: time.NewTimer(1 * time.Second), } sg.Add(s1) go runService(s1, sg, 1) s2 := &Service{ ChannelStopper: stop.NewChannelStopper(), t: time.NewTimer(2 * time.Second), } sg.Add(s2) go runService(s2, sg, 2) // Wait for all the services to finish stopping. sg.Wait() fmt.Println("Services have stopped") }
Output: Service 1 timer expired Service 2 stopping Services have stopped
func (*Group) Add ¶
Add adds a Stopper to the stop group. The stop group will call Stop on the stopper when the group is stopped. The group's Wait method will block until WaitForStopped returns for all attached stoppers.
func (*Group) IsStopping ¶
IsStopping returns true if Stop has been called.
func (*Group) Stop ¶
func (s *Group) Stop()
Stop notifies the Stopped channel that attached stoppers should stop. If already stopped, this is a no-op.
func (*Group) StopChannel ¶
StopChannel returns a channel that will be closed when Stop is called.
func (*Group) StopOnSignal ¶
StopOnSignal will call stop the group when the given os signals are received. If no signals are passed, it will trigger for any signal.
type Stopper ¶
type Stopper interface { IsStopping() bool IsStopped() bool Stop() StopChannel() chan bool Stopped() StoppedChannel() chan bool WaitForStopped() }
Stopper is an interface for objects that can be used with Group. See ChannelStopper for a simple implementation using channels.
IsStopping should return true if Stop has been called.
IsStopped should return a true if Stopped has been called.
Stop should be a non-blocking notification to stop any work in progress. It should be safe to call this multiple times.
Stopped should be a non-blocking notification that work has been stopped. It should be safe to call this multiple times.
WaitForStopped should block until the work has been stopped.