diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index 721fde2..fa0ee41 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -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) diff --git a/middleware/middleware.go b/middleware/middleware.go index eb47524..6a15851 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -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) } diff --git a/pkg/stopper/stopper.go b/pkg/stop/stop.go similarity index 64% rename from pkg/stopper/stopper.go rename to pkg/stop/stop.go index ed302d7..9703965 100644 --- a/pkg/stopper/stopper.go +++ b/pkg/stop/stop.go @@ -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() diff --git a/storage/storage.go b/storage/storage.go index 78b31cc..1d3ed4c 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -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 }