Documentation ¶
Overview ¶
Package timesync is used to check system time reliability by communicating with NTP time servers.
Index ¶
Constants ¶
const ( // MaxAllowedMessageDrift is the time we limit we receive and handle delivered messages within MaxAllowedMessageDrift = 10 * time.Minute // NtpOffset is 70 years in seconds since ntp counts from 1900 and unix from 1970 NtpOffset = 2208988800 // DefaultNtpPort is the ntp protocol port DefaultNtpPort = "123" // MaxRequestTries is the number of times we try to query ntp before we give up when encountering errors. MaxRequestTries = 3 // RequestTriesInterval is the interval to wait between tries to ask ntp for the time RequestTriesInterval = time.Second * 5 // MinResultsThreshold is the minimum number of successful ntp query results to calculate the drift MinResultsThreshold = 3 )
Variables ¶
var (
DefaultServers = []string{
"time-a-wwv.nist.gov",
"time-b-wwv.nist.gov",
"time-c-wwv.nist.gov",
"time.google.com",
"time1.google.com",
"time3.google.com",
"time4.google.com",
"time.asia.apple.com",
"time.americas.apple.com",
"0.cn.pool.ntp.org",
"1.cn.pool.ntp.org",
"2.cn.pool.ntp.org",
"3.cn.pool.ntp.org",
"time1.cloud.tencent.com",
"time2.cloud.tencent.com",
"time3.cloud.tencent.com",
"time4.cloud.tencent.com",
"ntp2.aliyun.com",
"ntp4.aliyun.com",
}
)
DefaultServer is a list of relay on more than one server.
Functions ¶
func CheckMessageDrift ¶
CheckMessageDrift checks if a given message timestamp is too far from our local clock. accepts a unix timestamp. can be created with Time.Now().Unix()
func CheckSystemClockDrift ¶
CheckSystemClockDrift is comparing our clock to the collected ntp data return the drift and an error when drift reading failed or exceeds our preset MaxAllowedDrift
Types ¶
type LayerConv ¶
type LayerConv struct {
// contains filtered or unexported fields
}
LayerConv is a converter between time to layer ID struct
func (LayerConv) LayerToTime ¶
LayerToTime returns the time of the provided layer
type LayerConverter ¶
type LayerConverter interface { TimeToLayer(time.Time) types.LayerID LayerToTime(types.LayerID) time.Time }
LayerConverter provides conversions from time to layer and vice versa
type LayerTimer ¶
LayerTimer is a channel of LayerIDs Subscribers will receive the ticked layer through such channel
type NtpPacket ¶
type NtpPacket struct { Settings uint8 // leap yr indicator, ver number, and mode Stratum uint8 // stratum of local clock Poll int8 // poll exponent Precision int8 // precision exponent RootDelay uint32 // root delay RootDispersion uint32 // root dispersion ReferenceID uint32 // reference id RefTimeSec uint32 // reference timestamp sec RefTimeFrac uint32 // reference timestamp fractional OrigTimeSec uint32 // origin time secs OrigTimeFrac uint32 // origin time fractional RxTimeSec uint32 // receive time secs RxTimeFrac uint32 // receive time frac3 TxTimeSec uint32 // transmit time secs TxTimeFrac uint32 // transmit time frac }
NtpPacket is a 48 bytes packet used for querying ntp information.
type Ticker ¶
type Ticker struct { LayerConverter // layer conversions provider // contains filtered or unexported fields }
Ticker is the struct responsible for notifying that a layer has been ticked to subscribers
func NewTicker ¶
func NewTicker(c Clock, lc LayerConverter) *Ticker
NewTicker returns a new instance of ticker
func (*Ticker) AwaitLayer ¶
AwaitLayer returns a channel that will be signald when layer id layerID was ticked by the clock, or if this layer has passed while sleeping. it does so by closing the returned channel
func (*Ticker) GetCurrentLayer ¶
GetCurrentLayer gets the current layer
func (*Ticker) Notify ¶
Notify notifies all the subscribers with the current layer if the tick time has passed notify is skipped and errMissedTickTime is returned notify may be skipped also for non-monotonic tick if some of the subscribers where not listening, they are skipped. In that case, errMissedTicks is returned along the number of subscribers not listening
func (*Ticker) StartNotifying ¶
func (t *Ticker) StartNotifying()
StartNotifying starts the clock notifying
func (Ticker) Subscribe ¶
func (s Ticker) Subscribe() LayerTimer
Subscribe returns a channel on which the subscriber will be notified when a new layer starts
func (Ticker) Unsubscribe ¶
func (s Ticker) Unsubscribe(ch LayerTimer)
Unsubscribe removed subscriber channel ch from notification list
type TimeClock ¶
type TimeClock struct { *Ticker // contains filtered or unexported fields }
TimeClock is the struct holding a real clock
func NewClock ¶
func NewClock(c Clock, tickInterval time.Duration, genesisTime time.Time, logger log.Log) *TimeClock
NewClock return TimeClock struct that notifies tickInterval has passed
func (*TimeClock) GetGenesisTime ¶
GetGenesisTime returns at which time this clock has started (used to calculate current tick)
func (*TimeClock) GetInterval ¶ added in v0.1.6
GetInterval returns the time interval between clock ticks
func (TimeClock) Subscribe ¶
func (s TimeClock) Subscribe() LayerTimer
Subscribe returns a channel on which the subscriber will be notified when a new layer starts
func (TimeClock) Unsubscribe ¶
func (s TimeClock) Unsubscribe(ch LayerTimer)
Unsubscribe removed subscriber channel ch from notification list