frontend: TrackerLogic interface returns modified context
HandleAnnounce and HandleScrape must return the modified context changed by the hooks. These contexts are passed to AfterAnnounce and AfterScrape for further use. Closes #304
This commit is contained in:
parent
abccf5bd7e
commit
f7becf952b
4 changed files with 24 additions and 16 deletions
|
@ -11,14 +11,20 @@ import (
|
||||||
// after the response has been delivered to the client.
|
// after the response has been delivered to the client.
|
||||||
type TrackerLogic interface {
|
type TrackerLogic interface {
|
||||||
// HandleAnnounce generates a response for an Announce.
|
// HandleAnnounce generates a response for an Announce.
|
||||||
HandleAnnounce(context.Context, *bittorrent.AnnounceRequest) (*bittorrent.AnnounceResponse, error)
|
//
|
||||||
|
// Returns the updated context, the generated AnnounceResponse and no error
|
||||||
|
// on success; nil and error on failure.
|
||||||
|
HandleAnnounce(context.Context, *bittorrent.AnnounceRequest) (context.Context, *bittorrent.AnnounceResponse, error)
|
||||||
|
|
||||||
// AfterAnnounce does something with the results of an Announce after it
|
// AfterAnnounce does something with the results of an Announce after it
|
||||||
// has been completed.
|
// has been completed.
|
||||||
AfterAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse)
|
AfterAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse)
|
||||||
|
|
||||||
// HandleScrape generates a response for a Scrape.
|
// HandleScrape generates a response for a Scrape.
|
||||||
HandleScrape(context.Context, *bittorrent.ScrapeRequest) (*bittorrent.ScrapeResponse, error)
|
//
|
||||||
|
// Returns the updated context, the generated AnnounceResponse and no error
|
||||||
|
// on success; nil and error on failure.
|
||||||
|
HandleScrape(context.Context, *bittorrent.ScrapeRequest) (context.Context, *bittorrent.ScrapeResponse, error)
|
||||||
|
|
||||||
// AfterScrape does something with the results of a Scrape after it has been completed.
|
// AfterScrape does something with the results of a Scrape after it has been completed.
|
||||||
AfterScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse)
|
AfterScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse)
|
||||||
|
|
|
@ -191,7 +191,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httpr
|
||||||
af = new(bittorrent.AddressFamily)
|
af = new(bittorrent.AddressFamily)
|
||||||
*af = req.IP.AddressFamily
|
*af = req.IP.AddressFamily
|
||||||
|
|
||||||
resp, err := f.logic.HandleAnnounce(context.Background(), req)
|
ctx, resp, err := f.logic.HandleAnnounce(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteError(w, err)
|
WriteError(w, err)
|
||||||
return
|
return
|
||||||
|
@ -203,7 +203,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httpr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go f.logic.AfterAnnounce(context.Background(), req, resp)
|
go f.logic.AfterAnnounce(ctx, req, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// scrapeRoute parses and responds to a Scrape.
|
// scrapeRoute parses and responds to a Scrape.
|
||||||
|
@ -248,7 +248,7 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
|
||||||
af = new(bittorrent.AddressFamily)
|
af = new(bittorrent.AddressFamily)
|
||||||
*af = req.AddressFamily
|
*af = req.AddressFamily
|
||||||
|
|
||||||
resp, err := f.logic.HandleScrape(context.Background(), req)
|
ctx, resp, err := f.logic.HandleScrape(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteError(w, err)
|
WriteError(w, err)
|
||||||
return
|
return
|
||||||
|
@ -260,5 +260,5 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go f.logic.AfterScrape(context.Background(), req, resp)
|
go f.logic.AfterScrape(ctx, req, resp)
|
||||||
}
|
}
|
||||||
|
|
|
@ -286,8 +286,9 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||||
af = new(bittorrent.AddressFamily)
|
af = new(bittorrent.AddressFamily)
|
||||||
*af = req.IP.AddressFamily
|
*af = req.IP.AddressFamily
|
||||||
|
|
||||||
|
var ctx context.Context
|
||||||
var resp *bittorrent.AnnounceResponse
|
var resp *bittorrent.AnnounceResponse
|
||||||
resp, err = t.logic.HandleAnnounce(context.Background(), req)
|
ctx, resp, err = t.logic.HandleAnnounce(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteError(w, txID, err)
|
WriteError(w, txID, err)
|
||||||
return
|
return
|
||||||
|
@ -295,7 +296,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||||
|
|
||||||
WriteAnnounce(w, txID, resp, actionID == announceV6ActionID)
|
WriteAnnounce(w, txID, resp, actionID == announceV6ActionID)
|
||||||
|
|
||||||
go t.logic.AfterAnnounce(context.Background(), req, resp)
|
go t.logic.AfterAnnounce(ctx, req, resp)
|
||||||
|
|
||||||
case scrapeActionID:
|
case scrapeActionID:
|
||||||
actionName = "scrape"
|
actionName = "scrape"
|
||||||
|
@ -319,8 +320,9 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||||
af = new(bittorrent.AddressFamily)
|
af = new(bittorrent.AddressFamily)
|
||||||
*af = req.AddressFamily
|
*af = req.AddressFamily
|
||||||
|
|
||||||
|
var ctx context.Context
|
||||||
var resp *bittorrent.ScrapeResponse
|
var resp *bittorrent.ScrapeResponse
|
||||||
resp, err = t.logic.HandleScrape(context.Background(), req)
|
ctx, resp, err = t.logic.HandleScrape(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteError(w, txID, err)
|
WriteError(w, txID, err)
|
||||||
return
|
return
|
||||||
|
@ -328,7 +330,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||||
|
|
||||||
WriteScrape(w, txID, resp)
|
WriteScrape(w, txID, resp)
|
||||||
|
|
||||||
go t.logic.AfterScrape(context.Background(), req, resp)
|
go t.logic.AfterScrape(ctx, req, resp)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
err = errUnknownAction
|
err = errUnknownAction
|
||||||
|
|
|
@ -49,7 +49,7 @@ type Logic struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleAnnounce generates a response for an Announce.
|
// HandleAnnounce generates a response for an Announce.
|
||||||
func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (resp *bittorrent.AnnounceResponse, err error) {
|
func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (_ context.Context, resp *bittorrent.AnnounceResponse, err error) {
|
||||||
resp = &bittorrent.AnnounceResponse{
|
resp = &bittorrent.AnnounceResponse{
|
||||||
Interval: l.announceInterval,
|
Interval: l.announceInterval,
|
||||||
MinInterval: l.announceInterval,
|
MinInterval: l.announceInterval,
|
||||||
|
@ -57,12 +57,12 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
|
||||||
}
|
}
|
||||||
for _, h := range l.preHooks {
|
for _, h := range l.preHooks {
|
||||||
if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
|
if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(resp.LogFields()).Debug("generated announce response")
|
log.WithFields(resp.LogFields()).Debug("generated announce response")
|
||||||
return resp, nil
|
return ctx, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AfterAnnounce does something with the results of an Announce after it has
|
// AfterAnnounce does something with the results of an Announce after it has
|
||||||
|
@ -78,18 +78,18 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleScrape generates a response for a Scrape.
|
// HandleScrape generates a response for a Scrape.
|
||||||
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (resp *bittorrent.ScrapeResponse, err error) {
|
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (_ context.Context, resp *bittorrent.ScrapeResponse, err error) {
|
||||||
resp = &bittorrent.ScrapeResponse{
|
resp = &bittorrent.ScrapeResponse{
|
||||||
Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)),
|
Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)),
|
||||||
}
|
}
|
||||||
for _, h := range l.preHooks {
|
for _, h := range l.preHooks {
|
||||||
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(resp.LogFields()).Debug("generated scrape response")
|
log.WithFields(resp.LogFields()).Debug("generated scrape response")
|
||||||
return resp, nil
|
return ctx, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AfterScrape does something with the results of a Scrape after it has been
|
// AfterScrape does something with the results of a Scrape after it has been
|
||||||
|
|
Loading…
Reference in a new issue