2021-08-29 17:48:31 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2021-08-29 17:48:31 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
channelsMux *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeersUpdateManager returns a new instance of PeersUpdateManager
|
|
|
|
func NewPeersUpdateManager() *PeersUpdateManager {
|
|
|
|
return &PeersUpdateManager{
|
|
|
|
peerChannels: make(map[string]chan *UpdateMessage),
|
|
|
|
channelsMux: &sync.Mutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendUpdate sends update message to the peer's channel
|
2023-02-03 10:33:28 +01:00
|
|
|
func (p *PeersUpdateManager) SendUpdate(peerID string, update *UpdateMessage) error {
|
2021-08-29 17:48:31 +02:00
|
|
|
p.channelsMux.Lock()
|
|
|
|
defer p.channelsMux.Unlock()
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
2022-08-27 12:57:03 +02:00
|
|
|
select {
|
|
|
|
case channel <- update:
|
2023-02-03 10:33:28 +01:00
|
|
|
log.Infof("update was sent to channel for peer %s", peerID)
|
2022-08-27 12:57:03 +02:00
|
|
|
default:
|
2023-02-03 10:33:28 +01:00
|
|
|
log.Warnf("channel for peer %s is %d full", peerID, len(channel))
|
2022-08-27 12:57:03 +02:00
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
log.Debugf("peer %s has no channel", peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateChannel creates a go channel for a given peer used to deliver updates relevant to the peer.
|
2023-02-03 10:33:28 +01:00
|
|
|
func (p *PeersUpdateManager) CreateChannel(peerID string) chan *UpdateMessage {
|
2021-08-29 17:48:31 +02:00
|
|
|
p.channelsMux.Lock()
|
|
|
|
defer p.channelsMux.Unlock()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
//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
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
log.Debugf("opened updates channel for a peer %s", peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
func (p *PeersUpdateManager) closeChannel(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)
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
log.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
|
|
|
|
func (p *PeersUpdateManager) CloseChannels(peerIDs []string) {
|
|
|
|
p.channelsMux.Lock()
|
|
|
|
defer p.channelsMux.Unlock()
|
|
|
|
for _, id := range peerIDs {
|
|
|
|
p.closeChannel(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseChannel closes updates channel of a given peer
|
|
|
|
func (p *PeersUpdateManager) CloseChannel(peerID string) {
|
|
|
|
p.channelsMux.Lock()
|
|
|
|
defer p.channelsMux.Unlock()
|
|
|
|
p.closeChannel(peerID)
|
|
|
|
}
|
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
// GetAllConnectedPeers returns a copy of the connected peers map
|
|
|
|
func (p *PeersUpdateManager) GetAllConnectedPeers() map[string]struct{} {
|
|
|
|
p.channelsMux.Lock()
|
|
|
|
defer p.channelsMux.Unlock()
|
|
|
|
m := make(map[string]struct{})
|
2023-02-03 10:33:28 +01:00
|
|
|
for ID := range p.peerChannels {
|
|
|
|
m[ID] = struct{}{}
|
2022-10-16 13:33:46 +02:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|