Use sync.Map in PeersUpdateManager

This commit is contained in:
Yury Gargay
2023-09-18 17:53:21 +02:00
parent 1324169ebb
commit 025fefc6bd
3 changed files with 65 additions and 40 deletions

View File

@@ -61,7 +61,7 @@ func NewServer(config *Config, accountManager AccountManager, peersUpdateManager
if appMetrics != nil { if appMetrics != nil {
// update gauge based on number of connected peers which is equal to open gRPC streams // update gauge based on number of connected peers which is equal to open gRPC streams
err = appMetrics.GRPCMetrics().RegisterConnectedStreams(func() int64 { err = appMetrics.GRPCMetrics().RegisterConnectedStreams(func() int64 {
return int64(len(peersUpdateManager.peerChannels)) return peersUpdateManager.Len()
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -1,6 +1,7 @@
package server package server
import ( import (
"fmt"
"sync" "sync"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -14,25 +15,25 @@ type UpdateMessage struct {
Update *proto.SyncResponse Update *proto.SyncResponse
} }
type UpdateChannel chan *UpdateMessage
type PeersUpdateManager struct { type PeersUpdateManager struct {
// peerChannels is an update channel indexed by Peer.ID // peerChannels is an update channel indexed by Peer.ID
peerChannels map[string]chan *UpdateMessage peerChannels sync.Map
channelsMux *sync.Mutex
} }
// NewPeersUpdateManager returns a new instance of PeersUpdateManager // NewPeersUpdateManager returns a new instance of PeersUpdateManager
func NewPeersUpdateManager() *PeersUpdateManager { func NewPeersUpdateManager() *PeersUpdateManager {
return &PeersUpdateManager{ return &PeersUpdateManager{}
peerChannels: make(map[string]chan *UpdateMessage),
channelsMux: &sync.Mutex{},
}
} }
// SendUpdate sends update message to the peer's channel // SendUpdate sends update message to the peer's channel
func (p *PeersUpdateManager) SendUpdate(peerID string, update *UpdateMessage) error { func (p *PeersUpdateManager) SendUpdate(peerID string, update *UpdateMessage) error {
p.channelsMux.Lock() if ch, ok := p.peerChannels.Load(peerID); ok {
defer p.channelsMux.Unlock() channel, ok := ch.(UpdateChannel)
if channel, ok := p.peerChannels[peerID]; ok { if !ok {
return fmt.Errorf("could not cast to UpdateChannel")
}
select { select {
case channel <- update: case channel <- update:
log.Debugf("update was sent to channel for peer %s", peerID) log.Debugf("update was sent to channel for peer %s", peerID)
@@ -46,35 +47,38 @@ func (p *PeersUpdateManager) SendUpdate(peerID string, update *UpdateMessage) er
} }
// CreateChannel creates a go channel for a given peer used to deliver updates relevant to the peer. // CreateChannel creates a go channel for a given peer used to deliver updates relevant to the peer.
func (p *PeersUpdateManager) CreateChannel(peerID string) chan *UpdateMessage { func (p *PeersUpdateManager) CreateChannel(peerID string) UpdateChannel {
p.channelsMux.Lock() p.closeChannel(peerID)
defer p.channelsMux.Unlock()
if channel, ok := p.peerChannels[peerID]; ok {
delete(p.peerChannels, peerID)
close(channel)
}
// mbragin: todo shouldn't it be more? or configurable? // mbragin: todo shouldn't it be more? or configurable?
channel := make(chan *UpdateMessage, channelBufferSize) channel := make(UpdateChannel, channelBufferSize)
p.peerChannels[peerID] = channel p.peerChannels.Store(peerID, channel)
log.Debugf("opened updates channel for a peer %s", peerID) log.Debugf("opened updates channel for a peer %s", peerID)
return channel return channel
} }
func (p *PeersUpdateManager) closeChannel(peerID string) { func (p *PeersUpdateManager) GetChannel(peerID string) UpdateChannel {
if channel, ok := p.peerChannels[peerID]; ok { if ch, ok := p.peerChannels.Load(peerID); ok {
delete(p.peerChannels, peerID) channel := ch.(UpdateChannel)
close(channel) return channel
} }
return nil
}
log.Debugf("closed updates channel of a peer %s", peerID) func (p *PeersUpdateManager) closeChannel(peerID string) {
if ch, ok := p.peerChannels.LoadAndDelete(peerID); ok {
channel, ok := ch.(UpdateChannel)
if !ok {
log.Errorf("could not cast to chan *UpdateMessage")
}
close(channel)
log.Debugf("closed updates channel of a peer %s", peerID)
}
} }
// CloseChannels closes updates channel for each given peer // CloseChannels closes updates channel for each given peer
func (p *PeersUpdateManager) CloseChannels(peerIDs []string) { func (p *PeersUpdateManager) CloseChannels(peerIDs []string) {
p.channelsMux.Lock()
defer p.channelsMux.Unlock()
for _, id := range peerIDs { for _, id := range peerIDs {
p.closeChannel(id) p.closeChannel(id)
} }
@@ -82,18 +86,26 @@ func (p *PeersUpdateManager) CloseChannels(peerIDs []string) {
// CloseChannel closes updates channel of a given peer // CloseChannel closes updates channel of a given peer
func (p *PeersUpdateManager) CloseChannel(peerID string) { func (p *PeersUpdateManager) CloseChannel(peerID string) {
p.channelsMux.Lock()
defer p.channelsMux.Unlock()
p.closeChannel(peerID) p.closeChannel(peerID)
} }
// GetAllConnectedPeers returns a copy of the connected peers map // GetAllConnectedPeers returns a copy of the connected peers map
func (p *PeersUpdateManager) GetAllConnectedPeers() map[string]struct{} { func (p *PeersUpdateManager) GetAllConnectedPeers() map[string]struct{} {
p.channelsMux.Lock()
defer p.channelsMux.Unlock()
m := make(map[string]struct{}) m := make(map[string]struct{})
for ID := range p.peerChannels { p.peerChannels.Range(func(key any, value any) bool {
m[ID] = struct{}{} if ID, ok := key.(string); ok {
} m[ID] = struct{}{}
}
return true
})
return m return m
} }
// Len returns the length of the peer channels
func (p *PeersUpdateManager) Len() (len int64) {
p.peerChannels.Range(func(key any, value any) bool {
len++
return true
})
return len
}

View File

@@ -1,9 +1,10 @@
package server package server
import ( import (
"github.com/netbirdio/netbird/management/proto"
"testing" "testing"
"time" "time"
"github.com/netbirdio/netbird/management/proto"
) )
//var peersUpdater *PeersUpdateManager //var peersUpdater *PeersUpdateManager
@@ -13,10 +14,22 @@ func TestCreateChannel(t *testing.T) {
peersUpdater := NewPeersUpdateManager() peersUpdater := NewPeersUpdateManager()
defer peersUpdater.CloseChannel(peer) defer peersUpdater.CloseChannel(peer)
if peersUpdater.Len() != 0 {
t.Error("peersUpdated should not have any channels yet")
}
if ch := peersUpdater.GetChannel(peer); ch != nil {
t.Errorf("We should not have channel for %s yet", peer)
}
_ = peersUpdater.CreateChannel(peer) _ = peersUpdater.CreateChannel(peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok { if ch := peersUpdater.GetChannel(peer); ch == nil {
t.Error("Error creating the channel") t.Error("Error creating the channel")
} }
if peersUpdater.Len() != 1 {
t.Error("peersUpdated should have 1 channel")
}
} }
func TestSendUpdate(t *testing.T) { func TestSendUpdate(t *testing.T) {
@@ -28,7 +41,7 @@ func TestSendUpdate(t *testing.T) {
}, },
}} }}
_ = peersUpdater.CreateChannel(peer) _ = peersUpdater.CreateChannel(peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok { if ch := peersUpdater.GetChannel(peer); ch == nil {
t.Error("Error creating the channel") t.Error("Error creating the channel")
} }
err := peersUpdater.SendUpdate(peer, update1) err := peersUpdater.SendUpdate(peer, update1)
@@ -36,7 +49,7 @@ func TestSendUpdate(t *testing.T) {
t.Error("Error sending update: ", err) t.Error("Error sending update: ", err)
} }
select { select {
case <-peersUpdater.peerChannels[peer]: case <-peersUpdater.GetChannel(peer):
default: default:
t.Error("Update wasn't send") t.Error("Update wasn't send")
} }
@@ -63,7 +76,7 @@ func TestSendUpdate(t *testing.T) {
select { select {
case <-timeout: case <-timeout:
t.Error("timed out reading previously sent updates") t.Error("timed out reading previously sent updates")
case updateReader := <-peersUpdater.peerChannels[peer]: case updateReader := <-peersUpdater.GetChannel(peer):
if updateReader.Update.NetworkMap.Serial == update2.Update.NetworkMap.Serial { if updateReader.Update.NetworkMap.Serial == update2.Update.NetworkMap.Serial {
t.Error("got the update that shouldn't have been sent") t.Error("got the update that shouldn't have been sent")
} }
@@ -76,11 +89,11 @@ func TestCloseChannel(t *testing.T) {
peer := "test-close" peer := "test-close"
peersUpdater := NewPeersUpdateManager() peersUpdater := NewPeersUpdateManager()
_ = peersUpdater.CreateChannel(peer) _ = peersUpdater.CreateChannel(peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok { if ch := peersUpdater.GetChannel(peer); ch == nil {
t.Error("Error creating the channel") t.Error("Error creating the channel")
} }
peersUpdater.CloseChannel(peer) peersUpdater.CloseChannel(peer)
if _, ok := peersUpdater.peerChannels[peer]; ok { if ch := peersUpdater.GetChannel(peer); ch != nil {
t.Error("Error closing the channel") t.Error("Error closing the channel")
} }
} }