forked from RussellLuo/timingwheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
38 lines (32 loc) · 761 Bytes
/
utils.go
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
30
31
32
33
34
35
36
37
38
package timingwheel
import (
"sync"
"time"
)
// truncate returns the result of rounding x toward zero to a multiple of m.
// If m <= 0, Truncate returns x unchanged.
func truncate(x, m int64) int64 {
if m <= 0 {
return x
}
return x - x%m
}
// timeToMs returns an integer number, which represents t in milliseconds.
func timeToMs(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
// msToTime returns the UTC time corresponding to the given Unix time,
// t milliseconds since January 1, 1970 UTC.
func msToTime(t int64) time.Time {
return time.Unix(0, t*int64(time.Millisecond)).UTC()
}
type waitGroupWrapper struct {
sync.WaitGroup
}
func (w *waitGroupWrapper) Wrap(cb func()) {
w.Add(1)
go func() {
cb()
w.Done()
}()
}