-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
state.go
142 lines (121 loc) · 2.9 KB
/
state.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
package frankenphp
import (
"slices"
"strconv"
"sync"
)
type stateID uint8
const (
// lifecycle states of a thread
stateBooting stateID = iota
stateShuttingDown
stateDone
// these states are safe to transition from at any time
stateInactive
stateReady
// states necessary for restarting workers
stateRestarting
stateYielding
// states necessary for transitioning between different handlers
stateTransitionRequested
stateTransitionInProgress
stateTransitionComplete
)
type threadState struct {
currentState stateID
mu sync.RWMutex
subscribers []stateSubscriber
}
type stateSubscriber struct {
states []stateID
ch chan struct{}
}
func newThreadState() *threadState {
return &threadState{
currentState: stateBooting,
subscribers: []stateSubscriber{},
mu: sync.RWMutex{},
}
}
func (ts *threadState) is(state stateID) bool {
ts.mu.RLock()
ok := ts.currentState == state
ts.mu.RUnlock()
return ok
}
func (ts *threadState) compareAndSwap(compareTo stateID, swapTo stateID) 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 {
// TODO: return the actual name for logging/metrics
return "state:" + strconv.Itoa(int(ts.get()))
}
func (ts *threadState) get() stateID {
ts.mu.RLock()
id := ts.currentState
ts.mu.RUnlock()
return id
}
func (ts *threadState) set(nextState stateID) {
ts.mu.Lock()
ts.currentState = nextState
ts.notifySubscribers(nextState)
ts.mu.Unlock()
}
func (ts *threadState) notifySubscribers(nextState stateID) {
if len(ts.subscribers) == 0 {
return
}
newSubscribers := []stateSubscriber{}
// notify subscribers to the state change
for _, sub := range ts.subscribers {
if !slices.Contains(sub.states, nextState) {
newSubscribers = append(newSubscribers, sub)
continue
}
close(sub.ch)
}
ts.subscribers = newSubscribers
}
// block until the thread reaches a certain state
func (ts *threadState) waitFor(states ...stateID) {
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
}
// safely request a state change from a different goroutine
func (ts *threadState) requestSafeStateChange(nextState stateID) bool {
ts.mu.Lock()
switch ts.currentState {
// disallow state changes if shutting down
case stateShuttingDown, stateDone:
ts.mu.Unlock()
return false
// ready and inactive are safe states to transition from
case stateReady, stateInactive:
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(stateReady, stateInactive, stateShuttingDown)
return ts.requestSafeStateChange(nextState)
}