Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 93801a7

Browse files
authored
Merge pull request #325 from ipfs/fix/pmgr-log
fix: log unexpected condition in peerWantManager.prepareSendWants()
2 parents 7348b26 + fd0e1ff commit 93801a7

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

internal/peermanager/peermanager.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"context"
55
"sync"
66

7+
logging "github.com/ipfs/go-log"
78
"github.com/ipfs/go-metrics-interface"
89

910
cid "github.com/ipfs/go-cid"
1011
peer "github.com/libp2p/go-libp2p-core/peer"
1112
)
1213

14+
var log = logging.Logger("bs:peermgr")
15+
1316
// PeerQueue provides a queue of messages to be sent for a single peer.
1417
type PeerQueue interface {
1518
AddBroadcastWantHaves([]cid.Cid)

internal/peermanager/peerwantmanager.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,44 @@ func (pwm *peerWantManager) prepareSendWants(p peer.ID, wantBlocks []cid.Cid, wa
8686
resWantHvs := make([]cid.Cid, 0)
8787

8888
// Get the existing want-blocks and want-haves for the peer
89-
if pws, ok := pwm.peerWants[p]; ok {
90-
// Iterate over the requested want-blocks
91-
for _, c := range wantBlocks {
92-
// If the want-block hasn't been sent to the peer
93-
if !pws.wantBlocks.Has(c) {
94-
// Record that the CID was sent as a want-block
95-
pws.wantBlocks.Add(c)
89+
pws, ok := pwm.peerWants[p]
90+
91+
if !ok {
92+
// In practice this should never happen:
93+
// - PeerManager calls addPeer() as soon as the peer connects
94+
// - PeerManager calls removePeer() as soon as the peer disconnects
95+
// - All calls to PeerWantManager are locked
96+
log.Errorf("prepareSendWants() called with peer %s but peer not found in peerWantManager", string(p))
97+
return resWantBlks, resWantHvs
98+
}
9699

97-
// Add the CID to the results
98-
resWantBlks = append(resWantBlks, c)
100+
// Iterate over the requested want-blocks
101+
for _, c := range wantBlocks {
102+
// If the want-block hasn't been sent to the peer
103+
if !pws.wantBlocks.Has(c) {
104+
// Record that the CID was sent as a want-block
105+
pws.wantBlocks.Add(c)
99106

100-
// Make sure the CID is no longer recorded as a want-have
101-
pws.wantHaves.Remove(c)
107+
// Add the CID to the results
108+
resWantBlks = append(resWantBlks, c)
102109

103-
// Increment the count of want-blocks
104-
pwm.wantBlockGauge.Inc()
105-
}
110+
// Make sure the CID is no longer recorded as a want-have
111+
pws.wantHaves.Remove(c)
112+
113+
// Increment the count of want-blocks
114+
pwm.wantBlockGauge.Inc()
106115
}
116+
}
107117

108-
// Iterate over the requested want-haves
109-
for _, c := range wantHaves {
110-
// If the CID has not been sent as a want-block or want-have
111-
if !pws.wantBlocks.Has(c) && !pws.wantHaves.Has(c) {
112-
// Record that the CID was sent as a want-have
113-
pws.wantHaves.Add(c)
118+
// Iterate over the requested want-haves
119+
for _, c := range wantHaves {
120+
// If the CID has not been sent as a want-block or want-have
121+
if !pws.wantBlocks.Has(c) && !pws.wantHaves.Has(c) {
122+
// Record that the CID was sent as a want-have
123+
pws.wantHaves.Add(c)
114124

115-
// Add the CID to the results
116-
resWantHvs = append(resWantHvs, c)
117-
}
125+
// Add the CID to the results
126+
resWantHvs = append(resWantHvs, c)
118127
}
119128
}
120129

0 commit comments

Comments
 (0)