diff --git a/bitswap/client/internal/peermanager/peermanager.go b/bitswap/client/internal/peermanager/peermanager.go index 25cdd605f..78a1d4c88 100644 --- a/bitswap/client/internal/peermanager/peermanager.go +++ b/bitswap/client/internal/peermanager/peermanager.go @@ -42,7 +42,7 @@ type PeerManager struct { createPeerQueue PeerQueueFactory ctx context.Context - psLk sync.RWMutex + psLk sync.Mutex sessions map[uint64]Session peerSessions map[peer.ID]map[uint64]struct{} diff --git a/bitswap/client/internal/peermanager/peerwantmanager.go b/bitswap/client/internal/peermanager/peerwantmanager.go index 0bc4732ca..dc4d22968 100644 --- a/bitswap/client/internal/peermanager/peerwantmanager.go +++ b/bitswap/client/internal/peermanager/peerwantmanager.go @@ -158,8 +158,6 @@ func (pwm *peerWantManager) broadcastWantHaves(wantHaves []cid.Cid) { // sendWants only sends the peer the want-blocks and want-haves that have not // already been sent to it. func (pwm *peerWantManager) sendWants(p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) { - fltWantBlks := make([]cid.Cid, 0, len(wantBlocks)) - fltWantHvs := make([]cid.Cid, 0, len(wantHaves)) // Get the existing want-blocks and want-haves for the peer pws, ok := pwm.peerWants[p] @@ -169,6 +167,8 @@ func (pwm *peerWantManager) sendWants(p peer.ID, wantBlocks []cid.Cid, wantHaves return } + fltWantBlks := make([]cid.Cid, 0, len(wantBlocks)) + // Iterate over the requested want-blocks for _, c := range wantBlocks { // If the want-block hasn't been sent to the peer @@ -198,6 +198,8 @@ func (pwm *peerWantManager) sendWants(p peer.ID, wantBlocks []cid.Cid, wantHaves pwm.reverseIndexAdd(c, p) } + fltWantHvs := make([]cid.Cid, 0, len(wantHaves)) + // Iterate over the requested want-haves for _, c := range wantHaves { // If we've already broadcasted this want, don't bother with a diff --git a/bitswap/client/internal/session/sessionwants.go b/bitswap/client/internal/session/sessionwants.go index 8906fef66..1368e7cdb 100644 --- a/bitswap/client/internal/session/sessionwants.go +++ b/bitswap/client/internal/session/sessionwants.go @@ -159,11 +159,9 @@ func (sw *sessionWants) CancelPending(keys []cid.Cid) { // LiveWants returns a list of live wants func (sw *sessionWants) LiveWants() []cid.Cid { - live := make([]cid.Cid, len(sw.liveWants)) - var i int + live := make([]cid.Cid, 0, len(sw.liveWants)) for c := range sw.liveWants { - live[i] = c - i++ + live = append(live, c) } return live @@ -188,7 +186,7 @@ func (sw *sessionWants) RandomLiveWant() cid.Cid { // Has live wants indicates if there are any live wants func (sw *sessionWants) HasLiveWants() bool { - return len(sw.liveWants) != 0 + return len(sw.liveWants) > 0 } // Indicates whether the want is in either of the fetch or live queues diff --git a/bitswap/client/internal/session/sessionwantsender.go b/bitswap/client/internal/session/sessionwantsender.go index 9e50771cc..338150ec3 100644 --- a/bitswap/client/internal/session/sessionwantsender.go +++ b/bitswap/client/internal/session/sessionwantsender.go @@ -269,7 +269,7 @@ func (sws *sessionWantSender) onChange(changes []change) { if chng.update.from != "" { // If the update includes blocks or haves, treat it as signaling that // the peer is available - if len(chng.update.ks) != 0 || len(chng.update.haves) != 0 { + if len(chng.update.ks) > 0 || len(chng.update.haves) > 0 { p := chng.update.from availability[p] = true @@ -295,7 +295,7 @@ func (sws *sessionWantSender) onChange(changes []change) { sws.checkForExhaustedWants(dontHaves, newlyUnavailable) // If there are any cancels, send them - if len(cancels) != 0 { + if len(cancels) > 0 { sws.canceller.CancelSessionWants(sws.sessionID, cancels) } @@ -449,7 +449,7 @@ func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid { } } } - if len(prunePeers) != 0 { + if len(prunePeers) > 0 { go func() { for p := range prunePeers { // Peer doesn't have anything we want, so remove it @@ -477,13 +477,11 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU // If a peer just became unavailable, then we need to check all wants // (because it may be the last peer who hadn't sent a DONT_HAVE for a CID) - if len(newlyUnavailable) != 0 { + if len(newlyUnavailable) > 0 { // Collect all pending wants - wants = make([]cid.Cid, len(sws.wants)) - var i int + wants = make([]cid.Cid, 0, len(sws.wants)) for c := range sws.wants { - wants[i] = c - i++ + wants = append(wants, c) } // If the last available peer in the session has become unavailable @@ -496,7 +494,7 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU // If all available peers for a cid sent a DONT_HAVE, signal to the session // that we've exhausted available peers - if len(wants) != 0 { + if len(wants) > 0 { exhausted := sws.bpm.AllPeersDoNotHaveBlock(sws.spm.Peers(), wants) sws.processExhaustedWants(exhausted) } @@ -506,7 +504,7 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU // already been marked as exhausted are passed to onPeersExhausted() func (sws *sessionWantSender) processExhaustedWants(exhausted []cid.Cid) { newlyExhausted := sws.newlyExhausted(exhausted) - if len(newlyExhausted) != 0 { + if len(newlyExhausted) > 0 { sws.onPeersExhausted(newlyExhausted) } } diff --git a/bitswap/client/internal/sessionmanager/sessionmanager.go b/bitswap/client/internal/sessionmanager/sessionmanager.go index a75a3f769..0d2b24330 100644 --- a/bitswap/client/internal/sessionmanager/sessionmanager.go +++ b/bitswap/client/internal/sessionmanager/sessionmanager.go @@ -57,7 +57,7 @@ type SessionManager struct { notif notifications.PubSub // Sessions - sessLk sync.RWMutex + sessLk sync.Mutex sessions map[uint64]Session // Session Index @@ -159,13 +159,13 @@ func (sm *SessionManager) ReceiveFrom(ctx context.Context, p peer.ID, blks []cid // Notify each session that is interested in the blocks / HAVEs / DONT_HAVEs for _, id := range sm.sessionInterestManager.InterestedSessions(blks, haves, dontHaves) { - sm.sessLk.RLock() + sm.sessLk.Lock() if sm.sessions == nil { // check if SessionManager was shutdown - sm.sessLk.RUnlock() + sm.sessLk.Unlock() return } sess, ok := sm.sessions[id] - sm.sessLk.RUnlock() + sm.sessLk.Unlock() if ok { sess.ReceiveFrom(p, blks, haves, dontHaves)