mv pkg/stopper pkg/stop

This makes most of the callsites stutter slightly less.
This commit is contained in:
Jimmy Zelinskie 2017-02-02 21:09:25 -05:00
parent 4d54980930
commit a4b08c021b
4 changed files with 34 additions and 34 deletions

View file

@ -23,7 +23,7 @@ import (
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/middleware"
"github.com/chihaya/chihaya/pkg/stopper"
"github.com/chihaya/chihaya/pkg/stop"
)
var (
@ -114,7 +114,7 @@ func (h *hook) Stop() <-chan error {
log.Debug("attempting to shutdown JWT middleware")
select {
case <-h.closing:
return stopper.AlreadyStopped
return stop.AlreadyStopped
default:
}
c := make(chan error)

View file

@ -9,7 +9,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/frontend"
"github.com/chihaya/chihaya/pkg/stopper"
"github.com/chihaya/chihaya/pkg/stop"
"github.com/chihaya/chihaya/storage"
)
@ -104,18 +104,18 @@ func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest,
// Stop stops the Logic.
//
// This stops any hooks that implement stopper.Stopper.
// This stops any hooks that implement stop.stop.
func (l *Logic) Stop() []error {
stopGroup := stopper.NewStopGroup()
stopGroup := stop.NewGroup()
for _, hook := range l.preHooks {
stoppable, ok := hook.(stopper.Stopper)
stoppable, ok := hook.(stop.Stopper)
if ok {
stopGroup.Add(stoppable)
}
}
for _, hook := range l.postHooks {
stoppable, ok := hook.(stopper.Stopper)
stoppable, ok := hook.(stop.Stopper)
if ok {
stopGroup.Add(stoppable)
}

View file

@ -1,5 +1,5 @@
// Package stopper implements a pattern for shutting down a group of processes.
package stopper
// Package stop implements a pattern for shutting down a group of processes.
package stop
import (
"sync"
@ -22,52 +22,52 @@ func init() {
type Stopper interface {
// Stop returns a channel that indicates whether the stop was
// successful.
// The channel can either return one error or be closed. Closing the
// channel signals a clean shutdown.
// The Stop function should return immediately and perform the actual
// shutdown in a separate goroutine.
//
// The channel can either return one error or be closed.
// Closing the channel signals a clean shutdown.
// Stop() should return immediately and perform the actual shutdown in a
// separate goroutine.
Stop() <-chan error
}
// StopGroup is a group that can be stopped.
type StopGroup struct {
stoppables []Func
sync.Mutex
}
// Func is a function that can be used to provide a clean shutdown.
type Func func() <-chan error
// NewStopGroup creates a new StopGroup.
func NewStopGroup() *StopGroup {
return &StopGroup{
// Group is a collection of Stoppers that can be stopped all at once.
type Group struct {
stoppables []Func
sync.Mutex
}
// NewGroup allocates a new Group.
func NewGroup() *Group {
return &Group{
stoppables: make([]Func, 0),
}
}
// Add adds a Stopper to the StopGroup.
// On the next call to Stop(), the Stopper will be stopped.
func (cg *StopGroup) Add(toAdd Stopper) {
// Add appends a Stopper to the Group.
func (cg *Group) Add(toAdd Stopper) {
cg.Lock()
defer cg.Unlock()
cg.stoppables = append(cg.stoppables, toAdd.Stop)
}
// AddFunc adds a Func to the StopGroup.
// On the next call to Stop(), the Func will be called.
func (cg *StopGroup) AddFunc(toAddFunc Func) {
// AddFunc appends a Func to the Group.
func (cg *Group) AddFunc(toAddFunc Func) {
cg.Lock()
defer cg.Unlock()
cg.stoppables = append(cg.stoppables, toAddFunc)
}
// Stop stops all members of the StopGroup.
// Stop stops all members of the Group.
//
// Stopping will be done in a concurrent fashion.
// The slice of errors returned contains all errors returned by stopping the
// members.
func (cg *StopGroup) Stop() []error {
func (cg *Group) Stop() []error {
cg.Lock()
defer cg.Unlock()

View file

@ -2,7 +2,7 @@ package storage
import (
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/pkg/stopper"
"github.com/chihaya/chihaya/pkg/stop"
)
// ErrResourceDoesNotExist is the error returned by all delete methods in the
@ -65,8 +65,8 @@ type PeerStore interface {
// returned.
ScrapeSwarm(infoHash bittorrent.InfoHash, addressFamily bittorrent.AddressFamily) bittorrent.Scrape
// Stopper is an interface that expects a Stop method to stop the
// stop is an interface that expects a Stop method to stop the
// PeerStore.
// For more details see the documentation in the stopper package.
stopper.Stopper
// For more details see the documentation in the stop package.
stop.Stopper
}