Record raw upload/download

This commit is contained in:
Justin Li 2014-08-01 16:07:10 -04:00
parent 8051e0e22e
commit ac61c7880d
2 changed files with 34 additions and 28 deletions

View file

@ -53,10 +53,10 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
} }
ann.BuildPeer(user, torrent) ann.BuildPeer(user, torrent)
var delta *models.AnnounceDelta
var uploaded, downloaded uint64
if tkr.cfg.PrivateEnabled { if tkr.cfg.PrivateEnabled {
uploaded, downloaded = delta(ann, torrent) delta = newAnnounceDelta(ann, torrent)
} }
created, err := updateSwarm(conn, ann) created, err := updateSwarm(conn, ann)
@ -70,16 +70,9 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
} }
if tkr.cfg.PrivateEnabled { if tkr.cfg.PrivateEnabled {
err = tkr.backend.RecordAnnounce(&models.AnnounceDelta{ delta.Created = created
Peer: ann.Peer, delta.Snatched = snatched
Torrent: ann.Torrent, if err = tkr.backend.RecordAnnounce(delta); err != nil {
User: ann.User,
Created: created,
Snatched: snatched,
Uploaded: uploaded,
Downloaded: downloaded,
})
if err != nil {
return err return err
} }
} else if tkr.cfg.PurgeInactiveTorrents && torrent.PeerCount() == 0 { } else if tkr.cfg.PurgeInactiveTorrents && torrent.PeerCount() == 0 {
@ -92,8 +85,11 @@ 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, t *models.Torrent) (uploaded, downloaded uint64) { // Builds a partially populated AnnounceDelta, without the Snatched and Created
// fields set.
func newAnnounceDelta(a *models.Announce, t *models.Torrent) *models.AnnounceDelta {
var oldUp, oldDown uint64 var oldUp, oldDown uint64
switch { switch {
case t.InSeederPool(a.Peer): case t.InSeederPool(a.Peer):
oldPeer := t.Seeders[a.Peer.Key()] oldPeer := t.Seeders[a.Peer.Key()]
@ -105,14 +101,8 @@ func delta(a *models.Announce, t *models.Torrent) (uploaded, downloaded uint64)
oldDown = oldPeer.Downloaded oldDown = oldPeer.Downloaded
} }
var ( rawDeltaUp := a.Peer.Uploaded - oldUp
rawDeltaUp = a.Peer.Uploaded - oldUp rawDeltaDown := a.Peer.Downloaded - oldDown
rawDeltaDown uint64
)
if !a.Config.FreeleechEnabled {
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.
if rawDeltaUp < 0 { if rawDeltaUp < 0 {
@ -123,10 +113,23 @@ func delta(a *models.Announce, t *models.Torrent) (uploaded, downloaded uint64)
rawDeltaDown = 0 rawDeltaDown = 0
} }
uploaded = uint64(float64(rawDeltaUp) * a.User.UpMultiplier * a.Torrent.UpMultiplier) uploaded := uint64(float64(rawDeltaUp) * a.User.UpMultiplier * a.Torrent.UpMultiplier)
downloaded = uint64(float64(rawDeltaDown) * a.User.DownMultiplier * a.Torrent.DownMultiplier) downloaded := uint64(float64(rawDeltaDown) * a.User.DownMultiplier * a.Torrent.DownMultiplier)
return if a.Config.FreeleechEnabled {
downloaded = 0
}
return &models.AnnounceDelta{
Peer: a.Peer,
Torrent: a.Torrent,
User: a.User,
Uploaded: uploaded,
RawUploaded: rawDeltaUp,
Downloaded: downloaded,
RawDownloaded: rawDeltaDown,
}
} }
// updateSwarm handles the changes to a torrent's swarm given an announce. // updateSwarm handles the changes to a torrent's swarm given an announce.

View file

@ -224,10 +224,13 @@ type AnnounceDelta struct {
// Snatched is true if this announce completed the download // Snatched is true if this announce completed the download
Snatched bool Snatched bool
// Uploaded contains the raw upload delta for this announce, in bytes // Uploaded contains the upload delta for this announce, in bytes
Uploaded uint64 Uploaded uint64
// Downloaded contains the raw download delta for this announce, in bytes RawUploaded uint64
Downloaded uint64
// Downloaded contains the download delta for this announce, in bytes
Downloaded uint64
RawDownloaded uint64
} }
// AnnounceResponse contains the information needed to fulfill an announce. // AnnounceResponse contains the information needed to fulfill an announce.