Skip to content

Commit

Permalink
Fix scheduler for clock shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
barnybug committed May 20, 2018
1 parent e5499d3 commit 820c4cc
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions util/time.go
Expand Up @@ -32,10 +32,6 @@ func NewScheduler(offset time.Duration, d time.Duration) *Scheduler {
panic(errors.New("non-positive interval for NewScheduler"))
}

now := time.Now()
next := NextSchedule(now, offset, d)
dnext := next.Sub(now)

// Give the channel a 1-element time buffer.
// If the client falls behind while reading, we drop ticks
// on the floor until the client catches up.
Expand All @@ -44,14 +40,16 @@ func NewScheduler(offset time.Duration, d time.Duration) *Scheduler {
C: c,
}

time.AfterFunc(dnext, func() {
go func() {
for {
c <- time.Now()
next = next.Add(d)
dnext = next.Sub(time.Now())
now := time.Now()
next := NextSchedule(now, offset, d)
dnext := next.Sub(now)
time.Sleep(dnext)
c <- time.Now()
}
})

}()

return t
}
Expand Down

0 comments on commit 820c4cc

Please sign in to comment.