peerconn.go | 10 ++++++++-- requesting.go | 7 ++++--- torrent.go | 13 +++++++++++++ diff --git a/peerconn.go b/peerconn.go index 4f43d50d3e444e9c61d67000947abf106e039c5a..f36da1c03f898d9cfd4176e48d0a44322783bc5d 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1550,8 +1550,10 @@ for _, f := range c.callbacks.DeletedRequest { f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)}) } c.updateExpectingChunks() - delete(c.t.pendingRequests, r) - delete(c.t.lastRequested, r) + if c.t.requestingPeer(r) == c { + delete(c.t.pendingRequests, r) + delete(c.t.lastRequested, r) + } return true } @@ -1692,6 +1694,10 @@ } func (pc *PeerConn) isLowOnRequests() bool { return pc.actualRequestState.Requests.IsEmpty() +} + +func (p *Peer) uncancelledRequests() uint64 { + return p.actualRequestState.Requests.GetCardinality() - p.cancelledRequests.GetCardinality() } func (pc *PeerConn) remoteIsTransmission() bool { diff --git a/requesting.go b/requesting.go index 50771025d88a67df603ab2da3e14bc6fe3838751..f3cd868091f22649c31f19f9819d43925b54f774 100644 --- a/requesting.go +++ b/requesting.go @@ -211,6 +211,7 @@ return false } more := true requestHeap := &next.Requests + t := p.t heap.Init(requestHeap) for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()) < p.nominalMaxRequests() { req := heap.Pop(requestHeap).(RequestIndex) @@ -219,9 +220,9 @@ // Waiting for a reject or piece message, which will suitably trigger us to update our // requests, so we can skip this one with no additional consideration. continue } - existing := p.t.pendingRequests[req] - if existing != nil && existing != p && existing.actualRequestState.Requests.GetCardinality()-existing.cancelledRequests.GetCardinality() > current.Requests.GetCardinality() { - existing.cancel(req) + existing := t.requestingPeer(req) + if existing != nil && existing != p && existing.uncancelledRequests() > current.Requests.GetCardinality() { + t.cancelRequest(req) } more = p.mustRequest(req) if !more { diff --git a/torrent.go b/torrent.go index b2688994cc4a5ba9d88f2b2cc77deed6f341cf97..db24603bcaa8fb7dae9dd236f80f5e2b93efb6b6 100644 --- a/torrent.go +++ b/torrent.go @@ -2310,3 +2310,16 @@ func (t *Torrent) updateComplete() { t.Complete.SetBool(t.haveAllPieces()) } + +func (t *Torrent) cancelRequest(r RequestIndex) *Peer { + p := t.pendingRequests[r] + if p != nil { + p.cancel(r) + } + delete(t.pendingRequests, r) + return p +} + +func (t *Torrent) requestingPeer(r RequestIndex) *Peer { + return t.pendingRequests[r] +}