Skip to content

Commit

Permalink
fix: bitswap simplify cancel (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumemichel authored Jan 31, 2025
1 parent d678ff8 commit 62512fb
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions bitswap/client/internal/peermanager/peerwantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,20 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid) {
return
}

// Record how many peers have a pending want-block and want-have for each
// key to be cancelled
// Track cancellation state: peerCounts tracks per-CID want counts across
// peers, while broadcastCancels collects CIDs wants that were broadcasted
// and need cancellation across all peers
peerCounts := make(map[cid.Cid]wantPeerCnts, len(cancelKs))
for _, c := range cancelKs {
peerCounts[c] = pwm.wantPeerCounts(c)
}

// Create a buffer to use for filtering cancels per peer, with the
// broadcast wants at the front of the buffer (broadcast wants are sent to
// all peers)
broadcastCancels := make([]cid.Cid, 0, len(cancelKs))
for _, c := range cancelKs {
if pwm.broadcastWants.Has(c) {
peerCounts[c] = pwm.wantPeerCounts(c)
if peerCounts[c].isBroadcast {
broadcastCancels = append(broadcastCancels, c)
}
}

// Send cancels to a particular peer
send := func(p peer.ID, pws *peerWant) {
send := func(pws *peerWant) {
// Start from the broadcast cancels
toCancel := broadcastCancels

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

// Remove cancelled broadcast wants
Expand All @@ -323,7 +318,7 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid) {
log.Errorf("sendCancels - peerWantManager index missing peer %s", p)
continue
}
send(p, pws)
send(pws)
}

clearWantsForCID(c)
Expand Down

0 comments on commit 62512fb

Please sign in to comment.