@@ -14,24 +14,21 @@ import (
14
14
type NTPSyncedClock struct {
15
15
offset int64
16
16
logger logging.Logger
17
- cancel context.CancelFunc
18
17
}
19
18
20
19
// NewNTPSyncedClock creates a new NTP synchronized clock and starts background sync.
21
- func NewNTPSyncedClock (parentCtx context.Context , server string , syncInterval time.Duration , logger logging.Logger ) (* NTPSyncedClock , error ) {
20
+ func NewNTPSyncedClock (ctx context.Context , server string , syncInterval time.Duration , logger logging.Logger ) (* NTPSyncedClock , error ) {
22
21
if logger == nil {
23
22
return nil , fmt .Errorf ("logger must not be nil" )
24
23
}
25
- ctx , cancel := context .WithCancel (parentCtx )
26
24
27
25
clock := & NTPSyncedClock {
28
26
logger : logger .With ("component" , "NTPSyncedClock" ),
29
- cancel : cancel ,
30
27
}
31
28
29
+ // Try to sync once, but don't fail if it doesn't work
32
30
if err := clock .syncOnce (server ); err != nil {
33
- cancel ()
34
- return nil , fmt .Errorf ("initial NTP query failed: %w" , err )
31
+ clock .logger .Warn ("Initial NTP sync failed, will retry in background and use system clock until sync succeeds" , "err" , err )
35
32
}
36
33
37
34
go clock .runSyncLoop (ctx , server , syncInterval )
@@ -67,6 +64,7 @@ func (c *NTPSyncedClock) syncOnce(server string) error {
67
64
}
68
65
69
66
// Now returns the current time compensated by the latest NTP offset.
67
+ // If the NTP sync has not yet succeeded, it returns the current system time.
70
68
func (c * NTPSyncedClock ) Now () time.Time {
71
69
offset := atomic .LoadInt64 (& c .offset )
72
70
return time .Now ().Add (time .Duration (offset ))
0 commit comments