From c012a7c90f772501f767746d924dd2420fc4ead0 Mon Sep 17 00:00:00 2001 From: Daniel Rocha Date: Tue, 31 Jan 2023 16:43:29 +0100 Subject: [PATCH] refactor: move `Sync()` to client --- cmd/htp/main.go | 8 ++++---- pkg/htp/client.go | 35 +++++++++++++++++++++++++++++++++++ pkg/htp/service.go | 35 ----------------------------------- pkg/htp/service_unix.go | 2 +- pkg/htp/service_windows.go | 2 +- 5 files changed, 41 insertions(+), 41 deletions(-) delete mode 100644 pkg/htp/service.go diff --git a/cmd/htp/main.go b/cmd/htp/main.go index 53f9a47..f671c3b 100644 --- a/cmd/htp/main.go +++ b/cmd/htp/main.go @@ -44,17 +44,17 @@ func buildRootCommand() *cobra.Command { } trace := &htp.SyncTrace{ - Before: func(i int) bool { return i < count }, - After: func(i int, round *htp.SyncRound) bool { + Before: func(model *htp.SyncModel) bool { return model.Count() < count }, + After: func(model *htp.SyncModel, round *htp.SyncRound) bool { logInfo( - silent, "(%d/%d) offset: %+.3f (±%.3f) seconds", i+1, + silent, "(%d/%d) offset: %+.3f (±%.3f) seconds", model.Count(), count, model.Offset().Sec(), model.Margin().Sec()) return true }, } logInfo(silent, "Syncing with %s ...", host) - if err := htp.Sync(client, model, trace); err != nil { + if err := client.Sync(model, trace); err != nil { return err } diff --git a/pkg/htp/client.go b/pkg/htp/client.go index cf50434..83d4bde 100644 --- a/pkg/htp/client.go +++ b/pkg/htp/client.go @@ -16,6 +16,11 @@ type SyncClient struct { tRecv NanoSec } +type SyncTrace struct { + Before func(model *SyncModel) bool + After func(model *SyncModel, round *SyncRound) bool +} + func NewSyncClient(host string, timeout time.Duration) (*SyncClient, error) { s := &SyncClient{} @@ -67,3 +72,33 @@ func (s *SyncClient) Round() (*SyncRound, error) { Receive: s.tRecv, }, nil } + +func (s *SyncClient) Sync(model *SyncModel, trace *SyncTrace) error { + for { + if !trace.Before(model) { + break + } + + // We wait until the next (estimated) best time to send a request which + // will reduce the error margin. + // + // I.e., we time our request based on the current model so that the + // server will reply in a "full" second (see README). + model.Sleep() + + round, err := s.Round() + if err != nil { + return err + } + + if err := model.Update(round); err != nil { + return err + } + + if !trace.After(model, round) { + break + } + } + + return nil +} diff --git a/pkg/htp/service.go b/pkg/htp/service.go deleted file mode 100644 index 14ca26d..0000000 --- a/pkg/htp/service.go +++ /dev/null @@ -1,35 +0,0 @@ -package htp - -type SyncTrace struct { - Before func(i int) bool - After func(i int, round *SyncRound) bool -} - -func Sync(client *SyncClient, model *SyncModel, trace *SyncTrace) error { - for i := 0; ; i++ { - if !trace.Before(i) { - break - } - - model.Sleep() - - round, err := client.Round() - if err != nil { - return err - } - - if err := model.Update(round); err != nil { - return err - } - - if !trace.After(i, round) { - break - } - } - - return nil -} - -func SyncSystem(model *SyncModel) error { - return syncSystem(model) -} diff --git a/pkg/htp/service_unix.go b/pkg/htp/service_unix.go index 4000e20..872604f 100644 --- a/pkg/htp/service_unix.go +++ b/pkg/htp/service_unix.go @@ -4,7 +4,7 @@ package htp import "golang.org/x/sys/unix" -func syncSystem(model *SyncModel) error { +func SyncSystem(model *SyncModel) error { tv := unix.NsecToTimeval(model.Now().UnixNano()) return unix.Settimeofday(&tv) } diff --git a/pkg/htp/service_windows.go b/pkg/htp/service_windows.go index fb557e8..b1ae66f 100644 --- a/pkg/htp/service_windows.go +++ b/pkg/htp/service_windows.go @@ -7,7 +7,7 @@ import ( "os/exec" ) -func syncSystem(model *SyncModel) error { +func SyncSystem(model *SyncModel) error { arg := fmt.Sprintf("Set-Date -Adjust $([TimeSpan]::FromSeconds(%+.3f))", -model.Offset().Sec()) return exec.Command("powershell", "-Command", arg).Run() }