mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-28 19:13:31 +01:00
97e10e440c
Fix leaked server connections close unused connections in the client lib close deprecated connection in the server lib The Server Picker is reusable in the guard if we want in the future. So we can support the server address changes. --------- Co-authored-by: Maycon Santos <mlsmaycon@gmail.com> * Add logging --------- Co-authored-by: Maycon Santos <mlsmaycon@gmail.com>
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// Store is a thread-safe store of peers
|
|
// It is used to store the peers that are connected to the relay server
|
|
type Store struct {
|
|
peers map[string]*Peer // consider to use [32]byte as key. The Peer(id string) would be faster
|
|
peersLock sync.RWMutex
|
|
}
|
|
|
|
// NewStore creates a new Store instance
|
|
func NewStore() *Store {
|
|
return &Store{
|
|
peers: make(map[string]*Peer),
|
|
}
|
|
}
|
|
|
|
// AddPeer adds a peer to the store
|
|
func (s *Store) AddPeer(peer *Peer) {
|
|
s.peersLock.Lock()
|
|
defer s.peersLock.Unlock()
|
|
odlPeer, ok := s.peers[peer.String()]
|
|
if ok {
|
|
odlPeer.Close()
|
|
}
|
|
|
|
s.peers[peer.String()] = peer
|
|
}
|
|
|
|
// DeletePeer deletes a peer from the store
|
|
func (s *Store) DeletePeer(peer *Peer) {
|
|
s.peersLock.Lock()
|
|
defer s.peersLock.Unlock()
|
|
|
|
dp, ok := s.peers[peer.String()]
|
|
if !ok {
|
|
return
|
|
}
|
|
if dp != peer {
|
|
return
|
|
}
|
|
|
|
delete(s.peers, peer.String())
|
|
}
|
|
|
|
// Peer returns a peer by its ID
|
|
func (s *Store) Peer(id string) (*Peer, bool) {
|
|
s.peersLock.RLock()
|
|
defer s.peersLock.RUnlock()
|
|
|
|
p, ok := s.peers[id]
|
|
return p, ok
|
|
}
|
|
|
|
// Peers returns all the peers in the store
|
|
func (s *Store) Peers() []*Peer {
|
|
s.peersLock.RLock()
|
|
defer s.peersLock.RUnlock()
|
|
|
|
peers := make([]*Peer, 0, len(s.peers))
|
|
for _, p := range s.peers {
|
|
peers = append(peers, p)
|
|
}
|
|
return peers
|
|
}
|