Skip to content

Commit

Permalink
fix lock
Browse files Browse the repository at this point in the history
  • Loading branch information
dvovk committed Jul 13, 2024
1 parent acb8ade commit 25367d7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions erigon-lib/diagnostics/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ func NewPeerStats(peerLimit int) *PeerStats {
}

func (p *PeerStats) AddOrUpdatePeer(peerID string, peerInfo PeerStatisticMsgUpdate) {
p.mu.Lock()
defer p.mu.Unlock()
if value, ok := p.peersInfo.Load(peerID); ok {
p.updatePeer(peerID, peerInfo, value)
} else {
Expand Down Expand Up @@ -195,7 +193,10 @@ type PeerUpdTime struct {
func (p *PeerStats) GetOldestUpdatedPeersWithSize(size int) []PeerUpdTime {
p.mu.Lock()
defer p.mu.Unlock()
return p.getOldestUpdatedPeersWithSize(size)
}

func (p *PeerStats) getOldestUpdatedPeersWithSize(size int) []PeerUpdTime {
timeArray := make([]PeerUpdTime, 0, p.getPeersCount())
for k, v := range p.lastUpdateMap {
timeArray = append(timeArray, PeerUpdTime{k, v})
Expand All @@ -221,7 +222,7 @@ func (p *PeerStats) RemovePeersWhichExceedLimit(limit int) {
func (p *PeerStats) removePeersWhichExceedLimit(limit int) {
peersToRemove := p.getPeersCount() - limit
if peersToRemove > 0 {
peers := p.GetOldestUpdatedPeersWithSize(peersToRemove)
peers := p.getOldestUpdatedPeersWithSize(peersToRemove)
for _, peer := range peers {
p.RemovePeer(peer.PeerID)
}
Expand Down
5 changes: 3 additions & 2 deletions erigon-lib/diagnostics/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,14 @@ func TestRemovePeersWhichExceedLimit(t *testing.T) {
pid := "test" + strconv.Itoa(i)
peerStats.AddOrUpdatePeer(pid, mockInboundUpdMsg)
}
require.Equal(t, 100, peerStats.GetPeersCount())

//peerStats.RemovePeersWhichExceedLimit(limit)
peerStats.RemovePeersWhichExceedLimit(limit)

require.Equal(t, limit, peerStats.GetPeersCount())

limit = 1000
//peerStats.RemovePeersWhichExceedLimit(limit)
peerStats.RemovePeersWhichExceedLimit(limit)

require.Equal(t, 100, peerStats.GetPeersCount())
}
Expand Down

0 comments on commit 25367d7

Please sign in to comment.