From f7becf952b06807a34807e80d429ca569d931b03 Mon Sep 17 00:00:00 2001 From: Cedric Charly Date: Mon, 5 Jun 2017 21:53:17 -0500 Subject: [PATCH] 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 --- frontend/frontend.go | 10 ++++++++-- frontend/http/frontend.go | 8 ++++---- frontend/udp/frontend.go | 10 ++++++---- middleware/middleware.go | 12 ++++++------ 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/frontend/frontend.go b/frontend/frontend.go index 63569c3..5ef4047 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -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) diff --git a/frontend/http/frontend.go b/frontend/http/frontend.go index c4de414..345b9e7 100644 --- a/frontend/http/frontend.go +++ b/frontend/http/frontend.go @@ -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) } diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index d304366..f315066 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -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 diff --git a/middleware/middleware.go b/middleware/middleware.go index 128d76e..b7075ad 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -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