Merge pull request #331 from cedricgc/tracker-logic-context

TrackerLogic returns modified Contexts
This commit is contained in:
mrd0ll4r 2017-06-08 17:39:04 +02:00 committed by GitHub
commit 3a323d9338
4 changed files with 24 additions and 16 deletions

View file

@ -11,14 +11,20 @@ import (
// after the response has been delivered to the client.
type TrackerLogic interface {
// 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
// has been completed.
AfterAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse)
// 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(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse)

View file

@ -191,7 +191,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httpr
af = new(bittorrent.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 {
WriteError(w, err)
return
@ -203,7 +203,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httpr
return
}
go f.logic.AfterAnnounce(context.Background(), req, resp)
go f.logic.AfterAnnounce(ctx, req, resp)
}
// 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 = req.AddressFamily
resp, err := f.logic.HandleScrape(context.Background(), req)
ctx, resp, err := f.logic.HandleScrape(context.Background(), req)
if err != nil {
WriteError(w, err)
return
@ -260,5 +260,5 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
return
}
go f.logic.AfterScrape(context.Background(), req, resp)
go f.logic.AfterScrape(ctx, req, resp)
}

View file

@ -286,8 +286,9 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
af = new(bittorrent.AddressFamily)
*af = req.IP.AddressFamily
var ctx context.Context
var resp *bittorrent.AnnounceResponse
resp, err = t.logic.HandleAnnounce(context.Background(), req)
ctx, resp, err = t.logic.HandleAnnounce(context.Background(), req)
if err != nil {
WriteError(w, txID, err)
return
@ -295,7 +296,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
WriteAnnounce(w, txID, resp, actionID == announceV6ActionID)
go t.logic.AfterAnnounce(context.Background(), req, resp)
go t.logic.AfterAnnounce(ctx, req, resp)
case scrapeActionID:
actionName = "scrape"
@ -319,8 +320,9 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
af = new(bittorrent.AddressFamily)
*af = req.AddressFamily
var ctx context.Context
var resp *bittorrent.ScrapeResponse
resp, err = t.logic.HandleScrape(context.Background(), req)
ctx, resp, err = t.logic.HandleScrape(context.Background(), req)
if err != nil {
WriteError(w, txID, err)
return
@ -328,7 +330,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
WriteScrape(w, txID, resp)
go t.logic.AfterScrape(context.Background(), req, resp)
go t.logic.AfterScrape(ctx, req, resp)
default:
err = errUnknownAction

View file

@ -49,7 +49,7 @@ type Logic struct {
}
// 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{
Interval: l.announceInterval,
MinInterval: l.announceInterval,
@ -57,12 +57,12 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
}
for _, h := range l.preHooks {
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")
return resp, nil
return ctx, resp, nil
}
// 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.
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{
Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)),
}
for _, h := range l.preHooks {
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")
return resp, nil
return ctx, resp, nil
}
// AfterScrape does something with the results of a Scrape after it has been