From c28e9aaeae2ac16be0809efe6a7fd833ad317c15 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 30 Oct 2020 12:19:53 +1100 Subject: [PATCH] Don't delete requests until after they're written to disk This prevents too many pending writes building up. Webseed peers re-request synchronously, and the writes are done asynchronously, so they download too quickly and there was no backpressure. The backpressure now is provided by the upper limit on outstanding requests per connection. --- peerconn.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/peerconn.go b/peerconn.go index 60d00465..9ad056ce 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1289,14 +1289,16 @@ func (c *peer) receiveChunk(msg *pp.Message) error { torrent.Add("chunks received due to allowed fast", 1) } - // Request has been satisfied. - if c.deleteRequest(req) { - if c.expectingChunks() { - c._chunksReceivedWhileExpecting++ + defer func() { + // Request has been satisfied. + if c.deleteRequest(req) { + if c.expectingChunks() { + c._chunksReceivedWhileExpecting++ + } + } else { + torrent.Add("chunks received unwanted", 1) } - } else { - torrent.Add("chunks received unwanted", 1) - } + }() // Do we actually want this chunk? if t.haveChunk(req) {