Calculate up/down delta before altering peer
This commit is contained in:
parent
f48e1e452f
commit
f747a79c8c
2 changed files with 36 additions and 36 deletions
|
@ -54,6 +54,8 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
||||||
|
|
||||||
ann.BuildPeer(user, torrent)
|
ann.BuildPeer(user, torrent)
|
||||||
|
|
||||||
|
uploaded, downloaded := delta(ann)
|
||||||
|
|
||||||
created, err := updateSwarm(conn, ann)
|
created, err := updateSwarm(conn, ann)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -65,8 +67,15 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if tkr.cfg.PrivateEnabled {
|
if tkr.cfg.PrivateEnabled {
|
||||||
delta := models.NewAnnounceDelta(ann, created, snatched)
|
err = tkr.backend.RecordAnnounce(&models.AnnounceDelta{
|
||||||
err = tkr.backend.RecordAnnounce(delta)
|
Peer: ann.Peer,
|
||||||
|
Torrent: ann.Torrent,
|
||||||
|
User: ann.User,
|
||||||
|
Created: created,
|
||||||
|
Snatched: snatched,
|
||||||
|
Uploaded: uploaded,
|
||||||
|
Downloaded: downloaded,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -80,6 +89,31 @@ 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) {
|
||||||
|
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.
|
// updateSwarm handles the changes to a torrent's swarm given an announce.
|
||||||
func updateSwarm(c Conn, ann *models.Announce) (created bool, err error) {
|
func updateSwarm(c Conn, ann *models.Announce) (created bool, err error) {
|
||||||
var createdv4, createdv6 bool
|
var createdv4, createdv6 bool
|
||||||
|
|
|
@ -239,40 +239,6 @@ type AnnounceResponse struct {
|
||||||
Compact bool
|
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.
|
// Scrape is a Scrape by a Peer.
|
||||||
type Scrape struct {
|
type Scrape struct {
|
||||||
Config *config.Config `json:"config"`
|
Config *config.Config `json:"config"`
|
||||||
|
|
Loading…
Add table
Reference in a new issue