ntp

package
v1.13.13 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MPL-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ntp provides a time sync client via SNTP protocol.

Index

Constants

View Source
const (
	// MinAllowablePoll is the minimum time allowed for a client to query a time server.
	MinAllowablePoll = 32 * time.Second
	// MaxAllowablePoll is the maximum allowed interval for querying a time server.
	MaxAllowablePoll = 2048 * time.Second
	// RetryPoll is the interval between retries if the error is not Kiss-o-Death.
	RetryPoll = time.Second
	// AdjustTimeLimit is a maximum time drift to compensate via adjtimex().
	//
	// Deltas smaller than AdjustTimeLimit are gradually adjusted (slewed) to approach the network time.
	// Deltas larger than AdjustTimeLimit are set by letting the system time jump.
	AdjustTimeLimit = 400 * time.Millisecond
	// EpochLimit is a minimum time difference to signal that change as epoch change.
	EpochLimit = 15 * time.Minute
	// ExpectedAccuracy is the expected time sync accuracy, used to adjust poll interval.
	ExpectedAccuracy = 200 * time.Millisecond
)

Variables

This section is empty.

Functions

func IsPTPDevice added in v1.9.1

func IsPTPDevice(server string) bool

IsPTPDevice checks if a given server string represents a PTP device.

The device path is validated when the device is queried, see QueryPTPDevice.

func QueryPTPDevice added in v1.9.1

func QueryPTPDevice(device string) (unix.Timespec, error)

QueryPTPDevice queries PTP device for current time.

Types

type AdjustTimeFunc

type AdjustTimeFunc func(buf *unix.Timex) (state timex.State, err error)

AdjustTimeFunc provides a function to adjust time.

type CurrentTimeFunc

type CurrentTimeFunc func() time.Time

CurrentTimeFunc provides a function which returns current time.

type Measurement added in v1.7.0

type Measurement struct {
	ClockOffset time.Duration
	Leap        ntp.LeapIndicator
	Spike       bool
}

Measurement is a struct containing correction data based on a time request.

type QueryFunc

type QueryFunc func(server string) (*ntp.Response, error)

QueryFunc provides a function which performs NTP query.

type SetTimeFunc

type SetTimeFunc func(tv *syscall.Timeval) error

SetTimeFunc provides a function to set system time.

type Syncer

type Syncer struct {
	MinPoll, MaxPoll, RetryPoll time.Duration

	// these functions are overridden in tests for mocking support
	CurrentTime CurrentTimeFunc
	NTPQuery    QueryFunc
	AdjustTime  AdjustTimeFunc
	DisableRTC  bool
	// contains filtered or unexported fields
}

Syncer performs time sync via NTP on schedule.

func NewSyncer

func NewSyncer(logger *zap.Logger, timeServers []string) *Syncer

NewSyncer creates new Syncer with default configuration.

func (*Syncer) EpochChange

func (syncer *Syncer) EpochChange() <-chan struct{}

EpochChange returns a channel which receives a value each time jumps more than EpochLimit.

func (*Syncer) Run

func (syncer *Syncer) Run(ctx context.Context)

Run runs the sync process.

Run is usually run in a goroutine. When context is canceled, sync process aborts.

func (*Syncer) SetTimeServers

func (syncer *Syncer) SetTimeServers(timeServers []string)

SetTimeServers sets the list of time servers to use.

func (*Syncer) Synced

func (syncer *Syncer) Synced() <-chan struct{}

Synced returns a channel which is closed when time is in sync.

Directories

Path Synopsis
internal
spike
Package spike provides a spike detector for NTP responses.
Package spike provides a spike detector for NTP responses.

Jump to

Keyboard shortcuts

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