chore(ntp): more logging

This commit is contained in:
Siyuan Miao 2025-02-15 19:42:29 +01:00
parent ed3b06f3bf
commit 0498ab4c3f
2 changed files with 31 additions and 25 deletions

View File

@ -8,9 +8,7 @@ import (
"strings"
"time"
"net"
"os/exec"
"time"
"github.com/hashicorp/go-envparse"
"github.com/pion/mdns/v2"
@ -23,11 +21,15 @@ import (
var mDNSConn *mdns.Conn
var networkState struct {
var networkState NetworkState
type NetworkState struct {
Up bool
IPv4 string
IPv6 string
MAC string
checked bool
}
type LocalIpInfo struct {
@ -65,14 +67,11 @@ func checkNetworkState() {
return
}
newState := struct {
Up bool
IPv4 string
IPv6 string
MAC string
}{
newState := NetworkState{
Up: iface.Attrs().OperState == netlink.OperUp,
MAC: iface.Attrs().HardwareAddr.String(),
checked: true,
}
addrs, err := netlink.AddrList(iface, nl.FAMILY_ALL)
@ -86,12 +85,6 @@ func checkNetworkState() {
setDhcpClientState(newState.Up)
}
// If the link is going down, put udhcpc into idle mode.
// If the link is coming back up, activate udhcpc and force it to renew the lease.
if newState.Up != networkState.Up {
setDhcpClientState(newState.Up)
}
for _, addr := range addrs {
if addr.IP.To4() != nil {
if !newState.Up && networkState.Up {

33
ntp.go
View File

@ -12,8 +12,12 @@ import (
)
const (
timeSyncRetryStep = 5 * time.Second
timeSyncRetryMaxInt = 1 * time.Minute
timeSyncRetryStep = 5 * time.Second
timeSyncRetryMaxInt = 1 * time.Minute
timeSyncWaitNetChkInt = 100 * time.Millisecond
timeSyncWaitNetUpInt = 3 * time.Second
timeSyncInterval = 1 * time.Hour
timeSyncTimeout = 2 * time.Second
)
var (
@ -27,13 +31,18 @@ var (
func TimeSyncLoop() {
for {
if !networkState.Up {
fmt.Printf("Waiting for network to come up\n")
time.Sleep(3 * time.Second)
if !networkState.checked {
time.Sleep(timeSyncWaitNetChkInt)
continue
}
fmt.Println("Syncing system time")
if !networkState.Up {
log.Printf("Waiting for network to come up")
time.Sleep(timeSyncWaitNetUpInt)
continue
}
log.Printf("Syncing system time")
start := time.Now()
err := SyncSystemTime()
if err != nil {
@ -51,7 +60,7 @@ func TimeSyncLoop() {
}
log.Printf("Time sync successful, now is: %v, time taken: %v", time.Now(), time.Since(start))
timeSynced = true
time.Sleep(1 * time.Hour) //once the first sync is done, sync every hour
time.Sleep(timeSyncInterval) // after the first sync is done
}
}
@ -70,16 +79,20 @@ func SyncSystemTime() (err error) {
func queryNetworkTime() (*time.Time, error) {
ntpServers, err := getNTPServersFromDHCPInfo()
if err != nil {
fmt.Printf("failed to get NTP servers from DHCP info: %v\n", err)
log.Printf("failed to get NTP servers from DHCP info: %v\n", err)
}
if ntpServers == nil {
ntpServers = defaultNTPServers
log.Printf("Using default NTP servers: %v\n", ntpServers)
} else {
log.Printf("Using NTP servers from DHCP: %v\n", ntpServers)
}
for _, server := range ntpServers {
now, err := queryNtpServer(server, 2*time.Second)
now, err := queryNtpServer(server, timeSyncTimeout)
if err == nil {
log.Printf("NTP server [%s] returned time: %v\n", server, now)
return now, nil
}
}
@ -88,7 +101,7 @@ func queryNetworkTime() (*time.Time, error) {
"http://cloudflare.com",
}
for _, url := range httpUrls {
now, err := queryHttpTime(url, 2*time.Second)
now, err := queryHttpTime(url, timeSyncTimeout)
if err == nil {
return now, nil
}