diff --git a/bitswap/client/internal/notifications/notifications.go b/bitswap/client/internal/notifications/notifications.go index 499a61c42..cd0816161 100644 --- a/bitswap/client/internal/notifications/notifications.go +++ b/bitswap/client/internal/notifications/notifications.go @@ -69,12 +69,13 @@ func (ps *impl) Shutdown() { // corresponding to |keys|. func (ps *impl) Subscribe(ctx context.Context, keys ...cid.Cid) <-chan blocks.Block { blocksCh := make(chan blocks.Block, len(keys)) - valuesCh := make(chan interface{}, len(keys)) // provide our own channel to control buffer, prevent blocking if len(keys) == 0 { close(blocksCh) return blocksCh } + valuesCh := make(chan interface{}, len(keys)) // provide our own channel to control buffer, prevent blocking + // prevent shutdown ps.lk.RLock() defer ps.lk.RUnlock() diff --git a/bitswap/client/internal/peermanager/peerwantmanager.go b/bitswap/client/internal/peermanager/peerwantmanager.go index dc4d22968..e9fdfbb46 100644 --- a/bitswap/client/internal/peermanager/peerwantmanager.go +++ b/bitswap/client/internal/peermanager/peerwantmanager.go @@ -1,8 +1,8 @@ package peermanager import ( - "bytes" "fmt" + "strings" cid "github.com/ipfs/go-cid" peer "github.com/libp2p/go-libp2p/core/peer" @@ -452,7 +452,7 @@ func (pwm *peerWantManager) getWants() []cid.Cid { } func (pwm *peerWantManager) String() string { - var b bytes.Buffer + var b strings.Builder for p, ws := range pwm.peerWants { b.WriteString(fmt.Sprintf("Peer %s: %d want-have / %d want-block:\n", p, ws.wantHaves.Len(), ws.wantBlocks.Len())) for _, c := range ws.wantHaves.Keys() { diff --git a/bitswap/client/internal/session/peerresponsetracker.go b/bitswap/client/internal/session/peerresponsetracker.go index 25c10fe87..f96424742 100644 --- a/bitswap/client/internal/session/peerresponsetracker.go +++ b/bitswap/client/internal/session/peerresponsetracker.go @@ -31,8 +31,6 @@ func (prt *peerResponseTracker) choose(peers []peer.ID) peer.ID { return "" } - rnd := rand.Float64() - // Find the total received blocks for all candidate peers total := 0 for _, p := range peers { @@ -41,6 +39,7 @@ func (prt *peerResponseTracker) choose(peers []peer.ID) peer.ID { // Choose one of the peers with a chance proportional to the number // of blocks received from that peer + rnd := rand.Float64() counted := 0.0 for _, p := range peers { counted += float64(prt.getPeerCount(p)) / float64(total) @@ -52,8 +51,7 @@ func (prt *peerResponseTracker) choose(peers []peer.ID) peer.ID { // We shouldn't get here unless there is some weirdness with floating point // math that doesn't quite cover the whole range of peers in the for loop // so just choose the last peer. - index := len(peers) - 1 - return peers[index] + return peers[len(peers)-1] } // getPeerCount returns the number of times the peer was first to send us a diff --git a/bitswap/client/internal/session/sessionwants.go b/bitswap/client/internal/session/sessionwants.go index 1368e7cdb..97c3e7800 100644 --- a/bitswap/client/internal/session/sessionwants.go +++ b/bitswap/client/internal/session/sessionwants.go @@ -62,7 +62,7 @@ func (sw *sessionWants) GetNextWants() []cid.Cid { } live := make([]cid.Cid, 0, liveSize) - for ; toAdd != 0 && sw.toFetch.Len() != 0; toAdd-- { + for ; toAdd > 0 && sw.toFetch.Len() > 0; toAdd-- { c := sw.toFetch.Pop() live = append(live, c) sw.liveWantsOrder = append(sw.liveWantsOrder, c)