timesync

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package timesync is used to check system time reliability by communicating with NTP time servers.

Index

Constants

View Source
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 tries we try to query ntp before we give up when having 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

View Source
var (
	DefaultServers = []string{
		"0.pool.ntp.org",
		"1.pool.ntp.org",
		"time.google.com",
		"time1.google.com",
		"time.asia.apple.com",
		"time.americas.apple.com",
	}
)

DefaultServer is a list of relay on more than one server.

Functions

func CheckMessageDrift

func CheckMessageDrift(data int64) bool

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

func CheckSystemClockDrift() (time.Duration, error)

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 Clock

type Clock interface {
	Now() time.Time
}

type LayerConv

type LayerConv struct {
	// contains filtered or unexported fields
}

func (LayerConv) LayerToTime

func (lc LayerConv) LayerToTime(id types.LayerID) time.Time

LayerToTime returns the time of the provided layer

func (LayerConv) TimeToLayer

func (lc LayerConv) TimeToLayer(t time.Time) types.LayerID

TimeToLayer returns the layer of the provided time

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

type LayerTimer chan types.LayerID

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.

func (*NtpPacket) Time

func (n *NtpPacket) Time() time.Time

Time makes a Time struct from NtpPacket data.

type RealClock

type RealClock struct{}

func (RealClock) Now

func (RealClock) Now() time.Time

type Ticker

type Ticker struct {
	// contains filtered or unexported fields
}

func NewTicker

func NewTicker(c Clock, lc LayerConverter) *Ticker

func (*Ticker) AwaitLayer

func (t *Ticker) AwaitLayer(layerId types.LayerID) chan struct{}

func (*Ticker) GetCurrentLayer

func (t *Ticker) GetCurrentLayer() types.LayerID

func (*Ticker) Notify

func (t *Ticker) Notify() (int, error)

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()

func (Ticker) Subscribe

func (s Ticker) Subscribe() LayerTimer

func (Ticker) Unsubscribe

func (s Ticker) Unsubscribe(ch LayerTimer)

type TimeClock

type TimeClock struct {
	*Ticker
	// contains filtered or unexported fields
}

func NewClock

func NewClock(c Clock, tickInterval time.Duration, genesisTime time.Time, logger log.Log) *TimeClock

func (*TimeClock) Close

func (t *TimeClock) Close()

func (*TimeClock) GetGenesisTime

func (t *TimeClock) GetGenesisTime() time.Time

func (TimeClock) Subscribe

func (s TimeClock) Subscribe() LayerTimer

func (TimeClock) Unsubscribe

func (s TimeClock) Unsubscribe(ch LayerTimer)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL