(tested) add hooks check when ping http route called

This commit is contained in:
Lawrence, Rendall
2022-05-14 01:36:21 +03:00
parent cf2adad4c9
commit 79c92df0f8
8 changed files with 70 additions and 11 deletions
+14
View File
@@ -18,6 +18,16 @@ type Hook interface {
HandleScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse) (context.Context, error)
}
// Pinger is an optional interface that may be implemented by a pre Hook
// to check if it is operational. Used in frontend.TrackerLogic.
//
// It may be useful in cases when Hook performs foreign requests to
// some external resources (i.e. storage) and `ping` request should
// also check resource availability.
type Pinger interface {
Ping(ctx context.Context) error
}
type skipSwarmInteraction struct{}
// SkipSwarmInteractionKey is a key for the context of an Announce to control
@@ -205,3 +215,7 @@ func (h *responseHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeR
return ctx, nil
}
func (h *responseHook) Ping(_ context.Context) error {
return h.store.Ping()
}
+19 -1
View File
@@ -19,12 +19,19 @@ var (
// NewLogic creates a new instance of a TrackerLogic that executes the provided
// middleware hooks.
func NewLogic(annInterval, minAnnInterval time.Duration, peerStore storage.PeerStorage, preHooks, postHooks []Hook) *Logic {
return &Logic{
l := &Logic{
announceInterval: annInterval,
minAnnounceInterval: minAnnInterval,
preHooks: append(preHooks, &responseHook{store: peerStore}),
postHooks: append(postHooks, &swarmInteractionHook{store: peerStore}),
pingers: make([]Pinger, 0, 1),
}
for _, h := range l.preHooks {
if ph, isOk := h.(Pinger); isOk {
l.pingers = append(l.pingers, ph)
}
}
return l
}
// Logic is an implementation of the TrackerLogic that functions by
@@ -34,6 +41,7 @@ type Logic struct {
minAnnounceInterval time.Duration
preHooks []Hook
postHooks []Hook
pingers []Pinger
}
// HandleAnnounce generates a response for an Announce.
@@ -101,6 +109,16 @@ func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest,
}
}
// Ping performs check if all Hook-s are operational
func (l *Logic) Ping(ctx context.Context) (err error) {
for _, p := range l.pingers {
if err = p.Ping(ctx); err != nil {
break
}
}
return
}
// Stop stops the Logic.
//
// This stops any hooks that implement stop.Stopper.