-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspoke.go
113 lines (100 loc) · 2.44 KB
/
spoke.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
package timer
import (
"sync"
"sync/atomic"
"time"
)
// Spoke a spoke of the wheel.
type Spoke struct {
root taskEntry // sentinel list element, only &root, root.prev, and root.next are used
taskCounter *atomic.Int64
expiration atomic.Int64
mu sync.Mutex
}
func NewSpoke(taskCounter *atomic.Int64) *Spoke {
sp := &Spoke{
taskCounter: taskCounter,
}
sp.expiration.Store(-1)
sp.root.next = &sp.root
sp.root.prev = &sp.root
return sp
}
// Add the timer task to this list
func (sp *Spoke) Add(te *taskEntry) {
for done := false; !done; {
// Remove the timer task if it is already in any other list
// We do this outside of the sync block below to avoid deadlocking.
// We may retry until task.list becomes null.
te.remove()
if te.list.Load() == nil { // fast check.
sp.mu.Lock()
if te.list.Load() == nil { // double check but slow.
sp.pushBack(te)
done = true
}
sp.mu.Unlock()
}
}
}
// Remove the specified timer task from this list
func (sp *Spoke) Remove(te *taskEntry) {
sp.mu.Lock()
defer sp.mu.Unlock()
if te.list.Load() == sp {
sp.remove(te)
}
}
func (sp *Spoke) pushBack(te *taskEntry) {
at := sp.root.prev
te.prev = at
te.next = at.next
te.prev.next = te
te.next.prev = te
te.list.Store(sp)
sp.taskCounter.Add(1)
}
func (sp *Spoke) remove(te *taskEntry) {
te.prev.next = te.next
te.next.prev = te.prev
te.next = nil // avoid memory leaks
te.prev = nil // avoid memory leaks
te.list.Store(nil)
sp.taskCounter.Add(-1)
}
// Flush all task entries and apply the supplied function to each of them
func (sp *Spoke) Flush(f func(*taskEntry)) {
sp.mu.Lock()
defer sp.mu.Unlock()
for e := sp.root.next; e != &sp.root; e = sp.root.next {
sp.remove(e)
f(e)
}
sp.SetExpiration(-1)
}
// SetExpiration the spoke's expiration time
// Returns true if the expiration time is changed
func (sp *Spoke) SetExpiration(expirationMs int64) bool {
return sp.expiration.Swap(expirationMs) != expirationMs
}
// GetExpiration the spoke's expiration time
func (sp *Spoke) GetExpiration() int64 { return sp.expiration.Load() }
// Delay implements delayqueue.Delayed.
func (sp *Spoke) Delay() int64 {
delay := sp.GetExpiration() - time.Now().UnixMilli()
if delay < 0 {
return 0
}
return delay
}
// CompareSpoke compare two `Spoke` with expiration.
func CompareSpoke(sp1, sp2 *Spoke) int {
v1, v2 := sp1.GetExpiration(), sp2.GetExpiration()
if v1 < v2 {
return -1
}
if v1 > v2 {
return 1
}
return 0
}