Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
fix: set prio. per file too slow; use last stat; engdebug
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Aug 12, 2021
1 parent 28fec4d commit 5aa6e03
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
31 changes: 11 additions & 20 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (e *Engine) StartTorrent(infohash string) error {
defer t.Unlock()

if t.Started {
return fmt.Errorf("Already started")
return fmt.Errorf("already started")
}
t.Started = true
t.StartedAt = time.Now()
Expand All @@ -365,12 +365,7 @@ func (e *Engine) StartTorrent(infohash string) error {
}
}
if t.t.Info() != nil {
t.t.AllowDataUpload()
// start all files by setting the priority to normal
for _, f := range t.t.Files() {
f.SetPriority(torrent.PiecePriorityNormal)
}
t.t.AllowDataDownload()
t.t.DownloadAll()
}
return nil
}
Expand All @@ -387,24 +382,17 @@ func (e *Engine) StopTorrent(infohash string) error {
defer t.Unlock()

if !t.Started {
return fmt.Errorf("Already stopped")
return fmt.Errorf("already stopped")
}

if t.t.Info() != nil {
t.t.DisallowDataDownload()
// stop all files by setting the priority to None
for _, f := range t.t.Files() {
f.SetPriority(torrent.PiecePriorityNone)
}
t.t.DisallowDataUpload()
t.t.CancelPieces(0, t.t.NumPieces())
}

t.Started = false
t.StoppedAt = time.Now()
for _, f := range t.Files {
if f != nil {
f.Started = false
}
f.Started = false
}

return nil
Expand Down Expand Up @@ -445,7 +433,9 @@ func (e *Engine) StartFile(infohash, filepath string) error {
if f.Started {
return fmt.Errorf("already started")
}
t.Started = true
if !t.Started {
t.Started = true
}
f.Started = true
f.f.SetPriority(torrent.PiecePriorityNormal)
return nil
Expand All @@ -466,7 +456,7 @@ func (e *Engine) StopFile(infohash, filepath string) error {
}
}
if f == nil {
return fmt.Errorf("Missing file %s", filepath)
return fmt.Errorf("missing file %s", filepath)
}
if !f.Started {
return fmt.Errorf("already stopped")
Expand All @@ -483,7 +473,8 @@ func (e *Engine) StopFile(infohash, filepath string) error {
}

if allStopped {
go e.StopTorrent(infohash)
t.Started = false
t.StoppedAt = time.Now()
}

return nil
Expand Down
14 changes: 6 additions & 8 deletions engine/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ func (torrent *Torrent) updateOnGotInfo(t *torrent.Torrent) {
}

func (torrent *Torrent) updateConnStat() {
lastStat := torrent.Stats
torrent.Stats = torrent.t.Stats()

// calculate ratio
bRead := torrent.Stats.BytesReadData.Int64()
bRead := torrent.Stats.BytesReadUsefulData.Int64()
bWrite := torrent.Stats.BytesWrittenData.Int64()
if bRead > 0 {
torrent.SeedRatio = float32(bWrite) / float32(bRead)
Expand All @@ -97,22 +98,19 @@ func (torrent *Torrent) updateConnStat() {
}

now := time.Now()
bytes := torrent.t.BytesCompleted()
ulbytes := torrent.Stats.BytesWrittenData.Int64()

if !torrent.updatedAt.IsZero() {
// calculate rate
dtinv := float32(time.Second) / float32(now.Sub(torrent.updatedAt))

dldb := float32(bytes - torrent.Downloaded)
dldb := float32(bRead - lastStat.BytesReadUsefulData.Int64())
torrent.DownloadRate = dldb * dtinv

uldb := float32(ulbytes - torrent.Uploaded)
uldb := float32(bWrite - lastStat.BytesWrittenData.Int64())
torrent.UploadRate = uldb * dtinv
}

torrent.Downloaded = bytes
torrent.Uploaded = ulbytes
torrent.Downloaded = torrent.t.BytesCompleted()
torrent.Uploaded = bWrite
torrent.updatedAt = now
}

Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2k
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797 h1:yDf7ARQc637HoxDho7xjqdvO5ZA2Yb+xzv/fOnnvZzw=
crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oXPKINnZ8mcqnye1EkVkqsectk=
crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 h1:fILCBBFnjnrQ0whVJlGhfv1E/QiaFDNtGFBObEVRnYg=
crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl+fi1br7+Rr3LqpNJf1/uxUdtRUV+Tnj0o93V2B9MU=
Expand Down Expand Up @@ -56,6 +57,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92 h1:WGk37RyXPWcIALJxTkTNrXN3yLQp7hSFa3x5GkrK/Rs=
github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k=
github.com/anacrolix/confluence v1.7.1-0.20210221225853-90405640e928/go.mod h1:NoLcfoRet+kYttjLXJRmh4qBVrylJsfIItik5GGj21A=
github.com/anacrolix/confluence v1.7.1-0.20210311004351-d642adb8546c h1:HfbeiZS/0hwdotwtQhllrd3PagmuLgCN9O8CHJgzPGQ=
github.com/anacrolix/confluence v1.7.1-0.20210311004351-d642adb8546c/go.mod h1:KCZ3eObqKECNeZg0ekAoJVakHMP3gAdR8i0bQ26IkzM=
Expand All @@ -77,24 +80,33 @@ github.com/anacrolix/missinggo v1.1.0/go.mod h1:MBJu3Sk/k3ZfGYcS7z18gwfu72Ey/xop
github.com/anacrolix/missinggo v1.1.2-0.20190815015349-b888af804467/go.mod h1:MBJu3Sk/k3ZfGYcS7z18gwfu72Ey/xopPFJJbTi5yIo=
github.com/anacrolix/missinggo v1.2.1 h1:0IE3TqX5y5D0IxeMwTyIgqdDew4QrzcXaaEnJQyjHvw=
github.com/anacrolix/missinggo v1.2.1/go.mod h1:J5cMhif8jPmFoC3+Uvob3OXXNIhOUikzMt+uUjeM21Y=
github.com/anacrolix/missinggo v1.3.0 h1:06HlMsudotL7BAELRZs0yDZ4yVXsHXGi323QBjAVASw=
github.com/anacrolix/missinggo v1.3.0/go.mod h1:bqHm8cE8xr+15uVfMG3BFui/TxyB6//H5fwlq/TeqMc=
github.com/anacrolix/missinggo/perf v1.0.0 h1:7ZOGYziGEBytW49+KmYGTaNfnwUqP1HBsy6BqESAJVw=
github.com/anacrolix/missinggo/perf v1.0.0/go.mod h1:ljAFWkBuzkO12MQclXzZrosP5urunoLS0Cbvb4V0uMQ=
github.com/anacrolix/missinggo/v2 v2.2.0/go.mod h1:o0jgJoYOyaoYQ4E2ZMISVa9c88BbUBVQQW4QeRkNCGY=
github.com/anacrolix/missinggo/v2 v2.5.0 h1:75aciOVrzVV1bTH9rl8tYLbXO9A7HXFtHexTChawe/U=
github.com/anacrolix/missinggo/v2 v2.5.0/go.mod h1:HYuCbwvJXY3XbcmcIcTgZXHleoDXawxPWx/YiPzFzV0=
github.com/anacrolix/missinggo/v2 v2.5.1/go.mod h1:WEjqh2rmKECd0t1VhQkLGTdIWXO6f6NLjp5GlMZ+6FA=
github.com/anacrolix/missinggo/v2 v2.5.2-0.20210623112532-e21e4ddc477d h1:Z0oOzAPar3HzEwU00HnbK5JRe701kyaa+bnBJOaQ5zU=
github.com/anacrolix/missinggo/v2 v2.5.2-0.20210623112532-e21e4ddc477d/go.mod h1:WEjqh2rmKECd0t1VhQkLGTdIWXO6f6NLjp5GlMZ+6FA=
github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb/go.mod h1:x2/ErsYUmT77kezS63+wzZp8E3byYB0gzirM/WMBLfw=
github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg=
github.com/anacrolix/mmsg v1.0.0/go.mod h1:x8kRaJY/dCrY9Al0PEcj1mb/uFHwP6GCJ9fLl4thEPc=
github.com/anacrolix/multiless v0.0.0-20200413040533-acfd16f65d5d/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
github.com/anacrolix/multiless v0.0.0-20210222022749-ef43011a77ec/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
github.com/anacrolix/multiless v0.1.0 h1:gjR3SdJ+E0avnmEoAV/7K7n2kILZhVu/M6aQEtz8H3s=
github.com/anacrolix/multiless v0.1.0/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619 h1:ZkusP2EHxvxm+IymiKJ8DBVE/E6fJkb8K/2+GXZpjAY=
github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=
github.com/anacrolix/stm v0.3.0-alpha h1:yhOHk1NPkpGKqCAOB4XkgFXwB5Eh2KU/WVMksNnxr2Y=
github.com/anacrolix/stm v0.3.0-alpha/go.mod h1:spImf/rXwiAUoYYJK1YCZeWkpaHZ3kzjGFjwK5OStfU=
github.com/anacrolix/sync v0.0.0-20180808010631-44578de4e778/go.mod h1:s735Etp3joe/voe2sdaXLcqDdJSay1O0OPnM0ystjqk=
github.com/anacrolix/sync v0.2.0 h1:oRe22/ZB+v7v/5Mbc4d2zE0AXEZy0trKyKLjqYOt6tY=
github.com/anacrolix/sync v0.2.0/go.mod h1:BbecHL6jDSExojhNtgTFSBcdGerzNc64tz3DCOj/I0g=
github.com/anacrolix/sync v0.3.0 h1:ZPjTrkqQWEfnYVGTQHh5qNjokWaXnjsyXTJSMsKY0TA=
github.com/anacrolix/sync v0.3.0/go.mod h1:BbecHL6jDSExojhNtgTFSBcdGerzNc64tz3DCOj/I0g=
github.com/anacrolix/tagflag v0.0.0-20180109131632-2146c8d41bf0/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw=
github.com/anacrolix/tagflag v1.0.0/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw=
github.com/anacrolix/tagflag v1.1.0/go.mod h1:Scxs9CV10NQatSmbyjqmqmeQNwGzlNe0CMUMIxqHIG8=
Expand All @@ -105,6 +117,8 @@ github.com/anacrolix/torrent v1.25.1-0.20210221061757-051093ca31f5/go.mod h1:737
github.com/anacrolix/torrent v1.25.1-0.20210224024805-693c30dd889e/go.mod h1:d4V6QqkInfQidWVk8b8hMv8mtciswNitI1A2BiRSQV0=
github.com/anacrolix/torrent v1.28.0 h1:zPT5Q4tGVYfIXubGXiD6r6ljHvLT0C65R2++JRhnM3o=
github.com/anacrolix/torrent v1.28.0/go.mod h1:FvRiYw56BypG8LHgbyyJoGMVTsjqFvvKR+8agy4bFwM=
github.com/anacrolix/torrent v1.30.1 h1:Y8Sz8V6BEALhafgc7s1a1pJKYzy6X5gfu6ocF5F7nog=
github.com/anacrolix/torrent v1.30.1/go.mod h1:40Hf2bWxFqTbTWbrdig57JnmYLCjShbWWjdbB3VN5n4=
github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425 h1:/Wi6l2ONI1FUFWN4cBwHOO90V4ylp4ud/eov6GUcVFk=
github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425/go.mod h1:Pz94W3kl8rf+wxH3IbCa9Sq+DTJr8OSbV2Q3/y51vYs=
github.com/anacrolix/utp v0.1.0 h1:FOpQOmIwYsnENnz7tAGohA+r6iXpRjrq8ssKSre2Cp4=
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (s *Server) Run(version string) error {
if err != nil {
return err
}
c.EngineDebug = s.DebugTorrent

// write cloud-torrent.yaml at the same dir with -c conf and exit
if s.ConvYAML {
Expand Down

0 comments on commit 5aa6e03

Please sign in to comment.