Skip to content

Commit f97054e

Browse files
committed
ntp: Ignore setup error
1 parent a2f9fef commit f97054e

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

common/ntp/service.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Options struct {
2626
Logger logger.Logger
2727
Server M.Socksaddr
2828
Interval time.Duration
29+
Timeout time.Duration
2930
WriteToSystem bool
3031
}
3132

@@ -39,6 +40,7 @@ type Service struct {
3940
server M.Socksaddr
4041
writeToSystem bool
4142
ticker *time.Ticker
43+
timeout time.Duration
4244
clockOffset time.Duration
4345
pause pause.Manager
4446
}
@@ -81,16 +83,18 @@ func NewService(options Options) *Service {
8183
writeToSystem: options.WriteToSystem,
8284
server: destination,
8385
ticker: time.NewTicker(interval),
86+
timeout: options.Timeout,
8487
pause: service.FromContext[pause.Manager](ctx),
8588
}
8689
}
8790

8891
func (s *Service) Start() error {
8992
err := s.update()
9093
if err != nil {
91-
return E.Cause(err, "initialize time")
94+
s.logger.Error(E.Cause(err, "initialize time"))
95+
} else {
96+
s.logger.Info("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
9297
}
93-
s.logger.Info("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
9498
go s.loopUpdate()
9599
return nil
96100
}
@@ -124,23 +128,31 @@ func (s *Service) loopUpdate() {
124128
}
125129
err := s.update()
126130
if err == nil {
127-
s.logger.Debug("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
131+
s.logger.Info("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
128132
} else {
129-
s.logger.Warn("update time: ", err)
133+
s.logger.Error("update time: ", err)
130134
}
131135
}
132136
}
133137

134138
func (s *Service) update() error {
139+
ctx := s.ctx
140+
var cancel context.CancelFunc
141+
if s.timeout > 0 {
142+
ctx, cancel = context.WithTimeout(ctx, s.timeout)
143+
}
135144
response, err := Exchange(s.ctx, s.dialer, s.server)
145+
if cancel != nil {
146+
cancel()
147+
}
136148
if err != nil {
137149
return err
138150
}
139151
s.clockOffset = response.ClockOffset
140152
if s.writeToSystem {
141153
writeErr := SetSystemTime(s.TimeFunc()())
142154
if writeErr != nil {
143-
s.logger.Warn("write time to system: ", writeErr)
155+
s.logger.Error("write time to system: ", writeErr)
144156
}
145157
}
146158
return nil

0 commit comments

Comments
 (0)