Skip to content

Commit 00256fd

Browse files
authored
refactor: ntp init logs but not panic (#1568)
1 parent e488708 commit 00256fd

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

core/ntp.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@ import (
1414
type NTPSyncedClock struct {
1515
offset int64
1616
logger logging.Logger
17-
cancel context.CancelFunc
1817
}
1918

2019
// 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) {
2221
if logger == nil {
2322
return nil, fmt.Errorf("logger must not be nil")
2423
}
25-
ctx, cancel := context.WithCancel(parentCtx)
2624

2725
clock := &NTPSyncedClock{
2826
logger: logger.With("component", "NTPSyncedClock"),
29-
cancel: cancel,
3027
}
3128

29+
// Try to sync once, but don't fail if it doesn't work
3230
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)
3532
}
3633

3734
go clock.runSyncLoop(ctx, server, syncInterval)
@@ -67,6 +64,7 @@ func (c *NTPSyncedClock) syncOnce(server string) error {
6764
}
6865

6966
// 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.
7068
func (c *NTPSyncedClock) Now() time.Time {
7169
offset := atomic.LoadInt64(&c.offset)
7270
return time.Now().Add(time.Duration(offset))

0 commit comments

Comments
 (0)