fix: ntp service panic
This commit is contained in:
parent
1d4af2d92b
commit
a1eab125ee
1 changed files with 15 additions and 5 deletions
|
@ -67,9 +67,19 @@ func (srv *Service) Running() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Service) update() {
|
func (srv *Service) update() {
|
||||||
response, err := ntp.Exchange(context.Background(), N.SystemDialer, srv.server)
|
var response *ntp.Response
|
||||||
if err != nil || response == nil {
|
var err error
|
||||||
log.Errorln("initialize time: %s", err)
|
for i := 0; i < 3; i++ {
|
||||||
|
response, err = ntp.Exchange(context.Background(), N.SystemDialer, srv.server)
|
||||||
|
if err != nil {
|
||||||
|
if i == 2 {
|
||||||
|
log.Errorln("Initialize NTP time failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second * 2) // wait for 2 seconds before the next try
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
offset = response.ClockOffset
|
offset = response.ClockOffset
|
||||||
if offset > time.Duration(0) {
|
if offset > time.Duration(0) {
|
||||||
|
@ -81,9 +91,9 @@ func (srv *Service) update() {
|
||||||
timeNow := response.Time
|
timeNow := response.Time
|
||||||
err = setSystemTime(timeNow)
|
err = setSystemTime(timeNow)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Infoln("sync system time success: %s", timeNow.Local().Format(ntp.TimeLayout))
|
log.Infoln("Sync system time success: %s", timeNow.Local().Format(ntp.TimeLayout))
|
||||||
} else {
|
} else {
|
||||||
log.Errorln("write time to system: %s", err)
|
log.Errorln("Write time to system: %s", err)
|
||||||
srv.syncSystemTime = false
|
srv.syncSystemTime = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue