Merge branch 'debug-slow-conn' into merged-fixes

This commit is contained in:
Zoltán Papp
2025-09-04 13:54:13 +02:00
5 changed files with 44 additions and 5 deletions

View File

@@ -33,6 +33,7 @@ type WGWatcher struct {
ctx context.Context
ctxCancel context.CancelFunc
ctxLock sync.Mutex
enabled time.Time
}
func NewWGWatcher(log *log.Entry, wgIfaceStater WGInterfaceStater, peerKey string, stateDump *stateDump) *WGWatcher {
@@ -48,6 +49,7 @@ func NewWGWatcher(log *log.Entry, wgIfaceStater WGInterfaceStater, peerKey strin
func (w *WGWatcher) EnableWgWatcher(parentCtx context.Context, onDisconnectedFn func()) {
w.log.Debugf("enable WireGuard watcher")
w.ctxLock.Lock()
w.enabled = time.Now()
if w.ctx != nil && w.ctx.Err() == nil {
w.log.Errorf("WireGuard watcher already enabled")
@@ -87,6 +89,8 @@ func (w *WGWatcher) DisableWgWatcher() {
func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, ctxCancel context.CancelFunc, onDisconnectedFn func(), initialHandshake time.Time) {
w.log.Infof("WireGuard watcher started")
debugTicker := time.NewTicker(time.Second)
timer := time.NewTimer(wgHandshakeOvertime)
defer timer.Stop()
defer ctxCancel()
@@ -95,12 +99,29 @@ func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, ctxCancel contex
for {
select {
case <-debugTicker.C:
handshake, err := w.wgState()
if err != nil {
w.log.Errorf("failed to read wg stats: %v", err)
continue
}
if !handshake.IsZero() {
w.log.Infof("first wg handshake detected at: %s, %s", handshake, time.Since(w.enabled))
debugTicker.Stop()
}
case <-timer.C:
handshake, ok := w.handshakeCheck(lastHandshake)
if !ok {
onDisconnectedFn()
return
}
/*
// todo: put it back if remove debug ticker
if lastHandshake.IsZero() {
w.log.Infof("first wg handshake detected at: %s", handshake)
}
*/
lastHandshake = *handshake
resetTime := time.Until(handshake.Add(checkPeriod))