From: Matt Joiner <anacrolix@gmail.com>
Date: Mon, 13 Dec 2021 01:11:38 +0000 (+1100)
Subject: Add Peer.cancelAllRequests for webseedPeer
X-Git-Tag: v1.39.1~1
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=9d9556c97f0a6d8c805fc3724674122dbb706d5a;p=btrtrc.git

Add Peer.cancelAllRequests for webseedPeer
---

diff --git a/peerconn.go b/peerconn.go
index 0083a073..423d4e8b 100644
--- a/peerconn.go
+++ b/peerconn.go
@@ -1586,6 +1586,16 @@ func (c *Peer) assertNoRequests() {
 	}
 }
 
+func (c *Peer) cancelAllRequests() (cancelled *roaring.Bitmap) {
+	cancelled = c.requestState.Requests.Clone()
+	cancelled.Iterate(func(x uint32) bool {
+		c.cancel(x)
+		return true
+	})
+	c.assertNoRequests()
+	return
+}
+
 // This is called when something has changed that should wake the writer, such as putting stuff into
 // the writeBuffer, or changing some state that the writer can act on.
 func (c *PeerConn) tickleWriter() {
diff --git a/webseed-peer.go b/webseed-peer.go
index 08a7be2c..f2ef7a81 100644
--- a/webseed-peer.go
+++ b/webseed-peer.go
@@ -129,10 +129,8 @@ func (ws *webseedPeer) handleUpdateRequests() {
 
 func (ws *webseedPeer) onClose() {
 	ws.peer.logger.WithLevel(log.Debug).Print("closing")
-	ws.peer.deleteAllRequests()
-	for _, r := range ws.activeRequests {
-		r.Cancel()
-	}
+	// Just deleting them means we would have to manually cancel active requests.
+	ws.peer.cancelAllRequests()
 	ws.peer.t.iterPeers(func(p *Peer) {
 		if p.isLowOnRequests() {
 			p.updateRequests("webseedPeer.onClose")