From 1ac5811990e4f85d5fabcc14123f646dfec9cb9e Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 28 Jan 2021 16:36:35 +1100 Subject: [PATCH] Remove requests as soon as chunk data is received Note that this breaks the backpressure on webseed responses again, and should be fixed shortly. --- peerconn.go | 11 +++++++++-- torrent.go | 7 +++++-- webseed-peer.go | 3 ++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/peerconn.go b/peerconn.go index 1f9a3a53..f9bec8e1 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1365,7 +1365,14 @@ func (c *Peer) receiveChunk(msg *pp.Message) error { torrent.Add("chunks received due to allowed fast", 1) } - defer func() { + // TODO: This needs to happen immediately, to prevent cancels occurring asynchronously when have + // actually already received the piece, while we have the Client unlocked to write the data out. + { + if _, ok := c.requests[req]; ok { + for _, f := range c.callbacks.ReceivedRequested { + f(PeerMessageEvent{c, msg}) + } + } // Request has been satisfied. if c.deleteRequest(req) { if c.expectingChunks() { @@ -1374,7 +1381,7 @@ func (c *Peer) receiveChunk(msg *pp.Message) error { } else { torrent.Add("chunks received unwanted", 1) } - }() + } // Do we actually want this chunk? if t.haveChunk(req) { diff --git a/torrent.go b/torrent.go index 059e5883..0b858162 100644 --- a/torrent.go +++ b/torrent.go @@ -2117,10 +2117,13 @@ func (t *Torrent) addWebSeed(url string) { Network: "http", reconciledHandshakeStats: true, peerSentHaveAll: true, - PeerMaxRequests: maxRequests, - RemoteAddr: remoteAddrFromUrl(url), + // TODO: Raise this limit, and instead limit concurrent fetches. + PeerMaxRequests: maxRequests, + RemoteAddr: remoteAddrFromUrl(url), }, client: webseed.Client{ + // TODO: Investigate a MaxConnsPerHost in the transport for this, possibly in a global + // Client. HttpClient: http.DefaultClient, Url: url, }, diff --git a/webseed-peer.go b/webseed-peer.go index 1856a4d0..0e4676f5 100644 --- a/webseed-peer.go +++ b/webseed-peer.go @@ -13,7 +13,8 @@ import ( ) type webseedPeer struct { - client webseed.Client + client webseed.Client + // TODO: Remove finished entries from this. requests map[Request]webseed.Request peer Peer }