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.
This commit is contained in:
Jimmy Zelinskie 2015-02-26 09:03:52 -05:00
parent 9b4999c0cf
commit ac2fcade1e
3 changed files with 6 additions and 5 deletions

View file

@ -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 {

View file

@ -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))
}

View file

@ -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,
})