Add Torrent-level request cancel for consistency

This commit is contained in:
Matt Joiner 2021-12-10 18:04:45 +11:00
parent aca22a0896
commit ddf41fc2f1
3 changed files with 25 additions and 5 deletions

View File

@ -1550,8 +1550,10 @@ func (c *Peer) deleteRequest(r RequestIndex) bool {
f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)}) f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)})
} }
c.updateExpectingChunks() c.updateExpectingChunks()
delete(c.t.pendingRequests, r) if c.t.requestingPeer(r) == c {
delete(c.t.lastRequested, r) delete(c.t.pendingRequests, r)
delete(c.t.lastRequested, r)
}
return true return true
} }
@ -1694,6 +1696,10 @@ func (pc *PeerConn) isLowOnRequests() bool {
return pc.actualRequestState.Requests.IsEmpty() return pc.actualRequestState.Requests.IsEmpty()
} }
func (p *Peer) uncancelledRequests() uint64 {
return p.actualRequestState.Requests.GetCardinality() - p.cancelledRequests.GetCardinality()
}
func (pc *PeerConn) remoteIsTransmission() bool { func (pc *PeerConn) remoteIsTransmission() bool {
return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-' return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
} }

View File

@ -211,6 +211,7 @@ func (p *Peer) applyRequestState(next desiredRequestState) bool {
} }
more := true more := true
requestHeap := &next.Requests requestHeap := &next.Requests
t := p.t
heap.Init(requestHeap) heap.Init(requestHeap)
for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()) < p.nominalMaxRequests() { for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()) < p.nominalMaxRequests() {
req := heap.Pop(requestHeap).(RequestIndex) req := heap.Pop(requestHeap).(RequestIndex)
@ -219,9 +220,9 @@ func (p *Peer) applyRequestState(next desiredRequestState) bool {
// requests, so we can skip this one with no additional consideration. // requests, so we can skip this one with no additional consideration.
continue continue
} }
existing := p.t.pendingRequests[req] existing := t.requestingPeer(req)
if existing != nil && existing != p && existing.actualRequestState.Requests.GetCardinality()-existing.cancelledRequests.GetCardinality() > current.Requests.GetCardinality() { if existing != nil && existing != p && existing.uncancelledRequests() > current.Requests.GetCardinality() {
existing.cancel(req) t.cancelRequest(req)
} }
more = p.mustRequest(req) more = p.mustRequest(req)
if !more { if !more {

View File

@ -2310,3 +2310,16 @@ func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex {
func (t *Torrent) updateComplete() { func (t *Torrent) updateComplete() {
t.Complete.SetBool(t.haveAllPieces()) 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]
}