-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.go
More file actions
153 lines (130 loc) · 2.65 KB
/
Copy pathtimer.go
File metadata and controls
153 lines (130 loc) · 2.65 KB
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
package dlt
import (
"container/heap"
"sync"
"sync/atomic"
"time"
)
type element struct {
Key string
Index int
Expire int64
}
// 过期队列
type expirationQueue []*element
func (q expirationQueue) Len() int {
return len(q)
}
func (q expirationQueue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
q[i].Index, q[j].Index = i, j
}
func (q expirationQueue) Less(i, j int) bool {
return q[i].Expire < q[j].Expire
}
func (q *expirationQueue) Pop() interface{} {
old := *q
n := len(old)
item := old[n-1]
item.Index = -1
*q = old[0 : n-1]
return item
}
func (q *expirationQueue) Push(x interface{}) {
n := len(*q)
item := x.(*element)
item.Index = n
*q = append(*q, item)
}
func (q *expirationQueue) Top() *element {
if q.Len() == 0 {
return nil
}
return (*q)[0]
}
func (q *expirationQueue) Update(item *element, expire int64) {
item.Expire = expire
heap.Fix(q, item.Index)
}
func (q *expirationQueue) Remove(item *element) {
heap.Remove(q, item.Index)
}
// 倒计时器
type CountdownTimer struct {
seconds int64
mutex sync.Mutex
q *expirationQueue
mapper map[string]*element
callback func(string)
}
// 创建倒计时器
func NewCountdownTimer(seconds int64, callback func(string)) *CountdownTimer {
q := make(expirationQueue, 0)
timer := CountdownTimer{
q: &q,
callback: callback,
seconds: seconds,
mapper: make(map[string]*element),
}
go timer.checkExpirationQueue()
return &timer
}
// 更新时间
func (timer *CountdownTimer) Update(key string) {
e := element{
Key: key,
Expire: time.Now().Unix() + atomic.LoadInt64(&timer.seconds),
}
timer.Remove(key)
timer.mutex.Lock()
defer timer.mutex.Unlock()
heap.Push(timer.q, &e)
timer.mapper[key] = &e
}
// 删除计时
func (timer *CountdownTimer) Remove(key string) {
timer.mutex.Lock()
defer timer.mutex.Unlock()
element, ok := timer.mapper[key]
if !ok {
return
}
delete(timer.mapper, key)
timer.q.Remove(element)
}
// 设置过期时间
func (timer *CountdownTimer) SetExpiration(seconds int64) {
if seconds > 0 {
atomic.StoreInt64(&timer.seconds, seconds)
}
}
// 检查过期队列
func (timer *CountdownTimer) checkExpirationQueue() {
t := time.NewTicker(time.Second * 1)
for {
select {
case <-t.C:
now := time.Now().Unix()
keys := make([]string, 0)
timer.mutex.Lock()
for {
if timer.q.Len() == 0 {
break
}
top := timer.q.Top()
if top == nil || top.Expire > now {
break
}
timer.q.Remove(top)
delete(timer.mapper, top.Key)
keys = append(keys, top.Key)
}
timer.mutex.Unlock()
if timer.callback != nil {
for _, key := range keys {
timer.callback(key)
}
}
}
}
}