Skip to content

Commit

Permalink
Fix requests still being made when downloading is disallowed
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Jan 15, 2024
1 parent b5a11f0 commit bdcb6c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 0 additions & 3 deletions peer.go
Expand Up @@ -480,9 +480,6 @@ func (cn *Peer) updateRequests(reason string) {
if cn.needRequestUpdate != "" {
return
}
if reason != peerUpdateRequestsTimerReason && !cn.isLowOnRequests() {
return
}
cn.needRequestUpdate = reason
cn.handleUpdateRequests()
}
Expand Down
10 changes: 3 additions & 7 deletions requesting.go
Expand Up @@ -173,6 +173,9 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
if t.closed.IsSet() {
return
}
if t.dataDownloadDisallowed.Bool() {
return
}
input := t.getRequestStrategyInput()
requestHeap := desiredPeerRequests{
peer: p,
Expand Down Expand Up @@ -257,13 +260,6 @@ func (p *Peer) applyRequestState(next desiredRequestState) {

t := p.t
originalRequestCount := current.Requests.GetCardinality()
// We're either here on a timer, or because we ran out of requests. Both are valid reasons to
// alter peakRequests.
if originalRequestCount != 0 && p.needRequestUpdate != peerUpdateRequestsTimerReason {
panic(fmt.Sprintf(
"expected zero existing requests (%v) for update reason %q",
originalRequestCount, p.needRequestUpdate))
}
for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()+current.Cancelled.GetCardinality()) < p.nominalMaxRequests() {
req := heap.Pop(requestHeap)
existing := t.requestingPeer(req)
Expand Down
18 changes: 15 additions & 3 deletions torrent.go
Expand Up @@ -2483,25 +2483,37 @@ func (t *Torrent) onWriteChunkErr(err error) {
}

func (t *Torrent) DisallowDataDownload() {
t.cl.lock()
defer t.cl.unlock()
t.disallowDataDownloadLocked()
}

func (t *Torrent) disallowDataDownloadLocked() {
t.dataDownloadDisallowed.Set()
t.iterPeers(func(p *Peer) {
// Could check if peer request state is empty/not interested?
p.updateRequests("disallow data download")
p.cancelAllRequests()
})
}

func (t *Torrent) AllowDataDownload() {
t.cl.lock()
defer t.cl.unlock()
t.dataDownloadDisallowed.Clear()
t.iterPeers(func(p *Peer) {
p.updateRequests("allow data download")
})
}

// Enables uploading data, if it was disabled.
func (t *Torrent) AllowDataUpload() {
t.cl.lock()
defer t.cl.unlock()
t.dataUploadDisallowed = false
for c := range t.conns {
c.updateRequests("allow data upload")
}
t.iterPeers(func(p *Peer) {
p.updateRequests("allow data upload")
})
}

// Disables uploading data, if it was enabled.
Expand Down

0 comments on commit bdcb6c9

Please sign in to comment.