diff --git a/SYNC_DETAILS.md b/SYNC_DETAILS.md index 06365b9..391b40a 100644 --- a/SYNC_DETAILS.md +++ b/SYNC_DETAILS.md @@ -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 diff --git a/tsnet/sync.go b/tsnet/sync.go index 870724e..d26a2c6 100644 --- a/tsnet/sync.go +++ b/tsnet/sync.go @@ -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:] } diff --git a/tsnet/sync_internal_test.go b/tsnet/sync_internal_test.go index 2d51956..77d0a17 100644 --- a/tsnet/sync_internal_test.go +++ b/tsnet/sync_internal_test.go @@ -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 {