From 6d129c7f68db608d69facc4d300c691e34289d64 Mon Sep 17 00:00:00 2001
From: Angelo Capossele <angelocapossele@gmail.com>
Date: Fri, 17 Jul 2020 09:09:26 +0100
Subject: [PATCH]  Make gossip mutex RW (#649)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* ⚡️ Make gossip mutex RW

* ⚡️ Make getNeighborsByID to use mutex in read mode
---
 packages/gossip/manager.go | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/packages/gossip/manager.go b/packages/gossip/manager.go
index 8ee8f0eb..e4c5e79a 100644
--- a/packages/gossip/manager.go
+++ b/packages/gossip/manager.go
@@ -32,7 +32,7 @@ type Manager struct {
 
 	wg sync.WaitGroup
 
-	mu        sync.Mutex
+	mu        sync.RWMutex
 	srv       *server.TCP
 	neighbors map[identity.ID]*Neighbor
 
@@ -149,8 +149,8 @@ func (m *Manager) SendMessage(msgData []byte, to ...identity.ID) {
 
 // AllNeighbors returns all the neighbors that are currently connected.
 func (m *Manager) AllNeighbors() []*Neighbor {
-	m.mu.Lock()
-	defer m.mu.Unlock()
+	m.mu.RLock()
+	defer m.mu.RUnlock()
 
 	result := make([]*Neighbor, 0, len(m.neighbors))
 	for _, n := range m.neighbors {
@@ -169,8 +169,8 @@ func (m *Manager) getNeighbors(ids ...identity.ID) []*Neighbor {
 func (m *Manager) getNeighborsByID(ids []identity.ID) []*Neighbor {
 	result := make([]*Neighbor, 0, len(ids))
 
-	m.mu.Lock()
-	defer m.mu.Unlock()
+	m.mu.RLock()
+	defer m.mu.RUnlock()
 
 	for _, id := range ids {
 		if n, ok := m.neighbors[id]; ok {
-- 
GitLab