Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
goshimmer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
iota-imt
goshimmer
Commits
df93b18a
Unverified
Commit
df93b18a
authored
4 years ago
by
Angelo Capossele
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix potential clock race condition (#814)
parent
a443f2e0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/clock/synchronization.go
+6
-0
6 additions, 0 deletions
packages/clock/synchronization.go
with
6 additions
and
0 deletions
packages/clock/synchronization.go
+
6
−
0
View file @
df93b18a
...
@@ -2,6 +2,7 @@ package clock
...
@@ -2,6 +2,7 @@ package clock
import
(
import
(
"errors"
"errors"
"sync"
"time"
"time"
"github.com/beevik/ntp"
"github.com/beevik/ntp"
...
@@ -13,6 +14,7 @@ var ErrNTPQueryFailed = errors.New("NTP query failed")
...
@@ -13,6 +14,7 @@ var ErrNTPQueryFailed = errors.New("NTP query failed")
// difference between network time and node's local time.
// difference between network time and node's local time.
var
offset
time
.
Duration
var
offset
time
.
Duration
var
offsetMutex
sync
.
RWMutex
// FetchTimeOffset establishes the difference in local vs network time.
// FetchTimeOffset establishes the difference in local vs network time.
// This difference is stored in offset so that it can be used to adjust the local clock.
// This difference is stored in offset so that it can be used to adjust the local clock.
...
@@ -21,6 +23,8 @@ func FetchTimeOffset(host string) error {
...
@@ -21,6 +23,8 @@ func FetchTimeOffset(host string) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"NTP query error (%v): %w"
,
err
,
ErrNTPQueryFailed
)
return
xerrors
.
Errorf
(
"NTP query error (%v): %w"
,
err
,
ErrNTPQueryFailed
)
}
}
offsetMutex
.
Lock
()
defer
offsetMutex
.
Unlock
()
offset
=
resp
.
ClockOffset
offset
=
resp
.
ClockOffset
return
nil
return
nil
...
@@ -28,5 +32,7 @@ func FetchTimeOffset(host string) error {
...
@@ -28,5 +32,7 @@ func FetchTimeOffset(host string) error {
// SyncedTime gets the synchronized time (according to the network) of a node.
// SyncedTime gets the synchronized time (according to the network) of a node.
func
SyncedTime
()
time
.
Time
{
func
SyncedTime
()
time
.
Time
{
offsetMutex
.
RLock
()
defer
offsetMutex
.
RUnlock
()
return
time
.
Now
()
.
Add
(
offset
)
return
time
.
Now
()
.
Add
(
offset
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment