cmd/chihaya: stop hooks

Fixes #214.
This commit is contained in:
Leo Balduf 2016-09-24 13:38:05 -04:00
parent c1b7ba4a52
commit 86ebb108fc
3 changed files with 41 additions and 3 deletions

View file

@ -119,7 +119,11 @@ func rootCmdRun(cmd *cobra.Command, args []string) error {
} }
} }
// TODO(jzelinskie): stop hooks here // Stop hooks.
errs := logic.Stop()
for _, err := range errs {
errChan <- err
}
close(errChan) close(errChan)
}() }()

View file

@ -23,6 +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/stopper"
) )
var ( var (
@ -94,8 +95,18 @@ func NewHook(cfg Config) middleware.Hook {
return h return h
} }
func (h *hook) Stop() { func (h *hook) Stop() <-chan error {
select {
case <-h.closing:
return stopper.AlreadyStopped
default:
}
c := make(chan error)
go func() {
close(h.closing) close(h.closing)
close(c)
}()
return c
} }
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) { func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {

View file

@ -9,6 +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/stopper"
"github.com/chihaya/chihaya/storage" "github.com/chihaya/chihaya/storage"
) )
@ -94,3 +95,25 @@ func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest,
} }
} }
} }
// Stop stops the Logic.
//
// This stops any hooks that implement stopper.Stopper.
func (l *Logic) Stop() []error {
stopGroup := stopper.NewStopGroup()
for _, hook := range l.preHooks {
stoppable, ok := hook.(stopper.Stopper)
if ok {
stopGroup.Add(stoppable)
}
}
for _, hook := range l.postHooks {
stoppable, ok := hook.(stopper.Stopper)
if ok {
stopGroup.Add(stoppable)
}
}
return stopGroup.Stop()
}