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

Commit

Permalink
Merge pull request #325 from ipfs/fix/pmgr-log
Browse files Browse the repository at this point in the history
fix: log unexpected condition in peerWantManager.prepareSendWants()
  • Loading branch information
Stebalien authored Mar 24, 2020
2 parents 7348b26 + fd0e1ff commit 93801a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
3 changes: 3 additions & 0 deletions internal/peermanager/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"
"sync"

logging "github.com/ipfs/go-log"
"github.com/ipfs/go-metrics-interface"

cid "github.com/ipfs/go-cid"
peer "github.com/libp2p/go-libp2p-core/peer"
)

var log = logging.Logger("bs:peermgr")

// PeerQueue provides a queue of messages to be sent for a single peer.
type PeerQueue interface {
AddBroadcastWantHaves([]cid.Cid)
Expand Down
55 changes: 32 additions & 23 deletions internal/peermanager/peerwantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,44 @@ func (pwm *peerWantManager) prepareSendWants(p peer.ID, wantBlocks []cid.Cid, wa
resWantHvs := make([]cid.Cid, 0)

// Get the existing want-blocks and want-haves for the peer
if pws, ok := pwm.peerWants[p]; ok {
// Iterate over the requested want-blocks
for _, c := range wantBlocks {
// If the want-block hasn't been sent to the peer
if !pws.wantBlocks.Has(c) {
// Record that the CID was sent as a want-block
pws.wantBlocks.Add(c)
pws, ok := pwm.peerWants[p]

if !ok {
// In practice this should never happen:
// - PeerManager calls addPeer() as soon as the peer connects
// - PeerManager calls removePeer() as soon as the peer disconnects
// - All calls to PeerWantManager are locked
log.Errorf("prepareSendWants() called with peer %s but peer not found in peerWantManager", string(p))
return resWantBlks, resWantHvs
}

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

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

// Increment the count of want-blocks
pwm.wantBlockGauge.Inc()
}
// Make sure the CID is no longer recorded as a want-have
pws.wantHaves.Remove(c)

// Increment the count of want-blocks
pwm.wantBlockGauge.Inc()
}
}

// Iterate over the requested want-haves
for _, c := range wantHaves {
// If the CID has not been sent as a want-block or want-have
if !pws.wantBlocks.Has(c) && !pws.wantHaves.Has(c) {
// Record that the CID was sent as a want-have
pws.wantHaves.Add(c)
// Iterate over the requested want-haves
for _, c := range wantHaves {
// If the CID has not been sent as a want-block or want-have
if !pws.wantBlocks.Has(c) && !pws.wantHaves.Has(c) {
// Record that the CID was sent as a want-have
pws.wantHaves.Add(c)

// Add the CID to the results
resWantHvs = append(resWantHvs, c)
}
// Add the CID to the results
resWantHvs = append(resWantHvs, c)
}
}

Expand Down

0 comments on commit 93801a7

Please sign in to comment.