commit
51926ad562
4 changed files with 34 additions and 34 deletions
|
@ -23,7 +23,7 @@ import (
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/bittorrent"
|
"github.com/chihaya/chihaya/bittorrent"
|
||||||
"github.com/chihaya/chihaya/middleware"
|
"github.com/chihaya/chihaya/middleware"
|
||||||
"github.com/chihaya/chihaya/pkg/stopper"
|
"github.com/chihaya/chihaya/pkg/stop"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -114,7 +114,7 @@ func (h *hook) Stop() <-chan error {
|
||||||
log.Debug("attempting to shutdown JWT middleware")
|
log.Debug("attempting to shutdown JWT middleware")
|
||||||
select {
|
select {
|
||||||
case <-h.closing:
|
case <-h.closing:
|
||||||
return stopper.AlreadyStopped
|
return stop.AlreadyStopped
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
c := make(chan error)
|
c := make(chan error)
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/chihaya/chihaya/bittorrent"
|
"github.com/chihaya/chihaya/bittorrent"
|
||||||
"github.com/chihaya/chihaya/frontend"
|
"github.com/chihaya/chihaya/frontend"
|
||||||
"github.com/chihaya/chihaya/pkg/stopper"
|
"github.com/chihaya/chihaya/pkg/stop"
|
||||||
"github.com/chihaya/chihaya/storage"
|
"github.com/chihaya/chihaya/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -104,18 +104,18 @@ func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest,
|
||||||
|
|
||||||
// Stop stops the Logic.
|
// 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 {
|
func (l *Logic) Stop() []error {
|
||||||
stopGroup := stopper.NewStopGroup()
|
stopGroup := stop.NewGroup()
|
||||||
for _, hook := range l.preHooks {
|
for _, hook := range l.preHooks {
|
||||||
stoppable, ok := hook.(stopper.Stopper)
|
stoppable, ok := hook.(stop.Stopper)
|
||||||
if ok {
|
if ok {
|
||||||
stopGroup.Add(stoppable)
|
stopGroup.Add(stoppable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, hook := range l.postHooks {
|
for _, hook := range l.postHooks {
|
||||||
stoppable, ok := hook.(stopper.Stopper)
|
stoppable, ok := hook.(stop.Stopper)
|
||||||
if ok {
|
if ok {
|
||||||
stopGroup.Add(stoppable)
|
stopGroup.Add(stoppable)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// Package stopper implements a pattern for shutting down a group of processes.
|
// Package stop implements a pattern for shutting down a group of processes.
|
||||||
package stopper
|
package stop
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -22,52 +22,52 @@ func init() {
|
||||||
type Stopper interface {
|
type Stopper interface {
|
||||||
// Stop returns a channel that indicates whether the stop was
|
// Stop returns a channel that indicates whether the stop was
|
||||||
// successful.
|
// successful.
|
||||||
// The channel can either return one error or be closed. Closing the
|
//
|
||||||
// channel signals a clean shutdown.
|
// The channel can either return one error or be closed.
|
||||||
// The Stop function should return immediately and perform the actual
|
// Closing the channel signals a clean shutdown.
|
||||||
// shutdown in a separate goroutine.
|
// Stop() should return immediately and perform the actual shutdown in a
|
||||||
|
// separate goroutine.
|
||||||
Stop() <-chan error
|
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.
|
// Func is a function that can be used to provide a clean shutdown.
|
||||||
type Func func() <-chan error
|
type Func func() <-chan error
|
||||||
|
|
||||||
// NewStopGroup creates a new StopGroup.
|
// Group is a collection of Stoppers that can be stopped all at once.
|
||||||
func NewStopGroup() *StopGroup {
|
type Group struct {
|
||||||
return &StopGroup{
|
stoppables []Func
|
||||||
|
sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGroup allocates a new Group.
|
||||||
|
func NewGroup() *Group {
|
||||||
|
return &Group{
|
||||||
stoppables: make([]Func, 0),
|
stoppables: make([]Func, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add adds a Stopper to the StopGroup.
|
// Add appends a Stopper to the Group.
|
||||||
// On the next call to Stop(), the Stopper will be stopped.
|
func (cg *Group) Add(toAdd Stopper) {
|
||||||
func (cg *StopGroup) Add(toAdd Stopper) {
|
|
||||||
cg.Lock()
|
cg.Lock()
|
||||||
defer cg.Unlock()
|
defer cg.Unlock()
|
||||||
|
|
||||||
cg.stoppables = append(cg.stoppables, toAdd.Stop)
|
cg.stoppables = append(cg.stoppables, toAdd.Stop)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddFunc adds a Func to the StopGroup.
|
// AddFunc appends a Func to the Group.
|
||||||
// On the next call to Stop(), the Func will be called.
|
func (cg *Group) AddFunc(toAddFunc Func) {
|
||||||
func (cg *StopGroup) AddFunc(toAddFunc Func) {
|
|
||||||
cg.Lock()
|
cg.Lock()
|
||||||
defer cg.Unlock()
|
defer cg.Unlock()
|
||||||
|
|
||||||
cg.stoppables = append(cg.stoppables, toAddFunc)
|
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.
|
// Stopping will be done in a concurrent fashion.
|
||||||
// The slice of errors returned contains all errors returned by stopping the
|
// The slice of errors returned contains all errors returned by stopping the
|
||||||
// members.
|
// members.
|
||||||
func (cg *StopGroup) Stop() []error {
|
func (cg *Group) Stop() []error {
|
||||||
cg.Lock()
|
cg.Lock()
|
||||||
defer cg.Unlock()
|
defer cg.Unlock()
|
||||||
|
|
|
@ -2,7 +2,7 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/chihaya/chihaya/bittorrent"
|
"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
|
// ErrResourceDoesNotExist is the error returned by all delete methods in the
|
||||||
|
@ -65,8 +65,8 @@ type PeerStore interface {
|
||||||
// returned.
|
// returned.
|
||||||
ScrapeSwarm(infoHash bittorrent.InfoHash, addressFamily bittorrent.AddressFamily) bittorrent.Scrape
|
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.
|
// PeerStore.
|
||||||
// For more details see the documentation in the stopper package.
|
// For more details see the documentation in the stop package.
|
||||||
stopper.Stopper
|
stop.Stopper
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue