11package main
22
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+
36import (
47 "bytes"
58 "encoding/json"
69 "fmt"
710 "github.com/go-chi/chi"
811 log "github.com/sirupsen/logrus"
9- "go.amzn.com/lambda/core"
1012 "go.amzn.com/lambda/core/statejson"
1113 "go.amzn.com/lambda/interop"
1214 "go.amzn.com/lambda/rapidcore"
@@ -38,8 +40,8 @@ const (
3840)
3941
4042func (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 ))
4345 if err != nil {
4446 return err
4547 }
@@ -62,7 +64,7 @@ type ErrorResponse struct {
6264 StackTrace []string `json:"stackTrace,omitempty"`
6365}
6466
65- func NewCustomInteropServer (lsOpts * LsOpts , delegate rapidcore. InteropServer , logCollector * LogCollector ) (server * CustomInteropServer ) {
67+ func NewCustomInteropServer (lsOpts * LsOpts , delegate interop. Server , logCollector * LogCollector ) (server * CustomInteropServer ) {
6668 server = & CustomInteropServer {
6769 delegate : delegate .(* rapidcore.Server ),
6870 port : lsOpts .InteropPort ,
@@ -99,9 +101,7 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate rapidcore.InteropServer, lo
99101 InvokedFunctionArn : invokeR .InvokedFunctionArn ,
100102 Payload : strings .NewReader (invokeR .Payload ), // r.Body,
101103 NeedDebugLogs : true ,
102- CorrelationID : "invokeCorrelationID" ,
103-
104- TraceID : invokeR .TraceId ,
104+ TraceID : invokeR .TraceId ,
105105 // TODO: set correct segment ID from request
106106 //LambdaSegmentID: "LambdaSegmentID", // r.Header.Get("X-Amzn-Segment-Id"),
107107 //CognitoIdentityID: "",
@@ -194,147 +194,81 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate rapidcore.InteropServer, lo
194194 return server
195195}
196196
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 )
204200}
205201
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 )
209205}
210206
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." )
222212 }
223-
224- return c .delegate .SendErrorResponse (invokeID , response )
213+ return c .delegate .SendInitErrorResponse (invokeID , response )
225214}
226215
227216func (c * CustomInteropServer ) GetCurrentInvokeID () string {
228- log .Traceln ("Function called" )
217+ log .Traceln ("GetCurrentInvokeID called" )
229218 return c .delegate .GetCurrentInvokeID ()
230219}
231220
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-
242221func (c * CustomInteropServer ) SendRuntimeReady () error {
243- log .Traceln ("Function called" )
222+ log .Traceln ("SendRuntimeReady called" )
244223 return c .delegate .SendRuntimeReady ()
245224}
246225
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 )
300229}
301230
302231func (c * CustomInteropServer ) Invoke (responseWriter http.ResponseWriter , invoke * interop.Invoke ) error {
303- log .Traceln ("Function called" )
232+ log .Traceln ("Invoke called" )
304233 return c .delegate .Invoke (responseWriter , invoke )
305234}
306235
307236func (c * CustomInteropServer ) FastInvoke (w http.ResponseWriter , i * interop.Invoke , direct bool ) error {
308- log .Traceln ("Function called" )
237+ log .Traceln ("FastInvoke called" )
309238 return c .delegate .FastInvoke (w , i , direct )
310239}
311240
312241func (c * CustomInteropServer ) Reserve (id string , traceID , lambdaSegmentID string ) (* rapidcore.ReserveResponse , error ) {
313- log .Traceln ("Function called" )
242+ log .Traceln ("Reserve called" )
314243 return c .delegate .Reserve (id , traceID , lambdaSegmentID )
315244}
316245
317246func (c * CustomInteropServer ) Reset (reason string , timeoutMs int64 ) (* statejson.ResetDescription , error ) {
318- log .Traceln ("Function called" )
247+ log .Traceln ("Reset called" )
319248 return c .delegate .Reset (reason , timeoutMs )
320249}
321250
322251func (c * CustomInteropServer ) AwaitRelease () (* statejson.InternalStateDescription , error ) {
323- log .Traceln ("Function called" )
252+ log .Traceln ("AwaitRelease called" )
324253 return c .delegate .AwaitRelease ()
325254}
326255
327- func (c * CustomInteropServer ) Shutdown (shutdown * interop.Shutdown ) * statejson.InternalStateDescription {
328- log .Traceln ("Function called" )
329- return c .delegate .Shutdown (shutdown )
330- }
331-
332256func (c * CustomInteropServer ) InternalState () (* statejson.InternalStateDescription , error ) {
333- log .Traceln ("Function called" )
257+ log .Traceln ("InternalState called" )
334258 return c .delegate .InternalState ()
335259}
336260
337261func (c * CustomInteropServer ) CurrentToken () * interop.Token {
338- log .Traceln ("Function called" )
262+ log .Traceln ("CurrentToken called" )
339263 return c .delegate .CurrentToken ()
340264}
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