-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ntp): add ntp util #1274
feat(ntp): add ntp util #1274
Conversation
7fb7b9c
to
2f1a71c
Compare
util/ntp/ntp.go
Outdated
serverList []string | ||
} | ||
|
||
func NewNtpServer() *Server { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically it is not NTPServer.
We better name it NTPChecker.
We can use |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1274 +/- ##
==========================================
- Coverage 76.05% 75.81% -0.25%
==========================================
Files 206 207 +1
Lines 10672 10742 +70
==========================================
+ Hits 8117 8144 +27
- Misses 2169 2205 +36
- Partials 386 393 +7 |
be6ae23
to
484a9b0
Compare
util/ntp/ntp.go
Outdated
offset time.Duration | ||
interval time.Duration | ||
logger *logger.SubLogger | ||
retryTimes int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of retrying?
Generally speaking, we repeat this check every few minutes, and we don't plan to use it as a "single source of truth." Therefore, we don't need to force retries on failure.
util/ntp/ntp.go
Outdated
threshold time.Duration | ||
offset time.Duration | ||
interval time.Duration | ||
logger *logger.SubLogger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid defining sub logger for small modules.
Here we can use the global logger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should have a consistent approach to this
util/ntp/ntp.go
Outdated
server := "pool.ntp.org" | ||
response, err := ntp.Query(server) | ||
if err != nil { | ||
c.logger.Debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we have an error, so better we log it as Error
not Debug
util/ntp/ntp.go
Outdated
} | ||
|
||
if err := response.Validate(); err != nil { | ||
c.logger.Debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. An error has occurred but we log it as Debug.
return | ||
|
||
case <-ticker.C: | ||
c.lock.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has flaw.
Imagine c.clockOffset
took time to process. and we locked till it finishes.
Therefore GetClockOffset()
will be blocked.
util/ntp/ntp.go
Outdated
return math.Abs(float64(offset)) > float64(c.threshold) | ||
} | ||
|
||
func (*Checker) String() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for defining this?
util/ntp/ntp.go
Outdated
} | ||
|
||
func (c *Checker) checkClockOffset() { | ||
ticker := time.NewTicker(c.interval) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep ticker instance and close it on Stop.
util/ntp/ntp.go
Outdated
c.cancel() | ||
} | ||
|
||
func (c *Checker) GetThreshold() time.Duration { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the reason for this.
Whoever creates the NTP check also knows the threshold. Why should we report it again here?
} | ||
|
||
func (c *Checker) GetClockOffset() (time.Duration, error) { | ||
c.lock.RLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can de simplified using defer
util/ntp/ntp.go
Outdated
c.lock.RUnlock() | ||
|
||
if offset == maxClockOffset { | ||
return 0, errors.New("error on getting clock offset") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please define error type.
That helps to test the package
484a9b0
to
ad15e4a
Compare
Description
This PR add NTP util
The availability score may drop due to unstable network connection or mismatch system clock with the whole blockchain network
Thus we provide an information in CLI, GUI and gRPC endpoint, show that how many milliseconds your node is ahead/behind the network.
We use NTP servers to get the correct time
and if the absolute difference between your node and the whole network is greater than
1 second
, the score may dropAll in all, this functionality allows user get the basic idea on what's going on with the node's status
Related issue(s)