From f747a79c8ce042e089f27a5f89d8e5f803eb0675 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Fri, 1 Aug 2014 15:11:37 -0400 Subject: [PATCH] Calculate up/down delta before altering peer --- tracker/announce.go | 38 ++++++++++++++++++++++++++++++++++++-- tracker/models/models.go | 34 ---------------------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/tracker/announce.go b/tracker/announce.go index eaf9e46..0fccfea 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -54,6 +54,8 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error { ann.BuildPeer(user, torrent) + uploaded, downloaded := delta(ann) + created, err := updateSwarm(conn, ann) if err != nil { return err @@ -65,8 +67,15 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error { } if tkr.cfg.PrivateEnabled { - delta := models.NewAnnounceDelta(ann, created, snatched) - err = tkr.backend.RecordAnnounce(delta) + err = tkr.backend.RecordAnnounce(&models.AnnounceDelta{ + Peer: ann.Peer, + Torrent: ann.Torrent, + User: ann.User, + Created: created, + Snatched: snatched, + Uploaded: uploaded, + Downloaded: downloaded, + }) if err != nil { return err } @@ -80,6 +89,31 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error { return w.WriteAnnounce(newAnnounceResponse(ann)) } +func delta(a *models.Announce) (uploaded, downloaded uint64) { + var ( + rawDeltaUp = a.Peer.Uploaded - a.Uploaded + rawDeltaDown uint64 + ) + + if !a.Config.FreeleechEnabled { + rawDeltaDown = a.Peer.Downloaded - a.Downloaded + } + + // Restarting a torrent may cause a delta to be negative. + if rawDeltaUp < 0 { + rawDeltaUp = 0 + } + + if rawDeltaDown < 0 { + rawDeltaDown = 0 + } + + uploaded = uint64(float64(rawDeltaUp) * a.User.UpMultiplier * a.Torrent.UpMultiplier) + downloaded = uint64(float64(rawDeltaDown) * a.User.DownMultiplier * a.Torrent.DownMultiplier) + + return +} + // updateSwarm handles the changes to a torrent's swarm given an announce. func updateSwarm(c Conn, ann *models.Announce) (created bool, err error) { var createdv4, createdv6 bool diff --git a/tracker/models/models.go b/tracker/models/models.go index 56a3b36..a6217c9 100644 --- a/tracker/models/models.go +++ b/tracker/models/models.go @@ -239,40 +239,6 @@ type AnnounceResponse struct { Compact bool } -// NewAnnounceDelta calculates a Peer's download and upload deltas between -// Announces and generates an AnnounceDelta. -func NewAnnounceDelta(a *Announce, created, snatched bool) *AnnounceDelta { - var ( - rawDeltaUp = a.Peer.Uploaded - a.Uploaded - rawDeltaDown uint64 - ) - - if !a.Config.FreeleechEnabled { - rawDeltaDown = a.Peer.Downloaded - a.Downloaded - } - - // Restarting a torrent may cause a delta to be negative. - if rawDeltaUp < 0 { - rawDeltaUp = 0 - } - - if rawDeltaDown < 0 { - rawDeltaDown = 0 - } - - return &AnnounceDelta{ - Peer: a.Peer, - Torrent: a.Torrent, - User: a.User, - - Created: created, - Snatched: snatched, - - Uploaded: uint64(float64(rawDeltaUp) * a.User.UpMultiplier * a.Torrent.UpMultiplier), - Downloaded: uint64(float64(rawDeltaDown) * a.User.DownMultiplier * a.Torrent.DownMultiplier), - } -} - // Scrape is a Scrape by a Peer. type Scrape struct { Config *config.Config `json:"config"`