netbird/relay/server/store.go

48 lines
846 B
Go
Raw Normal View History

2024-05-17 17:43:28 +02:00
package server
import (
"sync"
)
type Store struct {
2024-05-23 13:24:02 +02:00
peers map[string]*Peer // consider to use [32]byte as key. The Peer(id string) would be faster
peersLock sync.RWMutex
2024-05-17 17:43:28 +02:00
}
func NewStore() *Store {
return &Store{
peers: make(map[string]*Peer),
}
}
func (s *Store) AddPeer(peer *Peer) {
s.peersLock.Lock()
defer s.peersLock.Unlock()
2024-05-23 13:24:02 +02:00
s.peers[peer.String()] = peer
2024-05-17 17:43:28 +02:00
}
2024-05-23 13:24:02 +02:00
func (s *Store) DeletePeer(peer *Peer) {
2024-05-17 17:43:28 +02:00
s.peersLock.Lock()
defer s.peersLock.Unlock()
2024-05-23 13:24:02 +02:00
delete(s.peers, peer.String())
2024-05-17 17:43:28 +02:00
}
2024-05-23 13:24:02 +02:00
func (s *Store) Peer(id string) (*Peer, bool) {
s.peersLock.RLock()
defer s.peersLock.RUnlock()
2024-05-17 17:43:28 +02:00
2024-05-23 13:24:02 +02:00
p, ok := s.peers[id]
return p, ok
2024-05-17 17:43:28 +02:00
}
2024-06-05 19:49:30 +02:00
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
}