clock

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package clock keeps the system clock on NTP time. Boot steps it once, so certificates and ACME start from a sane clock. Then the clock polls its servers every 64s, lengthening to 1024s while the offset stays small. It first measures the clock's frequency error, as ntpd does without a drift file: ten minutes of offsets left alone, whose slope the kernel is told to correct, and the offset they built up slewed away on the side, as adjtime does, so the PLL never takes it for a frequency error. From then on each offset goes to the kernel's NTP discipline (the PLL ntpd drives through adjtimex), which slews it away and follows the frequency as it wanders, so time never jumps. The PLL alone would take hours to learn a frequency error of a few ppm, and the clock would drift milliseconds meanwhile. Only a large offset is stepped, and backwards only when the clock is more than a second ahead, which normal running never is.

Each poll takes a burst of samples from every server and keeps each server's fastest one, whose offset the round trip distorts least. The servers must then agree: a majority whose offsets, within each one's root distance, overlap. A server that answers nothing, or answers a time the others do not share, moves nothing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock added in v0.4.0

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

Clock is the edge's NTP client. New makes one; Sync steps the clock once, and Run keeps it in step until its context ends.

func New added in v0.4.0

func New(servers []string, network string) *Clock

New is a clock for servers (host or host:port; pool.ntp.org and its subdomains are pools, whose first addresses are servers of their own). network is "ip4" or "ip6" for an edge with one family, else "ip".

func (*Clock) Run added in v0.4.0

func (c *Clock) Run(ctx context.Context)

Run polls and corrects the clock until ctx ends.

func (*Clock) Status added in v0.4.0

func (c *Clock) Status() map[string]any

Status is the clock for /~!ops/status.

func (*Clock) Sync added in v0.4.0

func (c *Clock) Sync(ctx context.Context) error

Sync steps the clock to the servers' time, retrying each second until they agree or ctx ends. It is boot's: one sample a server, for speed.

Boot waits on it before anything listens, so it decides as soon as the answers in hand agree, a majority of the servers, not once every server has answered or timed out: a lost packet would hold boot for queryTimeout. A server that has not answered is asked again every bootRetry; the answers already in are kept, so a server that did answer is not asked again within seconds, which ntpd servers answer with a kiss of death.

func (*Clock) WriteMetrics added in v0.4.0

func (c *Clock) WriteMetrics(w *metrics.Writer)

WriteMetrics writes the clock's metrics.

Jump to

Keyboard shortcuts

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