-
Notifications
You must be signed in to change notification settings - Fork 2
/
timeout.go
61 lines (48 loc) · 1.16 KB
/
timeout.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
package tik
import (
"math"
"sync"
list "github.com/andy2046/gopie/pkg/dll"
)
type (
// Callback function to trigger when timeout expires.
Callback func()
// Timeout represents user timeout logic.
Timeout struct {
// absolute expiration time
expires uint64
// callbk func when expires
callbk Callback
// timeout list if pending on wheel or expiry queue
pending *list.List
// pointer to ticker
papa *Ticker
element *list.Element
iwheel uint8
islot uint8
m sync.RWMutex
}
)
func newTimeout(cb Callback) *Timeout {
to := &Timeout{}
to.init(cb)
return to
}
// init initialize timeout.
func (to *Timeout) init(cb Callback) {
to.callbk = cb
to.iwheel = math.MaxUint8
to.islot = math.MaxUint8
}
// Pending returns true if timeout is in timing wheel, false otherwise.
func (to *Timeout) Pending() bool {
to.m.RLock()
defer to.m.RUnlock()
return to.pending != nil && to.papa != nil && to.pending != to.papa.expired
}
// Expired returns true if timeout is in expired queue, false otherwise.
func (to *Timeout) Expired() bool {
to.m.RLock()
defer to.m.RUnlock()
return to.pending != nil && to.papa != nil && to.pending == to.papa.expired
}