2021-08-29 17:48:31 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2021-08-29 17:48:31 +02:00
|
|
|
"sync"
|
2023-11-16 18:21:52 +01:00
|
|
|
"time"
|
2023-08-18 19:23:11 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2023-11-16 18:21:52 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2021-08-29 17:48:31 +02:00
|
|
|
)
|
|
|
|
|
2022-08-27 12:57:03 +02:00
|
|
|
const channelBufferSize = 100
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
type UpdateMessage struct {
|
|
|
|
Update *proto.SyncResponse
|
|
|
|
}
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
type PeersUpdateManager struct {
|
2023-02-03 10:33:28 +01:00
|
|
|
// peerChannels is an update channel indexed by Peer.ID
|
2021-08-29 17:48:31 +02:00
|
|
|
peerChannels map[string]chan *UpdateMessage
|
2023-11-16 18:21:52 +01:00
|
|
|
// channelsMux keeps the mutex to access peerChannels
|
|
|
|
channelsMux *sync.Mutex
|
|
|
|
// metrics provides method to collect application metrics
|
|
|
|
metrics telemetry.AppMetrics
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeersUpdateManager returns a new instance of PeersUpdateManager
|
2023-11-16 18:21:52 +01:00
|
|
|
func NewPeersUpdateManager(metrics telemetry.AppMetrics) *PeersUpdateManager {
|
2021-08-29 17:48:31 +02:00
|
|
|
return &PeersUpdateManager{
|
|
|
|
peerChannels: make(map[string]chan *UpdateMessage),
|
|
|
|
channelsMux: &sync.Mutex{},
|
2023-11-16 18:21:52 +01:00
|
|
|
metrics: metrics,
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendUpdate sends update message to the peer's channel
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) SendUpdate(ctx context.Context, peerID string, update *UpdateMessage) {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
var found, dropped bool
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountSendUpdateDuration(time.Since(start), found, dropped)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
2023-11-16 18:21:52 +01:00
|
|
|
found = true
|
2022-08-27 12:57:03 +02:00
|
|
|
select {
|
|
|
|
case channel <- update:
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("update was sent to channel for peer %s", peerID)
|
2022-08-27 12:57:03 +02:00
|
|
|
default:
|
2023-11-16 18:21:52 +01:00
|
|
|
dropped = true
|
2024-09-06 16:28:19 +02:00
|
|
|
log.WithContext(ctx).Warnf("channel for peer %s is %d full or closed", peerID, len(channel))
|
2022-08-27 12:57:03 +02:00
|
|
|
}
|
2023-10-03 16:46:58 +02:00
|
|
|
} else {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("peer %s has no channel", peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateChannel creates a go channel for a given peer used to deliver updates relevant to the peer.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) CreateChannel(ctx context.Context, peerID string) chan *UpdateMessage {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
closed := false
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountCreateChannelDuration(time.Since(start), closed)
|
|
|
|
}
|
|
|
|
}()
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
2023-11-16 18:21:52 +01:00
|
|
|
closed = true
|
2023-02-03 10:33:28 +01:00
|
|
|
delete(p.peerChannels, peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
close(channel)
|
|
|
|
}
|
2023-08-18 19:23:11 +02:00
|
|
|
// mbragin: todo shouldn't it be more? or configurable?
|
2022-08-27 12:57:03 +02:00
|
|
|
channel := make(chan *UpdateMessage, channelBufferSize)
|
2023-02-03 10:33:28 +01:00
|
|
|
p.peerChannels[peerID] = channel
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("opened updates channel for a peer %s", peerID)
|
2023-11-16 18:21:52 +01:00
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) closeChannel(ctx context.Context, peerID string) {
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
|
|
|
delete(p.peerChannels, peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
close(channel)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("closed updates channel of a peer %s", peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
2022-10-16 13:33:46 +02:00
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
// CloseChannels closes updates channel for each given peer
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) CloseChannels(ctx context.Context, peerIDs []string) {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountCloseChannelsDuration(time.Since(start), len(peerIDs))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
for _, id := range peerIDs {
|
2024-07-03 11:33:02 +02:00
|
|
|
p.closeChannel(ctx, id)
|
2023-02-27 16:44:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseChannel closes updates channel of a given peer
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) CloseChannel(ctx context.Context, peerID string) {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountCloseChannelDuration(time.Since(start))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
p.closeChannel(ctx, peerID)
|
2023-02-27 16:44:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
// GetAllConnectedPeers returns a copy of the connected peers map
|
|
|
|
func (p *PeersUpdateManager) GetAllConnectedPeers() map[string]struct{} {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
m := make(map[string]struct{})
|
2023-11-16 18:21:52 +01:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountGetAllConnectedPeersDuration(time.Since(start), len(m))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
for ID := range p.peerChannels {
|
|
|
|
m[ID] = struct{}{}
|
2022-10-16 13:33:46 +02:00
|
|
|
}
|
2023-11-16 18:21:52 +01:00
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
return m
|
|
|
|
}
|
2023-12-05 14:17:56 +01:00
|
|
|
|
|
|
|
// HasChannel returns true if peers has channel in update manager, otherwise false
|
|
|
|
func (p *PeersUpdateManager) HasChannel(peerID string) bool {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
p.channelsMux.Lock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountHasChannelDuration(time.Since(start))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, ok := p.peerChannels[peerID]
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|