-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.go
270 lines (243 loc) · 5.02 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package time
import (
log "code.google.com/p/log4go"
"sync"
itime "time"
)
const (
timerFormat = "2006-01-02 15:04:05"
infiniteDuration = itime.Duration(1<<63 - 1)
timerBatchExpire = 1000
)
var (
timerLazyDelay = 300 * itime.Millisecond
)
type TimerData struct {
Key string
expire itime.Time
fn func()
index int
next *TimerData
}
func (td *TimerData) Delay() itime.Duration {
return td.expire.Sub(itime.Now())
}
func (td *TimerData) ExpireString() string {
return td.expire.Format(timerFormat)
}
type Timer struct {
lock sync.Mutex
free *TimerData
timers []*TimerData
signal *itime.Timer
num int
}
// A heap must be initialized before any of the heap operations
// can be used. Init is idempotent with respect to the heap invariants
// and may be called whenever the heap invariants may have been invalidated.
// Its complexity is O(n) where n = h.Len().
//
func NewTimer(num int) (t *Timer) {
t = new(Timer)
t.init(num)
return t
}
// Init init the timer.
func (t *Timer) Init(num int) {
t.init(num)
}
func (t *Timer) init(num int) {
t.signal = itime.NewTimer(infiniteDuration)
t.timers = make([]*TimerData, 0, num)
t.num = num
t.grow()
go t.start()
}
func (t *Timer) grow() {
var (
i int
td *TimerData
tds = make([]TimerData, t.num)
)
t.free = &(tds[0])
td = t.free
for i = 1; i < t.num; i++ {
td.next = &(tds[i])
td = td.next
}
return
}
// get get a free timer data.
func (t *Timer) get() (td *TimerData) {
if td = t.free; td == nil {
t.grow()
td = t.free
}
t.free = td.next
return
}
// put put back a timer data.
func (t *Timer) put(td *TimerData) {
td.next = t.free
t.free = td
}
// Push pushes the element x onto the heap. The complexity is
// O(log(n)) where n = h.Len().
func (t *Timer) Add(expire itime.Duration, fn func()) (td *TimerData) {
t.lock.Lock()
td = t.get()
td.expire = itime.Now().Add(expire)
td.fn = fn
t.add(td)
t.lock.Unlock()
return
}
// Del removes the element at index i from the heap.
// The complexity is O(log(n)) where n = h.Len().
func (t *Timer) Del(td *TimerData) {
t.lock.Lock()
t.del(td)
t.put(td)
t.lock.Unlock()
return
}
// Push pushes the element x onto the heap. The complexity is
// O(log(n)) where n = h.Len().
func (t *Timer) add(td *TimerData) {
var d itime.Duration
td.index = len(t.timers)
// add to the minheap last node
t.timers = append(t.timers, td)
t.up(td.index)
if td.index == 0 {
// if first node, signal start goroutine
d = td.Delay()
t.signal.Reset(d)
if Debug {
log.Debug("timer: add reset delay %d ms", int64(d)/int64(itime.Millisecond))
}
}
if Debug {
log.Debug("timer: push item key: %s, expire: %s, index: %d", td.Key, td.ExpireString(), td.index)
}
return
}
func (t *Timer) del(td *TimerData) {
var (
i = td.index
last = len(t.timers) - 1
)
if i < 0 || i > last || t.timers[i] != td {
// already remove, usually by expire
if Debug {
log.Debug("timer del i: %d, last: %d, %p", i, last, td)
}
return
}
if i != last {
t.swap(i, last)
t.down(i, last)
t.up(i)
}
// remove item is the last node
t.timers[last].index = -1 // for safety
t.timers = t.timers[:last]
if Debug {
log.Debug("timer: remove item key: %s, expire: %s, index: %d", td.Key, td.ExpireString(), td.index)
}
return
}
// Set update timer data.
func (t *Timer) Set(td *TimerData, expire itime.Duration) {
t.lock.Lock()
t.del(td)
td.expire = itime.Now().Add(expire)
t.add(td)
t.lock.Unlock()
return
}
// start start the timer.
func (t *Timer) start() {
for {
t.expire()
<-t.signal.C
}
}
// expire removes the minimum element (according to Less) from the heap.
// The complexity is O(log(n)) where n = max.
// It is equivalent to Del(0).
func (t *Timer) expire() {
var (
i int
td *TimerData
d itime.Duration
)
t.lock.Lock()
for {
if len(t.timers) == 0 {
d = infiniteDuration
if Debug {
log.Debug("timer: no other instance")
}
break
}
td = t.timers[0]
if d = td.Delay(); d > 0 {
break
}
if td.fn == nil {
log.Warn("expire timer no fn")
} else {
if Debug {
log.Debug("timer key: %s, expire: %s, index: %d expired, call fn", td.Key, td.ExpireString(), td.index)
}
td.fn()
}
// let caller put back
t.del(td)
if i++; i >= timerBatchExpire {
break
}
}
t.signal.Reset(d)
if Debug {
log.Debug("timer: expier reset delay %d ms", int64(d)/int64(itime.Millisecond))
}
t.lock.Unlock()
return
}
func (t *Timer) up(j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !t.less(j, i) {
break
}
t.swap(i, j)
j = i
}
}
func (t *Timer) down(i, n int) {
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
break
}
j := j1 // left child
if j2 := j1 + 1; j2 < n && !t.less(j1, j2) {
j = j2 // = 2*i + 2 // right child
}
if !t.less(j, i) {
break
}
t.swap(i, j)
i = j
}
}
func (t *Timer) less(i, j int) bool {
return t.timers[i].expire.Before(t.timers[j].expire)
}
func (t *Timer) swap(i, j int) {
t.timers[i], t.timers[j] = t.timers[j], t.timers[i]
t.timers[i].index = i
t.timers[j].index = j
}