Skip to content

Commit

Permalink
Display offset at each loop
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Nov 10, 2018
1 parent 889b4f9 commit 9ff87a6
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions htp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ func main() {

var (
offset int64
sleep int64 = 0
lo int64 = math.MinInt64
hi int64 = math.MaxInt64
)
for i := uint(0); i < *count; i++ {
time.Sleep(time.Duration(sleep))

t0 := time.Now().UnixNano()
resp, err := http.Head(*host)
t1 := time.Now().UnixNano()

if err != nil {
log.Fatal(err)
}
Expand All @@ -51,25 +55,24 @@ func main() {
lo = max(lo, t0-t2-Second)
hi = min(hi, t1-t2)
offset = (hi + lo) / 2
if !*quiet {
margin := (hi - lo) / 2
logger.Printf("offset: %+.3f (±%.3f) seconds\n",
float64(offset)/float64(Second),
float64(margin)/float64(Second))
}

sleep := offset - (t1-t0)/2 - t1%Second
sleep = offset - (t1-t0)/2 - t1%Second
for sleep < 0 {
sleep += Second
}
for sleep > Second {
sleep -= Second
}
time.Sleep(time.Duration(sleep))
}

now := time.Now().Add(time.Duration(-offset))
fmt.Printf("%s\n", now.Format(time.RFC3339Nano))
if !*quiet {
margin := (hi - lo) / 2
logger.Printf("offset: %.3f (± %.3f) seconds\n",
float64(offset)/float64(Second),
float64(margin)/float64(Second))
}
}

func min(a, b int64) int64 {
Expand Down

0 comments on commit 9ff87a6

Please # to comment.