Prevent unsigned overflow from breaking stats
This commit is contained in:
parent
ac61c7880d
commit
a55219963b
1 changed files with 17 additions and 21 deletions
|
@ -87,43 +87,39 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
||||||
|
|
||||||
// Builds a partially populated AnnounceDelta, without the Snatched and Created
|
// Builds a partially populated AnnounceDelta, without the Snatched and Created
|
||||||
// fields set.
|
// fields set.
|
||||||
func newAnnounceDelta(a *models.Announce, t *models.Torrent) *models.AnnounceDelta {
|
func newAnnounceDelta(ann *models.Announce, t *models.Torrent) *models.AnnounceDelta {
|
||||||
var oldUp, oldDown uint64
|
var oldUp, oldDown, rawDeltaUp, rawDeltaDown uint64
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case t.InSeederPool(a.Peer):
|
case t.InSeederPool(ann.Peer):
|
||||||
oldPeer := t.Seeders[a.Peer.Key()]
|
oldPeer := t.Seeders[ann.Peer.Key()]
|
||||||
oldUp = oldPeer.Uploaded
|
oldUp = oldPeer.Uploaded
|
||||||
oldDown = oldPeer.Downloaded
|
oldDown = oldPeer.Downloaded
|
||||||
case t.InLeecherPool(a.Peer):
|
case t.InLeecherPool(ann.Peer):
|
||||||
oldPeer := t.Leechers[a.Peer.Key()]
|
oldPeer := t.Leechers[ann.Peer.Key()]
|
||||||
oldUp = oldPeer.Uploaded
|
oldUp = oldPeer.Uploaded
|
||||||
oldDown = oldPeer.Downloaded
|
oldDown = oldPeer.Downloaded
|
||||||
}
|
}
|
||||||
|
|
||||||
rawDeltaUp := a.Peer.Uploaded - oldUp
|
|
||||||
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 ann.Peer.Uploaded > oldUp {
|
||||||
rawDeltaUp = 0
|
rawDeltaUp = ann.Peer.Uploaded - oldUp
|
||||||
|
}
|
||||||
|
if ann.Peer.Downloaded > oldDown {
|
||||||
|
rawDeltaDown = ann.Peer.Downloaded - oldDown
|
||||||
}
|
}
|
||||||
|
|
||||||
if rawDeltaDown < 0 {
|
uploaded := uint64(float64(rawDeltaUp) * ann.User.UpMultiplier * ann.Torrent.UpMultiplier)
|
||||||
rawDeltaDown = 0
|
downloaded := uint64(float64(rawDeltaDown) * ann.User.DownMultiplier * ann.Torrent.DownMultiplier)
|
||||||
}
|
|
||||||
|
|
||||||
uploaded := uint64(float64(rawDeltaUp) * a.User.UpMultiplier * a.Torrent.UpMultiplier)
|
if ann.Config.FreeleechEnabled {
|
||||||
downloaded := uint64(float64(rawDeltaDown) * a.User.DownMultiplier * a.Torrent.DownMultiplier)
|
|
||||||
|
|
||||||
if a.Config.FreeleechEnabled {
|
|
||||||
downloaded = 0
|
downloaded = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return &models.AnnounceDelta{
|
return &models.AnnounceDelta{
|
||||||
Peer: a.Peer,
|
Peer: ann.Peer,
|
||||||
Torrent: a.Torrent,
|
Torrent: ann.Torrent,
|
||||||
User: a.User,
|
User: ann.User,
|
||||||
|
|
||||||
Uploaded: uploaded,
|
Uploaded: uploaded,
|
||||||
RawUploaded: rawDeltaUp,
|
RawUploaded: rawDeltaUp,
|
||||||
|
|
Loading…
Add table
Reference in a new issue