forked from raintank/statsdaemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticker.go
More file actions
29 lines (26 loc) · 805 Bytes
/
ticker.go
File metadata and controls
29 lines (26 loc) · 805 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
27
28
29
package main
import (
"time"
)
/*
func main() {
work := 0
for {
ticker := getAlignedTicker(time.Duration(1) * time.Second)
<- ticker.C
fmt.Println("starting work at", time.Now())
time.Sleep(time.Duration(work) * 5 * time.Millisecond)
fmt.Println(" done work at", time.Now())
work += 1
}
}
*/
// getAlignedTicker returns a ticker 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 off due
// to scheduling etc.
func getAlignedTicker(period time.Duration) *time.Ticker {
unix := time.Now().UnixNano()
diff := time.Duration(period - (time.Duration(unix) % period))
return time.NewTicker(diff)
}