1
1
package main
2
2
3
+ // Original implementation: lambda/rapidcore/server.go includes Server struct with state
4
+ // Server interface between Runtime API and this init: lambda/interop/model.go:358
5
+
3
6
import (
4
7
"bytes"
5
8
"encoding/json"
6
9
"fmt"
7
10
"github.com/go-chi/chi"
8
11
log "github.com/sirupsen/logrus"
9
- "go.amzn.com/lambda/core"
10
12
"go.amzn.com/lambda/core/statejson"
11
13
"go.amzn.com/lambda/interop"
12
14
"go.amzn.com/lambda/rapidcore"
@@ -38,8 +40,8 @@ const (
38
40
)
39
41
40
42
func (l * LocalStackAdapter ) SendStatus (status LocalStackStatus , payload []byte ) error {
41
- status_url := fmt .Sprintf ("%s/status/%s/%s" , l .UpstreamEndpoint , l .RuntimeId , status )
42
- _ , err := http .Post (status_url , "application/json" , bytes .NewReader (payload ))
43
+ statusUrl := fmt .Sprintf ("%s/status/%s/%s" , l .UpstreamEndpoint , l .RuntimeId , status )
44
+ _ , err := http .Post (statusUrl , "application/json" , bytes .NewReader (payload ))
43
45
if err != nil {
44
46
return err
45
47
}
@@ -62,7 +64,7 @@ type ErrorResponse struct {
62
64
StackTrace []string `json:"stackTrace,omitempty"`
63
65
}
64
66
65
- func NewCustomInteropServer (lsOpts * LsOpts , delegate rapidcore. InteropServer , logCollector * LogCollector ) (server * CustomInteropServer ) {
67
+ func NewCustomInteropServer (lsOpts * LsOpts , delegate interop. Server , logCollector * LogCollector ) (server * CustomInteropServer ) {
66
68
server = & CustomInteropServer {
67
69
delegate : delegate .(* rapidcore.Server ),
68
70
port : lsOpts .InteropPort ,
@@ -99,9 +101,7 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate rapidcore.InteropServer, lo
99
101
InvokedFunctionArn : invokeR .InvokedFunctionArn ,
100
102
Payload : strings .NewReader (invokeR .Payload ), // r.Body,
101
103
NeedDebugLogs : true ,
102
- CorrelationID : "invokeCorrelationID" ,
103
-
104
- TraceID : invokeR .TraceId ,
104
+ TraceID : invokeR .TraceId ,
105
105
// TODO: set correct segment ID from request
106
106
//LambdaSegmentID: "LambdaSegmentID", // r.Header.Get("X-Amzn-Segment-Id"),
107
107
//CognitoIdentityID: "",
@@ -194,147 +194,81 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate rapidcore.InteropServer, lo
194
194
return server
195
195
}
196
196
197
- func (c * CustomInteropServer ) StartAcceptingDirectInvokes () error {
198
- log .Traceln ("Function called" )
199
- err := c .localStackAdapter .SendStatus (Ready , []byte {})
200
- if err != nil {
201
- return err
202
- }
203
- return c .delegate .StartAcceptingDirectInvokes ()
197
+ func (c * CustomInteropServer ) SendResponse (invokeID string , headers map [string ]string , reader io.Reader , trailers http.Header , request * interop.CancellableRequest ) error {
198
+ log .Traceln ("SendResponse called" )
199
+ return c .delegate .SendResponse (invokeID , headers , reader , trailers , request )
204
200
}
205
201
206
- func (c * CustomInteropServer ) SendResponse (invokeID string , contentType string , response io. Reader ) error {
207
- log .Traceln ("Function called" )
208
- return c .delegate .SendResponse (invokeID , contentType , response )
202
+ func (c * CustomInteropServer ) SendErrorResponse (invokeID string , response * interop. ErrorResponse ) error {
203
+ log .Traceln ("SendErrorResponse called" )
204
+ return c .delegate .SendErrorResponse (invokeID , response )
209
205
}
210
206
211
- func (c * CustomInteropServer ) SendErrorResponse (invokeID string , response * interop.ErrorResponse ) error {
212
- is , err := c .InternalState ()
213
- if err != nil {
214
- return err
215
- }
216
- rs := is .Runtime .State
217
- if rs .Name == core .RuntimeInitErrorStateName {
218
- err = c .localStackAdapter .SendStatus (Error , response .Payload )
219
- if err != nil {
220
- return err
221
- }
207
+ // SendInitErrorResponse writes error response during init to a shared memory and sends GIRD FAULT.
208
+ func (c * CustomInteropServer ) SendInitErrorResponse (invokeID string , response * interop.ErrorResponse ) error {
209
+ log .Traceln ("SendInitErrorResponse called" )
210
+ if err := c .localStackAdapter .SendStatus (Error , response .Payload ); err != nil {
211
+ log .Fatalln ("Failed to send init error to LocalStack " + err .Error () + ". Exiting." )
222
212
}
223
-
224
- return c .delegate .SendErrorResponse (invokeID , response )
213
+ return c .delegate .SendInitErrorResponse (invokeID , response )
225
214
}
226
215
227
216
func (c * CustomInteropServer ) GetCurrentInvokeID () string {
228
- log .Traceln ("Function called" )
217
+ log .Traceln ("GetCurrentInvokeID called" )
229
218
return c .delegate .GetCurrentInvokeID ()
230
219
}
231
220
232
- func (c * CustomInteropServer ) CommitResponse () error {
233
- log .Traceln ("Function called" )
234
- return c .delegate .CommitResponse ()
235
- }
236
-
237
- func (c * CustomInteropServer ) SendRunning (running * interop.Running ) error {
238
- log .Traceln ("Function called" )
239
- return c .delegate .SendRunning (running )
240
- }
241
-
242
221
func (c * CustomInteropServer ) SendRuntimeReady () error {
243
- log .Traceln ("Function called" )
222
+ log .Traceln ("SendRuntimeReady called" )
244
223
return c .delegate .SendRuntimeReady ()
245
224
}
246
225
247
- func (c * CustomInteropServer ) SendDone (done * interop.Done ) error {
248
- log .Traceln ("Function called" )
249
- return c .delegate .SendDone (done )
250
- }
251
-
252
- func (c * CustomInteropServer ) SendDoneFail (fail * interop.DoneFail ) error {
253
- log .Traceln ("Function called" )
254
- return c .delegate .SendDoneFail (fail )
255
- }
256
-
257
- func (c * CustomInteropServer ) StartChan () <- chan * interop.Start {
258
- log .Traceln ("Function called" )
259
- return c .delegate .StartChan ()
260
- }
261
-
262
- func (c * CustomInteropServer ) InvokeChan () <- chan * interop.Invoke {
263
- log .Traceln ("Function called" )
264
- return c .delegate .InvokeChan ()
265
- }
266
-
267
- func (c * CustomInteropServer ) ResetChan () <- chan * interop.Reset {
268
- log .Traceln ("Function called" )
269
- return c .delegate .ResetChan ()
270
- }
271
-
272
- func (c * CustomInteropServer ) ShutdownChan () <- chan * interop.Shutdown {
273
- log .Traceln ("Function called" )
274
- return c .delegate .ShutdownChan ()
275
- }
276
-
277
- func (c * CustomInteropServer ) TransportErrorChan () <- chan error {
278
- log .Traceln ("Function called" )
279
- return c .delegate .TransportErrorChan ()
280
- }
281
-
282
- func (c * CustomInteropServer ) Clear () {
283
- log .Traceln ("Function called" )
284
- c .delegate .Clear ()
285
- }
286
-
287
- func (c * CustomInteropServer ) IsResponseSent () bool {
288
- log .Traceln ("Function called" )
289
- return c .delegate .IsResponseSent ()
290
- }
291
-
292
- func (c * CustomInteropServer ) SetInternalStateGetter (cb interop.InternalStateGetter ) {
293
- log .Traceln ("Function called" )
294
- c .delegate .SetInternalStateGetter (cb )
295
- }
296
-
297
- func (c * CustomInteropServer ) Init (i * interop.Start , invokeTimeoutMs int64 ) {
298
- log .Traceln ("Function called" )
299
- c .delegate .Init (i , invokeTimeoutMs )
226
+ func (c * CustomInteropServer ) Init (i * interop.Init , invokeTimeoutMs int64 ) error {
227
+ log .Traceln ("Init called" )
228
+ return c .delegate .Init (i , invokeTimeoutMs )
300
229
}
301
230
302
231
func (c * CustomInteropServer ) Invoke (responseWriter http.ResponseWriter , invoke * interop.Invoke ) error {
303
- log .Traceln ("Function called" )
232
+ log .Traceln ("Invoke called" )
304
233
return c .delegate .Invoke (responseWriter , invoke )
305
234
}
306
235
307
236
func (c * CustomInteropServer ) FastInvoke (w http.ResponseWriter , i * interop.Invoke , direct bool ) error {
308
- log .Traceln ("Function called" )
237
+ log .Traceln ("FastInvoke called" )
309
238
return c .delegate .FastInvoke (w , i , direct )
310
239
}
311
240
312
241
func (c * CustomInteropServer ) Reserve (id string , traceID , lambdaSegmentID string ) (* rapidcore.ReserveResponse , error ) {
313
- log .Traceln ("Function called" )
242
+ log .Traceln ("Reserve called" )
314
243
return c .delegate .Reserve (id , traceID , lambdaSegmentID )
315
244
}
316
245
317
246
func (c * CustomInteropServer ) Reset (reason string , timeoutMs int64 ) (* statejson.ResetDescription , error ) {
318
- log .Traceln ("Function called" )
247
+ log .Traceln ("Reset called" )
319
248
return c .delegate .Reset (reason , timeoutMs )
320
249
}
321
250
322
251
func (c * CustomInteropServer ) AwaitRelease () (* statejson.InternalStateDescription , error ) {
323
- log .Traceln ("Function called" )
252
+ log .Traceln ("AwaitRelease called" )
324
253
return c .delegate .AwaitRelease ()
325
254
}
326
255
327
- func (c * CustomInteropServer ) Shutdown (shutdown * interop.Shutdown ) * statejson.InternalStateDescription {
328
- log .Traceln ("Function called" )
329
- return c .delegate .Shutdown (shutdown )
330
- }
331
-
332
256
func (c * CustomInteropServer ) InternalState () (* statejson.InternalStateDescription , error ) {
333
- log .Traceln ("Function called" )
257
+ log .Traceln ("InternalState called" )
334
258
return c .delegate .InternalState ()
335
259
}
336
260
337
261
func (c * CustomInteropServer ) CurrentToken () * interop.Token {
338
- log .Traceln ("Function called" )
262
+ log .Traceln ("CurrentToken called" )
339
263
return c .delegate .CurrentToken ()
340
264
}
265
+
266
+ func (c * CustomInteropServer ) SetSandboxContext (sbCtx interop.SandboxContext ) {
267
+ log .Traceln ("SetSandboxContext called" )
268
+ c .delegate .SetSandboxContext (sbCtx )
269
+ }
270
+
271
+ func (c * CustomInteropServer ) SetInternalStateGetter (cb interop.InternalStateGetter ) {
272
+ log .Traceln ("SetInternalStateGetter called" )
273
+ c .delegate .InternalStateGetter = cb
274
+ }
0 commit comments