-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathstate.go
More file actions
211 lines (178 loc) · 4.15 KB
/
state.go
File metadata and controls
211 lines (178 loc) · 4.15 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package state
import (
"slices"
"sync"
"sync/atomic"
"time"
)
type State int
const (
// lifecycle States of a thread
Reserved State = iota
Booting
BootRequested
ShuttingDown
Done
// these States are 'stable' and safe to transition from at any time
Inactive
Ready
// States necessary for restarting workers
Restarting
Yielding
// States necessary for transitioning between different handlers
TransitionRequested
TransitionInProgress
TransitionComplete
)
func (s State) String() string {
switch s {
case Reserved:
return "reserved"
case Booting:
return "booting"
case BootRequested:
return "boot requested"
case ShuttingDown:
return "shutting down"
case Done:
return "done"
case Inactive:
return "inactive"
case Ready:
return "ready"
case Restarting:
return "restarting"
case Yielding:
return "yielding"
case TransitionRequested:
return "transition requested"
case TransitionInProgress:
return "transition in progress"
case TransitionComplete:
return "transition complete"
default:
return "unknown"
}
}
type ThreadState struct {
currentState State
mu sync.RWMutex
subscribers []stateSubscriber
// how long threads have been waiting in stable states (unix ms, 0 = not waiting)
waitingSince atomic.Int64
}
type stateSubscriber struct {
states []State
ch chan struct{}
}
func NewThreadState() *ThreadState {
return &ThreadState{
currentState: Reserved,
subscribers: []stateSubscriber{},
mu: sync.RWMutex{},
}
}
func (ts *ThreadState) Is(state State) bool {
ts.mu.RLock()
ok := ts.currentState == state
ts.mu.RUnlock()
return ok
}
func (ts *ThreadState) CompareAndSwap(compareTo State, swapTo State) bool {
ts.mu.Lock()
ok := ts.currentState == compareTo
if ok {
ts.currentState = swapTo
ts.notifySubscribers(swapTo)
}
ts.mu.Unlock()
return ok
}
func (ts *ThreadState) Name() string {
return ts.Get().String()
}
func (ts *ThreadState) Get() State {
ts.mu.RLock()
id := ts.currentState
ts.mu.RUnlock()
return id
}
func (ts *ThreadState) Set(nextState State) {
ts.mu.Lock()
ts.currentState = nextState
ts.notifySubscribers(nextState)
ts.mu.Unlock()
}
func (ts *ThreadState) notifySubscribers(nextState State) {
if len(ts.subscribers) == 0 {
return
}
n := 0
for _, sub := range ts.subscribers {
if !slices.Contains(sub.states, nextState) {
ts.subscribers[n] = sub
n++
continue
}
close(sub.ch)
}
ts.subscribers = ts.subscribers[:n]
}
// WaitFor blocks until the thread reaches a certain state
func (ts *ThreadState) WaitFor(states ...State) {
ts.mu.Lock()
if slices.Contains(states, ts.currentState) {
ts.mu.Unlock()
return
}
sub := stateSubscriber{
states: states,
ch: make(chan struct{}),
}
ts.subscribers = append(ts.subscribers, sub)
ts.mu.Unlock()
<-sub.ch
}
// RequestSafeStateChange safely requests a state change from a different goroutine
func (ts *ThreadState) RequestSafeStateChange(nextState State) bool {
ts.mu.Lock()
switch ts.currentState {
// disallow state changes if shutting down or done
case ShuttingDown, Done, Reserved:
ts.mu.Unlock()
return false
// ready and inactive are safe states to transition from
case Ready, Inactive:
ts.currentState = nextState
ts.notifySubscribers(nextState)
ts.mu.Unlock()
return true
}
ts.mu.Unlock()
// wait for the state to change to a safe state
ts.WaitFor(Ready, Inactive, ShuttingDown)
return ts.RequestSafeStateChange(nextState)
}
// MarkAsWaiting hints that the thread reached a stable state and is waiting for requests or shutdown
func (ts *ThreadState) MarkAsWaiting(isWaiting bool) {
if isWaiting {
ts.waitingSince.Store(time.Now().UnixMilli())
} else {
ts.waitingSince.Store(0)
}
}
// IsInWaitingState returns true if a thread is waiting for a request or shutdown
func (ts *ThreadState) IsInWaitingState() bool {
return ts.waitingSince.Load() != 0
}
// WaitTime returns the time since the thread is waiting in a stable state in ms
func (ts *ThreadState) WaitTime() int64 {
since := ts.waitingSince.Load()
if since == 0 {
return 0
}
return time.Now().UnixMilli() - since
}
func (ts *ThreadState) SetWaitTime(t time.Time) {
ts.waitingSince.Store(t.UnixMilli())
}