@@ -19,56 +19,56 @@ extension NewLambdaRuntime {
19
19
var channel : Channel
20
20
var handler : NewLambdaChannelHandler < NewLambdaRuntime >
21
21
}
22
-
22
+
23
23
struct StateMachine {
24
24
enum Action {
25
25
case none
26
26
case createHandler( andConnection: Bool )
27
-
27
+
28
28
case requestNextInvocation( NewLambdaChannelHandler < NewLambdaRuntime > , succeedStartPromise: EventLoopPromise < Void > ? )
29
-
29
+
30
30
case reportInvocationResult( LambdaRequestID , Result < ByteBuffer ? , Error > , pipelineNextInvocationRequest: Bool , NewLambdaChannelHandler < NewLambdaRuntime > )
31
31
case reportStartupError( Error , NewLambdaChannelHandler < NewLambdaRuntime > )
32
-
32
+
33
33
case invokeHandler( Handler , Invocation , ByteBuffer )
34
-
34
+
35
35
case failRuntime( Error , startPomise: EventLoopPromise < Void > ? )
36
36
}
37
-
37
+
38
38
private enum State {
39
39
case initialized
40
40
case starting( EventLoopPromise < Void > ? )
41
41
case connected( Connection , EventLoopPromise < Void > ? )
42
42
case handlerCreated( Handler , EventLoopPromise < Void > ? )
43
43
case handlerCreationFailed( Error , EventLoopPromise < Void > ? )
44
44
case reportingStartupError( Connection , Error , EventLoopPromise < Void > ? )
45
-
45
+
46
46
case waitingForInvocation( Connection , Handler )
47
47
case executingInvocation( Connection , Handler , LambdaRequestID )
48
48
case reportingInvocationResult( Connection , Handler , nextInvocationRequestPipelined: Bool )
49
-
49
+
50
50
case failed( Error )
51
51
}
52
-
52
+
53
53
private var markShutdown : Bool
54
54
private var state : State
55
-
55
+
56
56
init ( ) {
57
57
self . markShutdown = false
58
58
self . state = . initialized
59
59
}
60
-
60
+
61
61
mutating func start( connection: Connection ? , promise: EventLoopPromise < Void > ? ) -> Action {
62
62
switch self . state {
63
63
case . initialized:
64
64
if let connection = connection {
65
65
self . state = . connected( connection, promise)
66
66
return . createHandler( andConnection: false )
67
67
}
68
-
68
+
69
69
self . state = . starting( promise)
70
70
return . createHandler( andConnection: true )
71
-
71
+
72
72
case . starting,
73
73
. connected,
74
74
. handlerCreated,
@@ -81,7 +81,7 @@ extension NewLambdaRuntime {
81
81
preconditionFailure ( " Invalid state: \( self . state) " )
82
82
}
83
83
}
84
-
84
+
85
85
mutating func handlerCreated( _ handler: Handler ) -> Action {
86
86
switch self . state {
87
87
case . initialized,
@@ -92,20 +92,20 @@ extension NewLambdaRuntime {
92
92
. reportingInvocationResult,
93
93
. reportingStartupError:
94
94
preconditionFailure ( " Invalid state: \( self . state) " )
95
-
95
+
96
96
case . starting( let promise) :
97
97
self . state = . handlerCreated( handler, promise)
98
98
return . none
99
-
99
+
100
100
case . connected( let connection, let promise) :
101
101
self . state = . waitingForInvocation( connection, handler)
102
102
return . requestNextInvocation( connection. handler, succeedStartPromise: promise)
103
-
103
+
104
104
case . failed:
105
105
return . none
106
106
}
107
107
}
108
-
108
+
109
109
mutating func handlerCreationFailed( _ error: Error ) -> Action {
110
110
switch self . state {
111
111
case . initialized,
@@ -116,20 +116,20 @@ extension NewLambdaRuntime {
116
116
. reportingInvocationResult,
117
117
. reportingStartupError:
118
118
preconditionFailure ( " Invalid state: \( self . state) " )
119
-
119
+
120
120
case . starting( let promise) :
121
121
self . state = . handlerCreationFailed( error, promise)
122
122
return . none
123
-
123
+
124
124
case . connected( let connection, let promise) :
125
125
self . state = . reportingStartupError( connection, error, promise)
126
126
return . reportStartupError( error, connection. handler)
127
-
127
+
128
128
case . failed:
129
129
return . none
130
130
}
131
131
}
132
-
132
+
133
133
mutating func httpConnectionCreated(
134
134
_ connection: Connection
135
135
) -> Action {
@@ -141,24 +141,24 @@ extension NewLambdaRuntime {
141
141
. reportingInvocationResult,
142
142
. reportingStartupError:
143
143
preconditionFailure ( " Invalid state: \( self . state) " )
144
-
144
+
145
145
case . starting( let promise) :
146
146
self . state = . connected( connection, promise)
147
147
return . none
148
-
148
+
149
149
case . handlerCreated( let handler, let promise) :
150
150
self . state = . waitingForInvocation( connection, handler)
151
151
return . requestNextInvocation( connection. handler, succeedStartPromise: promise)
152
-
152
+
153
153
case . handlerCreationFailed( let error, let promise) :
154
154
self . state = . reportingStartupError( connection, error, promise)
155
155
return . reportStartupError( error, connection. handler)
156
-
156
+
157
157
case . failed:
158
158
return . none
159
159
}
160
160
}
161
-
161
+
162
162
mutating func httpChannelConnectFailed( _ error: Error ) -> Action {
163
163
switch self . state {
164
164
case . initialized,
@@ -168,24 +168,24 @@ extension NewLambdaRuntime {
168
168
. reportingInvocationResult,
169
169
. reportingStartupError:
170
170
preconditionFailure ( " Invalid state: \( self . state) " )
171
-
171
+
172
172
case . starting( let promise) :
173
173
self . state = . failed( error)
174
174
return . failRuntime( error, startPomise: promise)
175
-
175
+
176
176
case . handlerCreated( _, let promise) :
177
177
self . state = . failed( error)
178
178
return . failRuntime( error, startPomise: promise)
179
-
179
+
180
180
case . handlerCreationFailed( let error, let promise) :
181
181
self . state = . failed( error)
182
182
return . failRuntime( error, startPomise: promise)
183
-
183
+
184
184
case . failed:
185
185
return . none
186
186
}
187
187
}
188
-
188
+
189
189
mutating func newInvocationReceived( _ invocation: Invocation , _ body: ByteBuffer ) -> Action {
190
190
switch self . state {
191
191
case . initialized,
@@ -197,16 +197,16 @@ extension NewLambdaRuntime {
197
197
. reportingInvocationResult,
198
198
. reportingStartupError:
199
199
preconditionFailure ( " Invalid state: \( self . state) " )
200
-
200
+
201
201
case . waitingForInvocation( let connection, let handler) :
202
- self . state = . executingInvocation( connection, handler, . init ( uuidString: invocation. requestID) !)
202
+ self . state = . executingInvocation( connection, handler, LambdaRequestID ( uuidString: invocation. requestID) !)
203
203
return . invokeHandler( handler, invocation, body)
204
-
204
+
205
205
case . failed:
206
206
return . none
207
207
}
208
208
}
209
-
209
+
210
210
mutating func acceptedReceived( ) -> Action {
211
211
switch self . state {
212
212
case . initialized,
@@ -216,27 +216,27 @@ extension NewLambdaRuntime {
216
216
. handlerCreationFailed,
217
217
. executingInvocation:
218
218
preconditionFailure ( " Invalid state: \( self . state) " )
219
-
219
+
220
220
case . waitingForInvocation:
221
221
preconditionFailure ( " TODO: fixme " )
222
-
222
+
223
223
case . reportingStartupError( _, let error, let promise) :
224
224
self . state = . failed( error)
225
225
return . failRuntime( error, startPomise: promise)
226
-
226
+
227
227
case . reportingInvocationResult( let connection, let handler, true ) :
228
228
self . state = . waitingForInvocation( connection, handler)
229
229
return . none
230
-
230
+
231
231
case . reportingInvocationResult( let connection, let handler, false ) :
232
232
self . state = . waitingForInvocation( connection, handler)
233
233
return . requestNextInvocation( connection. handler, succeedStartPromise: nil )
234
-
234
+
235
235
case . failed:
236
236
return . none
237
237
}
238
238
}
239
-
239
+
240
240
mutating func errorResponseReceived( _ errorResponse: ErrorResponse ) -> Action {
241
241
switch self . state {
242
242
case . initialized,
@@ -246,49 +246,45 @@ extension NewLambdaRuntime {
246
246
. handlerCreationFailed,
247
247
. executingInvocation:
248
248
preconditionFailure ( " Invalid state: \( self . state) " )
249
-
249
+
250
250
case . waitingForInvocation:
251
251
let error = LambdaRuntimeError . controlPlaneErrorResponse ( errorResponse)
252
252
self . state = . failed( error)
253
253
return . failRuntime( error, startPomise: nil )
254
-
254
+
255
255
case . reportingStartupError( _, let error, let promise) :
256
256
self . state = . failed( error)
257
257
return . failRuntime( error, startPomise: promise)
258
-
258
+
259
259
case . reportingInvocationResult:
260
260
let error = LambdaRuntimeError . controlPlaneErrorResponse ( errorResponse)
261
261
self . state = . failed( error)
262
262
return . failRuntime( error, startPomise: nil )
263
-
263
+
264
264
case . failed:
265
265
return . none
266
266
}
267
267
}
268
-
269
- mutating func handlerError( _ error: Error ) {
270
-
271
- }
272
-
273
- mutating func channelInactive( ) {
274
-
275
- }
276
-
268
+
269
+ mutating func handlerError( _: Error ) { }
270
+
271
+ mutating func channelInactive( ) { }
272
+
277
273
mutating func invocationFinished( _ result: Result < ByteBuffer ? , Error > ) -> Action {
278
274
switch self . state {
279
275
case . initialized,
280
- . starting,
281
- . handlerCreated,
282
- . handlerCreationFailed,
283
- . connected,
284
- . waitingForInvocation,
285
- . reportingStartupError,
286
- . reportingInvocationResult:
276
+ . starting,
277
+ . handlerCreated,
278
+ . handlerCreationFailed,
279
+ . connected,
280
+ . waitingForInvocation,
281
+ . reportingStartupError,
282
+ . reportingInvocationResult:
287
283
preconditionFailure ( " Invalid state: \( self . state) " )
288
-
284
+
289
285
case . failed:
290
286
return . none
291
-
287
+
292
288
case . executingInvocation( let connection, let handler, let requestID) :
293
289
let pipelining = true
294
290
self . state = . reportingInvocationResult( connection, handler, nextInvocationRequestPipelined: pipelining)
0 commit comments