This repository has been archived by the owner on May 7, 2022. It is now read-only.
forked from trustmaster/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.go
382 lines (349 loc) · 10.8 KB
/
component.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// The flow package is a framework for Flow-based programming in Go.
package flow
import (
"reflect"
"sync"
)
const (
// ComponentModeUndefined stands for a fallback component mode (Async).
ComponentModeUndefined = iota
// ComponentModeAsync stands for asynchronous functioning mode.
ComponentModeAsync
// ComponentModeSync stands for synchronous functioning mode.
ComponentModeSync
// ComponentModePool stands for async functioning with a fixed pool.
ComponentModePool
)
// DefaultComponentMode is the preselected functioning mode of all components being run.
var DefaultComponentMode = ComponentModeAsync
// Component is a generic flow component that has to be contained in concrete components.
// It stores network-specific information.
type Component struct {
// Is running flag indicates that the process is currently running.
IsRunning bool
// Net is a pointer to network to inform it when the process is started and over
// or to change its structure at run time.
Net *Graph
// Mode is component's functioning mode.
Mode int8
// PoolSize is used to define pool size when using ComponentModePool.
PoolSize uint8
// Term chan is used to terminate the process immediately without closing
// any channels.
Term chan struct{}
}
// Initalizable is the interface implemented by components/graphs with custom initialization code.
type Initializable interface {
Init()
}
// Finalizable is the interface implemented by components/graphs with extra finalization code.
type Finalizable interface {
Finish()
}
// Shutdowner is the interface implemented by components overriding default Shutdown() behavior.
type Shutdowner interface {
Shutdown()
}
// Looper is a long-running process which actively receives data from its ports
// using a Loop function
type Looper interface {
Loop()
}
// postHandler is used to bind handlers to a port
type portHandler struct {
onRecv reflect.Value
onClose reflect.Value
}
// RunProc runs event handling loop on component ports.
// It returns true on success or panics with error message and returns false on error.
func RunProc(c interface{}) bool {
// Check if passed interface is a valid pointer to struct
v := reflect.ValueOf(c)
if v.Kind() != reflect.Ptr || v.IsNil() {
panic("Argument of flow.Run() is not a valid pointer")
return false
}
vp := v
v = v.Elem()
if v.Kind() != reflect.Struct {
panic("Argument of flow.Run() is not a valid pointer to structure. Got type: " + vp.Type().Name())
return false
}
t := v.Type()
// Get internal state lock if available
hasLock := false
var locker sync.Locker
if lockField := v.FieldByName("StateLock"); lockField.IsValid() && lockField.Elem().IsValid() {
locker, hasLock = lockField.Interface().(sync.Locker)
}
// Call user init function if exists
if initable, ok := c.(Initializable); ok {
initable.Init()
}
// A group to wait for all inputs to be closed
inputsClose := new(sync.WaitGroup)
// A group to wait for all recv handlers to finish
handlersDone := new(sync.WaitGroup)
// Get the embedded flow.Component
vCom := v.FieldByName("Component")
isComponent := vCom.IsValid() && vCom.Type().Name() == "Component"
if !isComponent {
panic("Argument of flow.Run() is not a flow.Component")
}
// Get the component mode
componentMode := DefaultComponentMode
var poolSize uint8 = 0
if vComMode := vCom.FieldByName("Mode"); vComMode.IsValid() {
componentMode = int(vComMode.Int())
}
if vComPoolSize := vCom.FieldByName("PoolSize"); vComPoolSize.IsValid() {
poolSize = uint8(vComPoolSize.Uint())
}
// Create a slice of select cases and port handlers
cases := make([]reflect.SelectCase, 0, t.NumField())
handlers := make([]portHandler, 0, t.NumField())
// Make and listen on termination channel
vCom.FieldByName("Term").Set(reflect.MakeChan(vCom.FieldByName("Term").Type(), 0))
cases = append(cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: vCom.FieldByName("Term")})
handlers = append(handlers, portHandler{})
// Detect active components
looper, isLooper := c.(Looper)
// Iterate over struct fields and bind handlers
inputCount := 0
for i := 0; i < t.NumField(); i++ {
fv := v.Field(i)
ff := t.Field(i)
ft := fv.Type()
// Detect control channels
if fv.IsValid() && fv.Kind() == reflect.Chan && !fv.IsNil() && fv.CanSet() && (ft.ChanDir()&reflect.RecvDir) != 0 {
// Bind handlers for an input channel
cases = append(cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: fv})
h := portHandler{onRecv: vp.MethodByName("On" + ff.Name), onClose: vp.MethodByName("On" + ff.Name + "Close")}
handlers = append(handlers, h)
if h.onClose.IsValid() || h.onRecv.IsValid() {
// Add the input to the wait group
inputsClose.Add(1)
inputCount++
}
}
}
if inputCount == 0 && !isLooper {
panic("Components with no input ports are not supported")
}
// Prepare handler closures
recvHandler := func(onRecv, value reflect.Value) {
if hasLock {
locker.Lock()
}
valArr := [1]reflect.Value{value}
onRecv.Call(valArr[:])
if hasLock {
locker.Unlock()
}
handlersDone.Done()
}
closeHandler := func(onClose reflect.Value) {
if onClose.IsValid() {
// Lock the state and call OnClose handler
if hasLock {
locker.Lock()
}
onClose.Call([]reflect.Value{})
if hasLock {
locker.Unlock()
}
}
inputsClose.Done()
}
terminate := func(remaining int) {
if !vCom.FieldByName("IsRunning").Bool() {
return
}
vCom.FieldByName("IsRunning").SetBool(false)
for i := 0; i < remaining; i++ {
inputsClose.Done()
}
}
pop := func(index int) {
if len(cases) != len(handlers) {
panic("cases and handlers should be the same length")
} else if index < 0 || index >= len(cases) {
panic("invalid handler index")
}
if index < (len(cases)-1) && index >= 0 {
cases = append(cases[:index], cases[index+1:]...)
handlers = append(handlers[:index], handlers[index+1:]...)
} else if index == (len(cases) - 1) {
cases = cases[:index]
handlers = handlers[:index]
}
}
// closePorts closes all output channels of a process.
closePorts := func() {
// Iterate over struct fields
for i := 0; i < t.NumField(); i++ {
fv := v.Field(i)
ft := fv.Type()
vNet := vCom.FieldByName("Net")
// Detect and close send-only channels
if fv.IsValid() {
// TODO: likely needs to check fv.CanSet()
if fv.Kind() == reflect.Chan && (ft.ChanDir()&reflect.SendDir) != 0 && (ft.ChanDir()&reflect.RecvDir) == 0 {
if vNet.IsValid() && !vNet.IsNil() {
if vNet.Interface().(*Graph).DecSendChanRefCount(fv) {
fv.Close()
}
} else {
fv.Close()
}
} else if fv.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Chan {
ll := fv.Len()
if vNet.IsValid() && !vNet.IsNil() {
for i := 0; i < ll; i += 1 {
if vNet.Interface().(*Graph).DecSendChanRefCount(fv.Index(i)) {
fv.Index(i).Close()
}
}
} else {
for i := 0; i < ll; i += 1 {
fv.Index(i).Close()
}
}
}
}
}
}
// shutdown represents a standard process shutdown procedure.
shutdown := func() {
if s, ok := c.(Shutdowner); ok {
// Custom shutdown behavior
s.Shutdown()
} else {
// Call user finish function if exists
if finable, ok := c.(Finalizable); ok {
finable.Finish()
}
// Close all output ports if the process is still running
if vCom.FieldByName("IsRunning").Bool() {
closePorts()
}
}
}
// This accomodates the looper behaviour specifically.
// Because a looper does not rely on having a declared input handler, there is no blocking for inputsClosed.
// This opens a race condition for handlersDone.
handlersEst := make(chan bool, 1)
// Set mode to default if undefined
if componentMode == ComponentModeUndefined {
componentMode = DefaultComponentMode
}
// Set pool size of 1 for unspecified or non-pool modes
if !(componentMode == ComponentModePool && poolSize > 0) {
poolSize = 1
}
// Run the port handlers depending on component mode
go func() {
if isLooper {
defer func() {
terminate(inputCount)
handlersDone.Done()
}()
handlersDone.Add(1)
handlersEst <- true
looper.Loop()
return
}
for len(cases) > 1 {
// Pool mode, prefork limited goroutine pool for all inputs
var poolIndex uint8
poolWait := new(sync.WaitGroup)
once := new(sync.Once)
for poolIndex = 0; poolIndex < poolSize; poolIndex++ {
poolWait.Add(1)
go func() {
// TODO add pool of Loopers support
for {
chosen, recv, recvOK := reflect.Select(cases)
if !recvOK {
// Port has been closed
defer poolWait.Done()
once.Do(func() {
if chosen == 0 {
// Term signal
terminate(len(cases) - 1)
// Exit main loop by shortening cases to 0
cases = []reflect.SelectCase{}
} else {
// Close output down
closeHandler(handlers[chosen].onClose)
pop(chosen)
}
})
// NOTE: This is different from sync/pool mode because the pool has to completely shut down to safely remove a select case, then has to be rebuilt.
return
} else if handlers[chosen].onRecv.IsValid() {
handlersDone.Add(1)
if componentMode == ComponentModeAsync {
// Async Mode
go recvHandler(handlers[chosen].onRecv, recv)
} else {
// Sync/Pool Mode
recvHandler(handlers[chosen].onRecv, recv)
}
}
}
}()
}
select {
case handlersEst <- true:
default:
}
poolWait.Wait()
}
}()
// Indicate the process as running
<-handlersEst
vCom.FieldByName("IsRunning").SetBool(true)
go func() {
// Wait for all inputs to be closed
inputsClose.Wait()
// Wait all inport handlers to finish their job
handlersDone.Wait()
// Call shutdown handler (user or default)
shutdown()
// Get the embedded flow.Component and check if it belongs to a network
if vNet := vCom.FieldByName("Net"); vNet.IsValid() && !vNet.IsNil() {
if vNetCtr, hasNet := vNet.Interface().(netController); hasNet {
// Remove the instance from the network's WaitGroup
vNetCtr.getWait().Done()
}
}
}()
return true
}
// StopProc terminates the process if it is running.
// It doesn't close any in or out ports of the process, so it can be
// replaced without side effects.
func StopProc(c interface{}) bool {
// Check if passed interface is a valid pointer to struct
v := reflect.ValueOf(c)
if v.Kind() != reflect.Ptr || v.IsNil() {
panic("Argument of TermProc() is not a valid pointer")
return false
}
vp := v
v = v.Elem()
if v.Kind() != reflect.Struct {
panic("Argument of TermProc() is not a valid pointer to structure. Got type: " + vp.Type().Name())
return false
}
// Get the embedded flow.Component
vCom := v.FieldByName("Component")
isComponent := vCom.IsValid() && vCom.Type().Name() == "Component"
if !isComponent {
panic("Argument of TermProc() is not a flow.Component")
}
// Send the termination signal
vCom.FieldByName("Term").Close()
return true
}