-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimer.go
97 lines (75 loc) · 1.75 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package timer
import "time"
type TimerOption func(*timerOption)
func WithTimeInterval(interval time.Duration) TimerOption {
return func(to *timerOption) {
to.interval = interval
}
}
func WithOperationBufferSize(n int) TimerOption {
return func(to *timerOption) {
to.operationBufferSize = n
}
}
type TickOption func(*tickOption)
func WithData(data interface{}) TickOption {
return func(to *tickOption) {
to.data = data
}
}
func WithCyclically() TickOption {
return func(to *tickOption) {
to.cyclically = true
}
}
func WithChan(C chan *Event) TickOption {
return func(to *tickOption) {
to.ch = C
to.chOutside = true
}
}
func WithHandler(handler func(*Event)) TickOption {
return func(to *tickOption) {
to.handler = handler
}
}
type Timer interface {
Add(d time.Duration, opts ...TickOption) Tick
// Close to close timer,
// all ticks set would be discarded.
Close()
// Pause the timer,
// all ticks won't continue after Timer.Movenon().
Pause()
// Continue the paused timer.
Moveon()
}
// Tick that set in Timer can be required from Timer.Add()
type Tick interface {
//To reset the data set at Timer.Time()
Reset(data interface{}) error
//To cancel the tick
Cancel() error
//Delay the tick
Delay(d time.Duration) error
//To get the channel called at Timer.Time(),
//you will get the same channel if set, if not and handler is nil,
//then a new created channel will be returned.
C() <-chan *Event
// Insert time
InsertTime() time.Time
// The tick duration original set
Duration() time.Duration
// Fired count
Fired() int64
}
type Event struct {
Duration time.Duration
InsertTIme time.Time
Data interface{}
Error error
}
// Entry
func NewTimer(opts ...TimerOption) Timer {
return newTimingwheel(opts...)
}