stop

package
v0.0.0-...-f7fe051 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2014 License: MIT Imports: 1 Imported by: 10

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

NoWait represents a time.Duration with zero value. Logically meaning no grace wait period when stopping.

Functions

func All

func All(wait time.Duration, stoppers ...Stopper) <-chan Signal

All stops all Stopper types and returns another channel which will close once all things have finished stopping.

func Make

func Make() chan Signal

Make makes a new channel used to indicate when stopping has finished. Sends to channel will not block.

func Stopped

func Stopped() <-chan Signal

Stopped returns a channel that signals immediately. Useful for cases when no tear-down work is required and stopping is immediate.

Types

type Signal

type Signal struct{}

Signal is the type that gets sent down the stop channel.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL