-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgronos_messages_state.go
220 lines (197 loc) · 5.53 KB
/
gronos_messages_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
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
212
213
214
215
216
217
218
219
220
package gronos
import (
"fmt"
"runtime"
"sync"
"time"
"github.com/charmbracelet/log"
)
type RequestStatus[K comparable] struct {
KeyMessage[K]
RequestMessage[K, StatusState]
}
type RequestAlive[K comparable] struct {
KeyMessage[K]
RequestMessage[K, bool]
}
type RequestReason[K comparable] struct {
KeyMessage[K]
RequestMessage[K, error]
}
type RequestAllAlive[K comparable] struct {
RequestMessage[K, bool]
}
type RequestStatusAsync[K comparable] struct {
KeyMessage[K]
When StatusState
RequestMessage[K, struct{}]
}
var requestStatusPoolInited bool
var requestStatusPool sync.Pool
var requestAlivePoolInited bool
var requestAlivePool sync.Pool
var requestReasonPoolInited bool
var requestReasonPool sync.Pool
var requestAllAlivePoolInited bool
var requestAllAlivePool sync.Pool
var requestStatusAsyncPoolInited bool
var requestStatusAsyncPool sync.Pool
func MsgRequestStatus[K comparable](key K) (<-chan StatusState, *RequestStatus[K]) {
if !requestStatusPoolInited {
requestStatusPoolInited = true
requestStatusPool = sync.Pool{
New: func() any {
return &RequestStatus[K]{}
},
}
}
response := make(chan StatusState, 1)
msg := requestStatusPool.Get().(*RequestStatus[K])
msg.Key = key
msg.Response = response
return response, msg
}
func MsgRequestAlive[K comparable](key K) *RequestAlive[K] {
if !requestAlivePoolInited {
requestAlivePoolInited = true
requestAlivePool = sync.Pool{
New: func() any {
return &RequestAlive[K]{}
},
}
}
msg := requestAlivePool.Get().(*RequestAlive[K])
msg.Key = key
msg.Response = make(chan bool, 1)
return msg
}
func MsgRequestReason[K comparable](key K) *RequestReason[K] {
if !requestReasonPoolInited {
requestReasonPoolInited = true
requestReasonPool = sync.Pool{
New: func() any {
return &RequestReason[K]{}
},
}
}
msg := requestReasonPool.Get().(*RequestReason[K])
msg.Key = key
msg.Response = make(chan error, 1)
return msg
}
func MsgRequestAllAlive[K comparable]() (<-chan bool, *RequestAllAlive[K]) {
if !requestAllAlivePoolInited {
requestAllAlivePoolInited = true
requestAllAlivePool = sync.Pool{
New: func() any {
return &RequestAllAlive[K]{}
},
}
}
response := make(chan bool, 1)
msg := requestAllAlivePool.Get().(*RequestAllAlive[K])
msg.Response = response
return response, msg
}
func MsgRequestStatusAsync[K comparable](key K, when StatusState) (<-chan struct{}, *RequestStatusAsync[K]) {
if !requestStatusAsyncPoolInited {
requestStatusAsyncPoolInited = true
requestStatusAsyncPool = sync.Pool{
New: func() any {
return &RequestStatusAsync[K]{}
},
}
}
response := make(chan struct{}, 1)
msg := requestStatusAsyncPool.Get().(*RequestStatusAsync[K])
msg.Key = key
msg.When = when
msg.Response = response
return response, msg
}
func (g *gronos[K]) handleStateMessage(state *gronosState[K], m *MessagePayload) (error, bool) {
switch msg := m.Message.(type) {
case *RequestStatus[K]:
log.Debug("[GronosMessage] [RequestStatus]", msg.Key)
defer requestStatusPool.Put(msg)
return g.handleRequestStatus(state, msg.Key, msg.Response), true
case *RequestAlive[K]:
log.Debug("[GronosMessage] [RequestAlive]", msg.Key)
defer requestAlivePool.Put(msg)
return g.handleRequestAlive(state, msg.Key, msg.Response), true
case *RequestReason[K]:
log.Debug("[GronosMessage] [RequestReason]", msg.Key)
defer requestReasonPool.Put(msg)
return g.handleRequestReason(state, msg.Key, msg.Response), true
case *RequestAllAlive[K]:
log.Debug("[GronosMessage] [RequestAllAlive]")
defer requestAllAlivePool.Put(msg)
return g.handleRequestAllAlive(state, msg.Response), true
case *RequestStatusAsync[K]:
log.Debug("[GronosMessage] [RequestStatusAsync]", msg.Key)
defer requestStatusAsyncPool.Put(msg)
return g.handleRequestStatusAsync(state, msg.Key, msg.When, msg.Response), true
}
return nil, false
}
func (g *gronos[K]) handleRequestStatus(state *gronosState[K], key K, response chan<- StatusState) error {
defer close(response)
var value any
var ok bool
if value, ok = state.mstatus.Load(key); !ok {
response <- StatusNotFound
// return fmt.Errorf("app not found (status property) %v", key)
return nil
}
response <- value.(StatusState)
return nil
}
func (g *gronos[K]) handleRequestAlive(state *gronosState[K], key K, response chan<- bool) error {
var value any
var ok bool
if value, ok = state.mali.Load(key); !ok {
return fmt.Errorf("app not found (alive property) %v", key)
}
response <- value.(bool)
close(response)
return nil
}
func (g *gronos[K]) handleRequestReason(state *gronosState[K], key K, response chan<- error) error {
var value any
var ok bool
if value, ok = state.mrea.Load(key); !ok {
return fmt.Errorf("app not found (reason property) %v", key)
}
response <- value.(error)
close(response)
return nil
}
func (g *gronos[K]) handleRequestAllAlive(state *gronosState[K], response chan<- bool) error {
var alive bool
state.mali.Range(func(key, value any) bool {
if value.(bool) {
alive = true
return false
}
return true
})
response <- alive
close(response)
log.Debug("[GronosMessage] all alive", alive)
return nil
}
func (g *gronos[K]) handleRequestStatusAsync(state *gronosState[K], key K, when StatusState, response chan<- struct{}) error {
go func() {
var currentState int
for currentState < stateNumber(when) {
if value, ok := state.mstatus.Load(key); ok {
currentState = stateNumber(value.(StatusState))
}
<-time.After(time.Second / 16)
runtime.Gosched()
}
log.Debug("[GronosMessage] status async", key, currentState)
close(response)
}()
return nil
}