Skip to content

Commit 62512fb

Browse files
authoredJan 31, 2025··
fix: bitswap simplify cancel (#824)
1 parent d678ff8 commit 62512fb

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed
 

‎bitswap/client/internal/peermanager/peerwantmanager.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,20 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid) {
237237
return
238238
}
239239

240-
// Record how many peers have a pending want-block and want-have for each
241-
// key to be cancelled
240+
// Track cancellation state: peerCounts tracks per-CID want counts across
241+
// peers, while broadcastCancels collects CIDs wants that were broadcasted
242+
// and need cancellation across all peers
242243
peerCounts := make(map[cid.Cid]wantPeerCnts, len(cancelKs))
243-
for _, c := range cancelKs {
244-
peerCounts[c] = pwm.wantPeerCounts(c)
245-
}
246-
247-
// Create a buffer to use for filtering cancels per peer, with the
248-
// broadcast wants at the front of the buffer (broadcast wants are sent to
249-
// all peers)
250244
broadcastCancels := make([]cid.Cid, 0, len(cancelKs))
251245
for _, c := range cancelKs {
252-
if pwm.broadcastWants.Has(c) {
246+
peerCounts[c] = pwm.wantPeerCounts(c)
247+
if peerCounts[c].isBroadcast {
253248
broadcastCancels = append(broadcastCancels, c)
254249
}
255250
}
256251

257252
// Send cancels to a particular peer
258-
send := func(p peer.ID, pws *peerWant) {
253+
send := func(pws *peerWant) {
259254
// Start from the broadcast cancels
260255
toCancel := broadcastCancels
261256

@@ -301,8 +296,8 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid) {
301296
if len(broadcastCancels) > 0 {
302297
// If a broadcast want is being cancelled, send the cancel to all
303298
// peers
304-
for p, pws := range pwm.peerWants {
305-
send(p, pws)
299+
for _, pws := range pwm.peerWants {
300+
send(pws)
306301
}
307302

308303
// Remove cancelled broadcast wants
@@ -323,7 +318,7 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid) {
323318
log.Errorf("sendCancels - peerWantManager index missing peer %s", p)
324319
continue
325320
}
326-
send(p, pws)
321+
send(pws)
327322
}
328323

329324
clearWantsForCID(c)

0 commit comments

Comments
 (0)
Please sign in to comment.