Calculate delta based off of old peer

This commit is contained in:
Jimmy Zelinskie 2014-08-01 15:51:26 -04:00
parent 5622a3ded2
commit 8051e0e22e

View file

@ -56,7 +56,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
var uploaded, downloaded uint64 var uploaded, downloaded uint64
if tkr.cfg.PrivateEnabled { if tkr.cfg.PrivateEnabled {
uploaded, downloaded = delta(ann) uploaded, downloaded = delta(ann, torrent)
} }
created, err := updateSwarm(conn, ann) created, err := updateSwarm(conn, ann)
@ -92,14 +92,26 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
return w.WriteAnnounce(newAnnounceResponse(ann)) return w.WriteAnnounce(newAnnounceResponse(ann))
} }
func delta(a *models.Announce) (uploaded, downloaded uint64) { func delta(a *models.Announce, t *models.Torrent) (uploaded, downloaded uint64) {
var oldUp, oldDown uint64
switch {
case t.InSeederPool(a.Peer):
oldPeer := t.Seeders[a.Peer.Key()]
oldUp = oldPeer.Uploaded
oldDown = oldPeer.Downloaded
case t.InLeecherPool(a.Peer):
oldPeer := t.Leechers[a.Peer.Key()]
oldUp = oldPeer.Uploaded
oldDown = oldPeer.Downloaded
}
var ( var (
rawDeltaUp = a.Peer.Uploaded - a.Uploaded rawDeltaUp = a.Peer.Uploaded - oldUp
rawDeltaDown uint64 rawDeltaDown uint64
) )
if !a.Config.FreeleechEnabled { if !a.Config.FreeleechEnabled {
rawDeltaDown = a.Peer.Downloaded - a.Downloaded rawDeltaDown = a.Peer.Downloaded - oldDown
} }
// Restarting a torrent may cause a delta to be negative. // Restarting a torrent may cause a delta to be negative.