-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
37 lines (30 loc) · 654 Bytes
/
timer.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
package timer
import (
"time"
)
type Timer struct {
wheel *Wheel
}
// New time range is wheelNum * gearSize = 20*time.Second = 20s
// number:20
// gear:time.Second
func New(wheelNum int, gearSize time.Duration) *Timer {
if wheelNum <= 0 || gearSize <= 0 {
return nil
}
return &Timer{
wheel: NewWheel(wheelNum, gearSize),
}
}
// After like time.After,accuracy means max 1/20 deviation
// the low accuracy the better performance.
func (t *Timer) After(d time.Duration) <-chan struct{} {
if t.wheel != nil {
return t.wheel.After(d)
}
return t.wheel.After(d)
}
// Stop stops the timewheel
func (t *Timer) Stop() {
t.wheel.Stop()
}