Skip to content

Commit

Permalink
Fewer loops in sendCancels
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Jan 27, 2025
1 parent c868133 commit 834d8ab
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions bitswap/client/internal/peermanager/peerwantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,58 +284,51 @@ 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)
}
} else {
// Only send cancels to peers that received a corresponding want
cancelPeers := make(map[peer.ID]struct{}, len(pwm.wantPeers[cancelKs[0]]))
for _, c := range cancelKs {
for p := range pwm.wantPeers[c] {
cancelPeers[p] = struct{}{}
}
}
for p := range cancelPeers {
pws, ok := pwm.peerWants[p]
if !ok {
// Should never happen but check just in case
log.Errorf("sendCancels - peerWantManager index missing peer %s", p)
continue
}

send(p, pws)
}
}

// Decrement the wants gauges
for _, c := range cancelKs {
clearWantsForCID := func(c cid.Cid) {
peerCnts := peerCounts[c]

// If there were any peers that had a pending want-block for the key
if peerCnts.wantBlock > 0 {
// Decrement the want-block gauge
pwm.wantBlockGauge.Dec()
}

// If there was a peer that had a pending want or it was a broadcast want
if peerCnts.wanted() {
// Decrement the total wants gauge
pwm.wantGauge.Dec()
}
delete(pwm.wantPeers, c)
}

// Remove cancelled broadcast wants
for _, c := range broadcastCancels {
pwm.broadcastWants.Remove(c)
}
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)
}

// Batch-remove the reverse-index. There's no need to clear this index
// peer-by-peer.
for _, c := range cancelKs {
delete(pwm.wantPeers, c)
// Remove cancelled broadcast wants
for _, c := range broadcastCancels {
pwm.broadcastWants.Remove(c)
}

for _, c := range cancelKs {
clearWantsForCID(c)
}
} else {
// Only send cancels to peers that received a corresponding want
for _, c := range cancelKs {
for p := range pwm.wantPeers[c] {
pws, ok := pwm.peerWants[p]
if !ok {
// Should never happen but check just in case
log.Errorf("sendCancels - peerWantManager index missing peer %s", p)
continue

Check warning on line 325 in bitswap/client/internal/peermanager/peerwantmanager.go

View check run for this annotation

Codecov / codecov/patch

bitswap/client/internal/peermanager/peerwantmanager.go#L323-L325

Added lines #L323 - L325 were not covered by tests
}
send(p, pws)
}

clearWantsForCID(c)
}
}
}

Expand Down

0 comments on commit 834d8ab

Please sign in to comment.