/
clock_ntp.go
executable file
·95 lines (78 loc) · 1.84 KB
/
clock_ntp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gtime
// A clock who is independent with system clock and sync with NTP server.
// Change system clock need ROOT, but NtpClock doesn't.
import (
"github.com/cryptowilliam/goutil/basic/gerrors"
"math"
"sync"
"time"
)
// github.com/hlandau/degoutils/clock
type NtpClock struct {
diff time.Duration
diffRwmu sync.RWMutex
}
var _globalNtpClock_ *NtpClock
func GetNtpClockONLINE() (*NtpClock, error) {
ntpTime, err := GetNetTimeInLocalONLINE()
if err != nil {
return nil, err
}
nc := NtpClock{
diff: ntpTime.Sub(time.Now()),
}
return &nc, nil
}
func GetNtpSyncClockONLINE() (*NtpClock, error) {
ntpTime, err := GetNetTimeInLocalONLINE()
if err != nil {
return nil, err
}
nc := NtpClock{
diff: ntpTime.Sub(time.Now()),
}
go nc.sync()
return &nc, nil
}
func (nc *NtpClock) sync() {
for {
time.Sleep(time.Second * 5)
ntpTime, err := GetNetTimeInLocalONLINE()
if err != nil {
continue
}
nc.diffRwmu.Lock()
nc.diff = ntpTime.Sub(time.Now())
nc.diffRwmu.Unlock()
}
}
func (nc *NtpClock) Name() string {
return "ntp"
}
func (nc *NtpClock) Now() time.Time {
nc.diffRwmu.RLock()
diff := nc.diff
nc.diffRwmu.RUnlock()
return time.Now().Add(diff)
}
func (nc *NtpClock) Sleep(d time.Duration) {
time.Sleep(d)
}
func (nc *NtpClock) IsMock() bool {
return false
}
func (nc *NtpClock) Set(tm time.Time) error {
return gerrors.New("ntp clock doesn't support Set interface")
}
func VerifySystemTimeWithNtpOL(intervalAllowed time.Duration) error {
ntpTime, err := GetNetTimeInLocalONLINE()
if err != nil {
return err
}
now := time.Now()
diff := ntpTime.Sub(now)
if math.Abs(float64(diff.Nanoseconds())) > math.Abs(float64(intervalAllowed.Nanoseconds())) {
return gerrors.Errorf("system time(%s) different from ntp time(%s) more than %s", now.String(), ntpTime.String(), intervalAllowed.String())
}
return nil
}