-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflows.go
348 lines (298 loc) · 9.52 KB
/
flows.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
package flow
import (
"context"
"fmt"
"io"
"net/http"
"os"
"reflect"
"runtime"
"sync"
"sync/atomic"
"time"
fdk "github.com/fnproject/fdk-go"
)
// TODO take this off pkg level to get a handle on a flow ?
type HTTPRequest struct {
Headers http.Header
Method string
Body []byte
}
type HTTPResponse struct {
StatusCode int32
Headers http.Header
Body []byte
}
type Flow interface {
InvokeFunction(functionID string, arg *HTTPRequest) FlowFuture
Supply(action interface{}) FlowFuture
Delay(duration time.Duration) FlowFuture
CompletedValue(value interface{}) FlowFuture // value can be an error
EmptyFuture() FlowFuture
AllOf(futures ...FlowFuture) FlowFuture
AnyOf(futures ...FlowFuture) FlowFuture
}
type FlowFuture interface {
Get() (chan interface{}, chan error)
// Get result as the given type. E.g. for use with ThenCompose
GetType(t reflect.Type) (chan interface{}, chan error)
ThenApply(action interface{}) FlowFuture
ThenCompose(action interface{}) FlowFuture
ThenCombine(other FlowFuture, action interface{}) FlowFuture
WhenComplete(action interface{}) FlowFuture
ThenAccept(action interface{}) FlowFuture
AcceptEither(other FlowFuture, action interface{}) FlowFuture
ApplyToEither(other FlowFuture, action interface{}) FlowFuture
ThenAcceptBoth(other FlowFuture, action interface{}) FlowFuture
ThenRun(action interface{}) FlowFuture
Handle(action interface{}) FlowFuture
Exceptionally(action interface{}) FlowFuture
ExceptionallyCompose(action interface{}) FlowFuture
Complete(value interface{}) bool
}
var debugMu uint32 // atomics are faster than mu lock/unlock
var httpClient *http.Client
// UseHTTPClient allows the default http client to be overriden
// for calls to the flow service. This function must be called
// prior to flows.WithFlow to take effect (e.g. from an init method)
func UseHTTPClient(client *http.Client) {
httpClient = client
}
// Debug enables internal library debugging
func Debug(withDebug bool) {
// go won't cast bool to uint32, you better believe it
var bint uint32
if withDebug {
bint++
}
atomic.StoreUint32(&debugMu, bint)
debug("Enabled debugging")
}
// Log to stderr when Debug mode is enabled
func Log(msg string) {
debug(msg)
}
func debug(msg string) {
if atomic.LoadUint32(&debugMu) == 1 {
fmt.Fprintln(os.Stderr, msg)
}
}
var actions = make(map[string]interface{})
func getActionKey(actionFunc interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(actionFunc).Pointer()).Name()
}
// RegisterAction registers a go function so it can be used as an action
// in a flow stage
func RegisterAction(actionFunc interface{}) {
if reflect.TypeOf(actionFunc).Kind() != reflect.Func {
panic("Action must be a function!")
}
actions[getActionKey(actionFunc)] = actionFunc
}
var cfMtx = &sync.Mutex{}
var cf *flow
func CurrentFlow() Flow {
cfMtx.Lock()
defer cfMtx.Unlock()
if cf == nil {
panic("Tried accessing unintialized flow")
}
return cf
}
func WithFlow(fn fdk.Handler) fdk.Handler {
return fdk.HandlerFunc(func(ctx context.Context, in io.Reader, out io.Writer) {
codec := newCodec(ctx, in, out)
if codec.isContinuation() {
initFlow(codec, false)
handleInvocation(codec)
return
}
initFlow(codec, true)
defer cf.commit()
debug("Invoking user's main flow function")
// TODO do we want separate reader/writer here?
fn.Serve(ctx, in, out)
debug("Completed invocation of user's main flow function")
})
}
func initFlow(codec codec, shouldCreate bool) {
client := newFlowClient()
var flowID string
if shouldCreate {
flowID = client.createFlow(codec.getFunctionID())
debug(fmt.Sprintf("Created new flow %v", flowID))
} else {
flowID = codec.getFlowID()
debug(fmt.Sprintf("Awakened flow %v", flowID))
}
cfMtx.Lock()
defer cfMtx.Unlock()
cf = &flow{
client: client,
flowID: flowID,
codec: codec,
}
}
type flow struct {
client flowClient
flowID string
codec codec
}
type flowFuture struct {
*flow
stageID string
returnType reflect.Type
}
// wraps result to runtime.Caller()
type codeLoc struct {
file string
line int
ok bool
}
func (cl *codeLoc) String() string {
if cl.ok {
return fmt.Sprintf("%s:%d", cl.file, cl.line)
}
return "unknown"
}
func newCodeLoc() *codeLoc {
_, file, line, ok := runtime.Caller(2)
return &codeLoc{file: file, line: line, ok: ok}
}
func (cf *flow) commit() {
cf.client.commit(cf.flowID)
}
func returnTypeForFunc(fn interface{}) reflect.Type {
t := reflect.ValueOf(fn).Type()
if t.NumOut() > 0 {
return t.Out(0)
}
return nil
}
func (cf *flow) continuationFuture(stageID string, fn interface{}) *flowFuture {
return &flowFuture{flow: cf, stageID: stageID, returnType: returnTypeForFunc(fn)}
}
func (cf *flow) Supply(action interface{}) FlowFuture {
sid := cf.client.supply(cf.flowID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (cf *flow) Delay(duration time.Duration) FlowFuture {
sid := cf.client.delay(cf.flowID, duration, newCodeLoc())
return &flowFuture{flow: cf, stageID: sid}
}
func (cf *flow) CompletedValue(value interface{}) FlowFuture {
sid := cf.client.completedValue(cf.flowID, value, newCodeLoc())
return &flowFuture{flow: cf, stageID: sid, returnType: reflect.TypeOf(value)}
}
func (cf *flow) InvokeFunction(functionID string, arg *HTTPRequest) FlowFuture {
sid := cf.client.invokeFunction(cf.flowID, functionID, arg, newCodeLoc())
return &flowFuture{
flow: cf,
stageID: sid,
returnType: reflect.TypeOf(new(HTTPResponse)),
}
}
func (cf *flow) EmptyFuture() FlowFuture {
sid := cf.client.emptyFuture(cf.flowID, newCodeLoc())
return &flowFuture{flow: cf, stageID: sid}
}
func futureCids(futures ...FlowFuture) []string {
var sids []string
for _, f := range futures {
ff := f.(*flowFuture)
sids = append(sids, ff.stageID)
}
return sids
}
func (cf *flow) AllOf(futures ...FlowFuture) FlowFuture {
sid := cf.client.allOf(cf.flowID, futureCids(futures...), newCodeLoc())
return &flowFuture{flow: cf, stageID: sid}
}
func (cf *flow) AnyOf(futures ...FlowFuture) FlowFuture {
sid := cf.client.anyOf(cf.flowID, futureCids(futures...), newCodeLoc())
// If all dependent futures are of the same type, we can introspect
// the type as a convenience. Otherwise, we have no way of determining
// the return type at runtime
var introspected reflect.Type
for i, f := range futures {
if ff, ok := f.(*flowFuture); ok {
if i == 0 {
introspected = ff.returnType
} else if ff.returnType != introspected {
// different types
introspected = nil
break
}
introspected = ff.returnType
continue
}
// unknown type
introspected = nil
break
}
debug(fmt.Sprintf("Introspected return type %v\n", introspected))
return &flowFuture{
flow: cf,
stageID: sid,
returnType: introspected,
}
}
func (f *flowFuture) Get() (chan interface{}, chan error) {
return f.client.getAsync(f.flowID, f.stageID, f.returnType)
}
func (f *flowFuture) GetType(t reflect.Type) (chan interface{}, chan error) {
return f.client.getAsync(f.flowID, f.stageID, t)
}
func (f *flowFuture) ThenApply(action interface{}) FlowFuture {
sid := f.client.thenApply(f.flowID, f.stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) ThenCompose(action interface{}) FlowFuture {
sid := f.client.thenCompose(f.flowID, f.stageID, action, newCodeLoc())
// no type information available for inner future
return &flowFuture{flow: cf, stageID: sid}
}
func (f *flowFuture) ThenCombine(other FlowFuture, action interface{}) FlowFuture {
sid := f.client.thenCombine(f.flowID, f.stageID, other.(*flowFuture).stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) WhenComplete(action interface{}) FlowFuture {
sid := f.client.whenComplete(f.flowID, f.stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) ThenAccept(action interface{}) FlowFuture {
sid := f.client.thenAccept(f.flowID, f.stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) AcceptEither(other FlowFuture, action interface{}) FlowFuture {
sid := f.client.acceptEither(f.flowID, f.stageID, other.(*flowFuture).stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) ApplyToEither(other FlowFuture, action interface{}) FlowFuture {
sid := f.client.applyToEither(f.flowID, f.stageID, other.(*flowFuture).stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) ThenAcceptBoth(other FlowFuture, action interface{}) FlowFuture {
sid := f.client.thenAcceptBoth(f.flowID, f.stageID, other.(*flowFuture).stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) ThenRun(action interface{}) FlowFuture {
sid := f.client.thenRun(f.flowID, f.stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) Handle(action interface{}) FlowFuture {
sid := f.client.handle(f.flowID, f.stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) Exceptionally(action interface{}) FlowFuture {
sid := f.client.exceptionally(f.flowID, f.stageID, action, newCodeLoc())
return cf.continuationFuture(sid, action)
}
func (f *flowFuture) ExceptionallyCompose(action interface{}) FlowFuture {
sid := f.client.exceptionallyCompose(f.flowID, f.stageID, action, newCodeLoc())
// no type information available for inner future
return &flowFuture{flow: cf, stageID: sid}
}
func (f *flowFuture) Complete(value interface{}) bool {
return f.client.complete(f.flowID, f.stageID, value, newCodeLoc())
}