Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bitswap/peermanager] take read-lock for read-only operation #755

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions 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.Mutex
psLk sync.RWMutex
sessions map[uint64]Session
peerSessions map[peer.ID]map[uint64]struct{}

Expand Down Expand Up @@ -121,9 +121,9 @@ func (pm *PeerManager) Disconnected(p peer.ID) {
// ks is the set of blocks, HAVEs and DONT_HAVEs in the message
// Note that this is just used to calculate latency.
func (pm *PeerManager) ResponseReceived(p peer.ID, ks []cid.Cid) {
pm.pqLk.Lock()
pm.pqLk.RLock()
pq, ok := pm.peerQueues[p]
pm.pqLk.Unlock()
pm.pqLk.RUnlock()

if ok {
pq.ResponseReceived(ks)
Expand Down Expand Up @@ -233,8 +233,8 @@ func (pm *PeerManager) UnregisterSession(ses uint64) {
// signalAvailability is called when a peer's connectivity changes.
// It informs interested sessions.
func (pm *PeerManager) signalAvailability(p peer.ID, isConnected bool) {
pm.psLk.Lock()
defer pm.psLk.Unlock()
pm.psLk.RLock()
defer pm.psLk.RUnlock()

sesIds, ok := pm.peerSessions[p]
if !ok {
Expand Down
7 changes: 1 addition & 6 deletions bitswap/client/internal/session/sessionwantsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,7 @@
case sws.changes <- c:
default:
// changes channel is full, so add change in a go routine instead
go func() {
select {
case sws.changes <- c:
case <-sws.ctx.Done():
}
}()
go sws.addChange(c)

Check warning on line 223 in bitswap/client/internal/session/sessionwantsender.go

View check run for this annotation

Codecov / codecov/patch

bitswap/client/internal/session/sessionwantsender.go#L223

Added line #L223 was not covered by tests
}
}

Expand Down
Loading