Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion SYNC_DETAILS.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ As the newest version wins, the clocks matter: each side sends its time when the
(`-sync-max-clock-skew`). The time is also in every keepalive (every 10s), so a clock that drifts or gets
stepped later is noticed too: the warning is logged when it crosses the limit, and again when it's back
(within 80% of it, to not flap). One-way transit time only adds to the measured difference, so the
smallest of the last 6 samples (a minute) is used. Peers up to v0.24.1 send no time in their keepalives (and
smallest of the last 6 samples (a minute) is used, except that when the last 2 are all well above the older ones (a clock
step, not a delayed packet) the older ones are dropped. Peers up to v0.24.1 send no time in their keepalives (and
ignore ours): only the link-up check applies to them.

With `-sync-conflicts list`, files changed on both sides are left alone (both versions stay where they are) and
Expand Down
10 changes: 10 additions & 0 deletions tsnet/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,20 @@ func skewLimit(maxSkew time.Duration) time.Duration {
// measured skew, so the smallest one is the best estimate of the real one.
const skewWindow = 6

// skewStep is the smallest jump of the clock skew (up) we take as a clock change right away, see noteSkewLocked.
const skewStep = 50 * time.Millisecond

// noteSkewLocked records a clock skew sample from the peer (how much its clock is behind ours, roughly) and
// logs when the clock gets too far off (or back to normal). Needs stateMu.
func (s *Server) noteSkewLocked(peer Peer, l *link, sample time.Duration) {
l.skewRecent = append(l.skewRecent, sample)
if n := len(l.skewRecent); n > 2 {
// a clock step forward leaves the old smaller samples in the window: if the last 2 are all well above
// the older ones (one delayed sample doesn't count), start over from them instead of waiting for the old to age out.
if slices.Min(l.skewRecent[n-2:])-slices.Min(l.skewRecent[:n-2]) > max(skewLimit(s.SyncMaxClockSkew), skewStep) {
l.skewRecent = slices.Clone(l.skewRecent[n-2:])
}
}
if len(l.skewRecent) > skewWindow {
l.skewRecent = l.skewRecent[1:]
}
Expand Down
18 changes: 14 additions & 4 deletions tsnet/sync_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,22 @@ func TestNoteSkew(t *testing.T) {
if l.clockSkew != 100*ms || l.skewWarned {
t.Errorf("delayed sample: skew %v warned %v", l.clockSkew, l.skewWarned)
}
// the clock really moves: after skewWindow samples the old ones are gone.
note(100 * ms)
// the clock really moves: after 2 samples that are far above the old ones, they are dropped.
note(700 * ms)
if l.clockSkew != 100*ms {
t.Errorf("first moved sample: skew %v", l.clockSkew)
}
note(700 * ms)
if l.clockSkew != 700*ms || !l.skewWarned || len(l.skewRecent) != 2 {
t.Errorf("moved clock: skew %v warned %v window %v", l.clockSkew, l.skewWarned, l.skewRecent)
}
// a small move is not a step: the old samples age out after skewWindow samples.
for range skewWindow {
note(700 * ms)
note(720 * ms)
}
if l.clockSkew != 700*ms || !l.skewWarned {
t.Errorf("moved clock: skew %v warned %v", l.clockSkew, l.skewWarned)
if l.clockSkew != 720*ms {
t.Errorf("small move: skew %v", l.clockSkew)
}
// back below the limit but not by the margin: still warned (no flapping).
for range skewWindow {
Expand Down
Loading