forked from iotaledger/goshimmer
/
synchronization.go
47 lines (37 loc) · 1.09 KB
/
synchronization.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
package clock
import (
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/beevik/ntp"
)
// ErrNTPQueryFailed is returned if an NTP query failed.
var ErrNTPQueryFailed = errors.New("NTP query failed")
// difference between network time and node's local time.
var (
offset time.Duration
offsetMutex sync.RWMutex
)
// 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.
func FetchTimeOffset(host string) error {
resp, err := ntp.Query(host)
if err != nil {
return errors.Errorf("NTP query error (%v): %w", err, ErrNTPQueryFailed)
}
offsetMutex.Lock()
defer offsetMutex.Unlock()
offset = resp.ClockOffset
return nil
}
// SyncedTime gets the synchronized time (according to the network) of a node.
func SyncedTime() time.Time {
offsetMutex.RLock()
defer offsetMutex.RUnlock()
return time.Now().Add(offset)
}
// Since returns the time elapsed since t.
// It is shorthand for clock.SyncedTime().Sub(t).
func Since(t time.Time) time.Duration {
return SyncedTime().Sub(t)
}