Skip to content

Commit

Permalink
replace unneeded RWMutex with Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Oct 18, 2024
1 parent d8be537 commit d57c555
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bitswap/client/internal/peermanager/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
6 changes: 4 additions & 2 deletions bitswap/client/internal/peermanager/peerwantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions bitswap/client/internal/session/sessionwants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 8 additions & 10 deletions bitswap/client/internal/session/sessionwantsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
}
Expand Down
8 changes: 4 additions & 4 deletions bitswap/client/internal/sessionmanager/sessionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type SessionManager struct {
notif notifications.PubSub

// Sessions
sessLk sync.RWMutex
sessLk sync.Mutex
sessions map[uint64]Session

// Session Index
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d57c555

Please sign in to comment.