fix(main): node expired when system sleep too long

This commit is contained in:
nullcat
2026-05-18 04:04:11 +08:00
parent 962ea6be21
commit 15bda1a807
2 changed files with 76 additions and 14 deletions
+37
View File
@@ -0,0 +1,37 @@
package core
import (
"context"
"log/slog"
"time"
)
func StartTimeWatchDog(ctx context.Context, logger *slog.Logger) <-chan struct{} {
logger.Info("starting watchdog")
ch := make(chan struct{}, 1)
go func() {
lastUnix := time.Now().Unix()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
nowUnix := time.Now().Unix()
diff := nowUnix - lastUnix
if diff > 300 {
logger.Warn("system time jump detected(wake up from sleep?)",
slog.Int64("jump_seconds", diff),
)
ch <- struct{}{}
logger.Debug("signal sent, watchdog exiting...")
return
} else {
lastUnix = nowUnix
}
}
}
}()
return ch
}