/
ntp.go
55 lines (49 loc) · 1.65 KB
/
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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package blockchain
import (
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/assetcloud/chain/common"
"github.com/assetcloud/chain/types"
)
const (
ntpChecks = 6
driftThreshold = 10 * time.Second // Allowed clock drift before warning user
)
var (
ntpLog = chainlog.New("submodule", "ntp")
)
// checkClockDrift queries an NTP server for clock drifts and warns the user if
// one large enough is detected.
func (chain *BlockChain) checkClockDrift() {
realnow := common.GetRealTimeRetry(chain.client.GetConfig().GetModuleConfig().NtpHosts, 10)
if realnow.IsZero() {
chain.UpdateNtpClockSyncStatus(false)
ntpLog.Warn("checkClockDrift", "sntpDrift err", "get ntptime error")
return
}
now := types.Now()
drift := now.Sub(realnow)
if drift < -driftThreshold || drift > driftThreshold {
warning := fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift)
howtofix := "Please enable network time synchronisation in system settings"
separator := strings.Repeat("-", len(warning))
ntpLog.Info(fmt.Sprint(separator))
ntpLog.Info(fmt.Sprint(warning))
ntpLog.Info(fmt.Sprint(howtofix))
ntpLog.Info(fmt.Sprint(separator))
atomic.AddInt32(&chain.failed, 1)
if atomic.LoadInt32(&chain.failed) == ntpChecks {
ntpLog.Info("System clock seems ERROR")
chain.UpdateNtpClockSyncStatus(false)
}
} else {
atomic.StoreInt32(&chain.failed, 0)
chain.UpdateNtpClockSyncStatus(true)
ntpLog.Info(fmt.Sprintf("Sanity NTP check reported %v drift, all ok", drift))
}
}