hvpn-node3/peer_meta.go
HeshamTB 7d1a0cdbdc
feat: grace period for new peers as a new evection policy
Signed-off-by: HeshamTB <hishaminv@gmail.com>
2024-03-28 01:22:29 +03:00

55 lines
986 B
Go

package hvpnnode3
import (
"sync"
"time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// Contians data about peers
type PeerData struct {
addedOn time.Time
deleted bool
}
// Basic implementaion to track peers based on public keys. Thraed safe.
type PeerMeta struct {
data map[wgtypes.Key]PeerData
rwl *sync.RWMutex
}
func NewPeerMeta() *PeerMeta {
meta := new(PeerMeta)
meta.data = make(map[wgtypes.Key]PeerData)
meta.rwl = new(sync.RWMutex)
return meta
}
func (m *PeerMeta) AddPeer(publicKey wgtypes.Key) {
m.rwl.Lock()
m.data[publicKey] = PeerData{
addedOn: time.Now().UTC(),
deleted: false,
}
m.rwl.Unlock()
}
func (m *PeerMeta) DeletePeer(publicKey wgtypes.Key) {
m.rwl.Lock()
d := m.data[publicKey]
d.deleted = true
m.rwl.Unlock()
}
func (m *PeerMeta) TimeAdded(publicKey wgtypes.Key) time.Time {
m.rwl.RLock()
t := m.data[publicKey].addedOn
m.rwl.RUnlock()
return t
}