From ac2fcade1e575e6bc853c9938cfce528f3fb9f76 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 26 Feb 2015 09:03:52 -0500 Subject: [PATCH] tracker: record announce/scrape stats in handlers This deduplicates code tracking the stats code tracking announces and scrapes for each protocol. The down side is that it isn't aware of any failures writing responses, but it was already being called before the write to a response for HTTP. --- http/routes.go | 4 ---- tracker/announce.go | 1 + tracker/scrape.go | 6 +++++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/http/routes.go b/http/routes.go index 3a8f1c7..b405b65 100644 --- a/http/routes.go +++ b/http/routes.go @@ -86,8 +86,6 @@ func handleTorrentError(err error, w *Writer) (int, error) { } func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) { - stats.RecordEvent(stats.Announce) - writer := &Writer{w} ann, err := s.newAnnounce(r, p) if err != nil { @@ -98,8 +96,6 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprou } func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) { - stats.RecordEvent(stats.Scrape) - writer := &Writer{w} scrape, err := s.newScrape(r, p) if err != nil { diff --git a/tracker/announce.go b/tracker/announce.go index 74a3bd9..68b2bc5 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -70,6 +70,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) (err error) { stats.RecordEvent(stats.DeletedTorrent) } + stats.RecordEvent(stats.Announce) return w.WriteAnnounce(newAnnounceResponse(ann)) } diff --git a/tracker/scrape.go b/tracker/scrape.go index 921f356..01435d9 100644 --- a/tracker/scrape.go +++ b/tracker/scrape.go @@ -4,7 +4,10 @@ package tracker -import "github.com/chihaya/chihaya/tracker/models" +import ( + "github.com/chihaya/chihaya/stats" + "github.com/chihaya/chihaya/tracker/models" +) // HandleScrape encapsulates all the logic of handling a BitTorrent client's // scrape without being coupled to any transport protocol. @@ -24,6 +27,7 @@ func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) (err error) { torrents = append(torrents, torrent) } + stats.RecordEvent(stats.Scrape) return w.WriteScrape(&models.ScrapeResponse{ Files: torrents, })