forked from etcd-io/etcd
/
timer.go
170 lines (141 loc) · 3.49 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
package raft
import (
"math/rand"
"sync"
"time"
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
type timer struct {
fireChan chan time.Time
stopChan chan bool
state int
rand *rand.Rand
minDuration time.Duration
maxDuration time.Duration
internalTimer *time.Timer
mutex sync.Mutex
}
const (
STOPPED = iota
READY
RUNNING
)
//------------------------------------------------------------------------------
//
// Constructors
//
//------------------------------------------------------------------------------
// Creates a new timer. Panics if a non-positive duration is used.
func newTimer(minDuration time.Duration, maxDuration time.Duration) *timer {
if minDuration <= 0 {
panic("raft: Non-positive minimum duration not allowed")
} else if maxDuration <= 0 {
panic("raft: Non-positive maximum duration not allowed")
} else if minDuration > maxDuration {
panic("raft: Minimum duration cannot be greater than maximum duration")
}
return &timer{
minDuration: minDuration,
maxDuration: maxDuration,
state: READY,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
stopChan: make(chan bool, 1),
fireChan: make(chan time.Time),
}
}
//------------------------------------------------------------------------------
//
// Accessors
//
//------------------------------------------------------------------------------
// Sets the minimum and maximum duration of the timer.
func (t *timer) setDuration(duration time.Duration) {
t.minDuration = duration
t.maxDuration = duration
}
//------------------------------------------------------------------------------
//
// Methods
//
//------------------------------------------------------------------------------
// Checks if the timer is currently running.
func (t *timer) running() bool {
return t.state == RUNNING
}
// Stops the timer and closes the channel.
func (t *timer) stop() {
t.mutex.Lock()
defer t.mutex.Unlock()
if t.internalTimer != nil {
t.internalTimer.Stop()
}
if t.state != STOPPED {
t.state = STOPPED
// non-blocking buffer
t.stopChan <- true
}
}
// Change the state of timer to ready
func (t *timer) ready() {
t.mutex.Lock()
defer t.mutex.Unlock()
if t.state == RUNNING {
panic("Timer is already running")
}
t.state = READY
t.stopChan = make(chan bool, 1)
t.fireChan = make(chan time.Time)
}
// Fire at the timer
func (t *timer) fire() {
select {
case t.fireChan <- time.Now():
return
default:
return
}
}
// Start the timer, this func will be blocked until the timer:
// (1) times out
// (2) stopped
// (3) fired
// Return false if stopped.
// Make sure the start func will not restart the stopped timer.
func (t *timer) start() bool {
t.mutex.Lock()
if t.state != READY {
t.mutex.Unlock()
return false
}
t.state = RUNNING
d := t.minDuration
if t.maxDuration > t.minDuration {
d += time.Duration(t.rand.Int63n(int64(t.maxDuration - t.minDuration)))
}
t.internalTimer = time.NewTimer(d)
internalTimer := t.internalTimer
t.mutex.Unlock()
// Wait for the timer channel, stop channel or fire channel.
stopped := false
select {
case <-internalTimer.C:
case <-t.fireChan:
case <-t.stopChan:
stopped = true
}
// Clean up timer and state.
t.mutex.Lock()
t.internalTimer.Stop()
t.internalTimer = nil
if stopped {
t.state = STOPPED
} else if t.state == RUNNING {
t.state = READY
}
t.mutex.Unlock()
return !stopped
}