From b2cabe7dbf70c874044928bf2d0355df9f4d4452 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 11 Oct 2021 22:04:53 +1100 Subject: [PATCH] Handle allowed fast while choked when requests already exist in the same piece --- peerconn.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/peerconn.go b/peerconn.go index a4ad5952..e00006ff 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1118,15 +1118,24 @@ func (c *PeerConn) mainReadLoop() (err error) { torrent.Add("allowed fasts received", 1) log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).SetLevel(log.Debug).Log(c.t.logger) pieceIndex := msg.Index.Int() - c.peerAllowedFast.AddInt(pieceIndex) - n := roaringBitmapRangeCardinality( - &c.actualRequestState.Requests, - t.pieceRequestIndexOffset(pieceIndex), - t.pieceRequestIndexOffset(pieceIndex+1)) - if n != 0 { - panic(n) + // If we have outstanding requests that aren't currently counted toward the combined + // outstanding request count, increment them. + if c.peerAllowedFast.CheckedAdd(msg.Index.Uint32()) && c.peerChoking && + // The check here could be against having the info, but really what we need to know + // is if there are any existing requests. + !c.actualRequestState.Requests.IsEmpty() { + + i := c.actualRequestState.Requests.Iterator() + i.AdvanceIfNeeded(t.pieceRequestIndexOffset(pieceIndex)) + for i.HasNext() { + r := i.Next() + if r >= t.pieceRequestIndexOffset(pieceIndex+1) { + break + } + c.t.pendingRequests.Inc(i.Next()) + } } - c.updateRequests("allowed fast") + c.updateRequests("PeerConn.mainReadLoop allowed fast") case pp.Extended: err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload) default: -- 2.48.1