-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
36 lines (32 loc) · 737 Bytes
/
timer.go
File metadata and controls
36 lines (32 loc) · 737 Bytes
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
package econ
import "time"
func newTimer(duration time.Duration) (t *time.Timer, drained bool) {
return time.NewTimer(duration), false
}
// closeTimer should be used as a deferred function
// in order to cleanly shut down a timer
func closeTimer(timer *time.Timer, drained *bool) {
if drained == nil {
panic("drained bool pointer is nil")
}
if !timer.Stop() {
if *drained {
return
}
<-timer.C
*drained = true
}
}
// resetTimer sets drained to false after resetting the timer.
func resetTimer(timer *time.Timer, duration time.Duration, drained *bool) {
if drained == nil {
panic("drained bool pointer is nil")
}
if !timer.Stop() {
if !*drained {
<-timer.C
}
}
timer.Reset(duration)
*drained = false
}