forked from grafana/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.go
More file actions
26 lines (24 loc) · 746 Bytes
/
clock.go
File metadata and controls
26 lines (24 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package clock
import "time"
// AlignedTick returns a tick channel so that, let's say interval is a second
// then it will tick at every whole second, or if it's 60s than it's every whole
// minute. Note that in my testing this is about .0001 to 0.0002 seconds later due
// to scheduling etc.
func AlignedTick(period time.Duration) <-chan time.Time {
// note that time.Ticker is not an interface,
// and that if we instantiate one, we can't write to its channel
// hence we can't leverage that type.
c := make(chan time.Time)
go func() {
for {
unix := time.Now().UnixNano()
diff := time.Duration(period - (time.Duration(unix) % period))
time.Sleep(diff)
select {
case c <- time.Now():
default:
}
}
}()
return c
}