Skip to content

Commit

Permalink
refactor: move Sync() to client
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Jan 31, 2023
1 parent 8b1180b commit c012a7c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
8 changes: 4 additions & 4 deletions cmd/htp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/htp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
}
35 changes: 0 additions & 35 deletions pkg/htp/service.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/htp/service_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/htp/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit c012a7c

Please sign in to comment.