Skip to content

Commit

Permalink
util/systimemon: check system time jump by comparing UnixNano (pingca…
Browse files Browse the repository at this point in the history
…p#4725)

After Go1.9, time.Sub() compare time using monotime, so it can't be
used to check system time jump.
  • Loading branch information
tiancaiamao authored and coocood committed Oct 9, 2017
1 parent 9c2ea17 commit 550537a
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions util/systimemon/systime_mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ func StartMonitor(now func() time.Time, systimeErrHandler func()) {
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
last := now()
select {
case <-tick.C:
if now().Sub(last) < 0 {
log.Errorf("system time jump backward, last:%v", last)
systimeErrHandler()
}
last := now().UnixNano()
<-tick.C
if now().UnixNano() < last {
log.Errorf("system time jump backward, last:%v", last)
systimeErrHandler()
}
}
}

0 comments on commit 550537a

Please sign in to comment.