fix peer status persistence, update status every second

This commit is contained in:
Pavel Sviderski
2024-10-07 15:35:15 +10:00
parent 7b98ed34df
commit 2c0cf28509
2 changed files with 28 additions and 8 deletions
+7 -2
View File
@@ -2,6 +2,7 @@ package network
import ( import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"log/slog"
"time" "time"
) )
@@ -20,8 +21,8 @@ type peer struct {
status string status string
} }
func newPeer(config PeerConfig) peer { func newPeer(config PeerConfig) *peer {
p := peer{ p := &peer{
config: config, config: config,
status: PeerStatusUnknown, status: PeerStatusUnknown,
} }
@@ -83,6 +84,7 @@ const peerDownInterval = (180 + 5 + 90) * time.Second
// - if we're between (T0) and (T0+endpointConnectionTimeout), and there's no handshake since the endpoint change, // - if we're between (T0) and (T0+endpointConnectionTimeout), and there's no handshake since the endpoint change,
// consider the state to be unknown // consider the state to be unknown
func (p *peer) calculateStatus() { func (p *peer) calculateStatus() {
lastStatus := p.status
sinceLastHandshake := time.Since(p.lastHandshakeTime) sinceLastHandshake := time.Since(p.lastHandshakeTime)
sinceEndpointChange := time.Since(p.lastEndpointChangeTime) sinceEndpointChange := time.Since(p.lastEndpointChangeTime)
@@ -114,4 +116,7 @@ func (p *peer) calculateStatus() {
// No endpoint, so unknown. // No endpoint, so unknown.
p.status = PeerStatusUnknown p.status = PeerStatusUnknown
} }
if p.status != lastStatus {
slog.Debug("Peer status changed.", "public_key", p.config.PublicKey, "status", p.status)
}
} }
+21 -6
View File
@@ -14,13 +14,14 @@ import (
"net/netip" "net/netip"
"slices" "slices"
"sync" "sync"
"time"
"uncloud/internal/secret" "uncloud/internal/secret"
) )
type WireGuardNetwork struct { type WireGuardNetwork struct {
link netlink.Link link netlink.Link
// peers is a map of peers indexed by their public key. // peers is a map of peers indexed by their public key.
peers map[string]peer peers map[string]*peer
// mu synchronises concurrent network configuration changes. // mu synchronises concurrent network configuration changes.
mu sync.Mutex mu sync.Mutex
} }
@@ -32,7 +33,7 @@ func NewWireGuardNetwork() (*WireGuardNetwork, error) {
} }
return &WireGuardNetwork{ return &WireGuardNetwork{
link: link, link: link,
peers: make(map[string]peer), peers: make(map[string]*peer),
}, nil }, nil
} }
@@ -260,8 +261,22 @@ func (n *WireGuardNetwork) updatePeerRoutes() error {
} }
func (n *WireGuardNetwork) Run(ctx context.Context) error { func (n *WireGuardNetwork) Run(ctx context.Context) error {
// TODO: check if the endpoint should be changed for any peers. If so, change it and notify the controller to ticker := time.NewTicker(1 * time.Second)
// preserve the change in the machine state. for {
<-ctx.Done() select {
return nil case <-ticker.C:
n.mu.Lock()
if err := n.updatePeersFromWireGuard(); err != nil {
slog.Error("Failed to update peers status from WireGuard interface.",
"name", n.link.Attrs().Name, "err", err)
}
n.mu.Unlock()
// TODO: check if the endpoint should be changed for any peers. If so, change it and notify the controller
// to preserve the change in the machine state.
case <-ctx.Done():
return nil
}
}
} }