Skip to content

Commit 77b4c1c

Browse files
authored
ntp: fix panic on NTP query error and add IPv6 server for IPv6-only support (#424)
* fix(ntp): prevent panic on NTP query error and add IPv6 server in defaultNTPServers * fix(ntp): make sure queryMultipleNTP finish if all servers failed
1 parent 5f8b451 commit 77b4c1c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

internal/timesync/ntp.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var defaultNTPServers = []string{
1313
"time.aws.com",
1414
"time.windows.com",
1515
"time.google.com",
16-
"162.159.200.123", // time.cloudflare.com
16+
"162.159.200.123", // time.cloudflare.com IPv4
17+
"2606:4700:f1::123", // time.cloudflare.com IPv6
1718
"0.pool.ntp.org",
1819
"1.pool.ntp.org",
1920
"2.pool.ntp.org",
@@ -57,6 +58,13 @@ func (t *TimeSync) queryMultipleNTP(servers []string, timeout time.Duration) (no
5758

5859
// query the server
5960
now, response, err := queryNtpServer(server, timeout)
61+
if err != nil {
62+
scopedLogger.Warn().
63+
Str("error", err.Error()).
64+
Msg("failed to query NTP server")
65+
results <- nil
66+
return
67+
}
6068

6169
// set the last RTT
6270
metricNtpServerLastRTT.WithLabelValues(
@@ -76,32 +84,33 @@ func (t *TimeSync) queryMultipleNTP(servers []string, timeout time.Duration) (no
7684
strconv.Itoa(int(response.Precision)),
7785
).Set(1)
7886

79-
if err == nil {
80-
// increase success count
81-
metricNtpTotalSuccessCount.Inc()
82-
metricNtpSuccessCount.WithLabelValues(server).Inc()
83-
84-
scopedLogger.Info().
85-
Str("time", now.Format(time.RFC3339)).
86-
Str("reference", response.ReferenceString()).
87-
Str("rtt", response.RTT.String()).
88-
Str("clockOffset", response.ClockOffset.String()).
89-
Uint8("stratum", response.Stratum).
90-
Msg("NTP server returned time")
91-
results <- &ntpResult{
92-
now: now,
93-
offset: &response.ClockOffset,
94-
}
95-
} else {
96-
scopedLogger.Warn().
97-
Str("error", err.Error()).
98-
Msg("failed to query NTP server")
87+
// increase success count
88+
metricNtpTotalSuccessCount.Inc()
89+
metricNtpSuccessCount.WithLabelValues(server).Inc()
90+
91+
scopedLogger.Info().
92+
Str("time", now.Format(time.RFC3339)).
93+
Str("reference", response.ReferenceString()).
94+
Str("rtt", response.RTT.String()).
95+
Str("clockOffset", response.ClockOffset.String()).
96+
Uint8("stratum", response.Stratum).
97+
Msg("NTP server returned time")
98+
results <- &ntpResult{
99+
now: now,
100+
offset: &response.ClockOffset,
99101
}
100102
}(server)
101103
}
102104

103-
result := <-results
104-
return result.now, result.offset
105+
for range servers {
106+
result := <-results
107+
if result == nil {
108+
continue
109+
}
110+
now, offset = result.now, result.offset
111+
return
112+
}
113+
return
105114
}
106115

107116
func queryNtpServer(server string, timeout time.Duration) (now *time.Time, response *ntp.Response, err error) {

0 commit comments

Comments
 (0)