MCPcopy Index your code
hub / github.com/aws/aws-lambda-go / handleInvoke

Function handleInvoke

lambda/invoke_loop.go:43–102  ·  view source on GitHub ↗

handleInvoke returns an error if the function panics, or some other non-recoverable error occurred

(invoke *invoke, handler *handlerOptions)

Source from the content-addressed store, hash-verified

41
42// handleInvoke returns an error if the function panics, or some other non-recoverable error occurred
43func handleInvoke(invoke *invoke, handler *handlerOptions) error {
44 // set the deadline
45 deadline, err := parseDeadline(invoke)
46 if err != nil {
47 return reportFailure(invoke, lambdaErrorResponse(err))
48 }
49 ctx, cancel := context.WithDeadline(handler.baseContext, deadline)
50 defer cancel()
51
52 // set the invoke metadata values
53 lc := lambdacontext.LambdaContext{
54 AwsRequestID: invoke.id,
55 InvokedFunctionArn: invoke.headers.Get(headerInvokedFunctionARN),
56 TenantID: invoke.headers.Get(headerTenantID),
57 }
58 if err := parseClientContext(invoke, &lc.ClientContext); err != nil {
59 return reportFailure(invoke, lambdaErrorResponse(err))
60 }
61 if err := parseCognitoIdentity(invoke, &lc.Identity); err != nil {
62 return reportFailure(invoke, lambdaErrorResponse(err))
63 }
64 ctx = lambdacontext.NewContext(ctx, &lc)
65
66 // set the trace id
67 traceID := invoke.headers.Get(headerTraceID)
68 if lambdacontext.MaxConcurrency() == 1 {
69 os.Setenv("_X_AMZN_TRACE_ID", traceID)
70 }
71 // nolint:staticcheck
72 ctx = context.WithValue(ctx, "x-amzn-trace-id", traceID)
73
74 // call the handler, marshal any returned error
75 response, invokeErr := callBytesHandlerFunc(ctx, invoke.payload.Bytes(), handler.handlerFunc)
76 if invokeErr != nil {
77 if err := reportFailure(invoke, invokeErr); err != nil {
78 return err
79 }
80 if invokeErr.ShouldExit {
81 return fmt.Errorf("calling the handler function resulted in a panic, the process should exit")
82 }
83 return nil
84 }
85 // if the response needs to be closed (ex: net.Conn, os.File), ensure it's closed before the next invoke to prevent a resource leak
86 if response, ok := response.(io.Closer); ok {
87 defer response.Close()
88 }
89
90 // if the response defines a content-type, plumb it through
91 contentType := contentTypeBytes
92 type ContentType interface{ ContentType() string }
93 if response, ok := response.(ContentType); ok {
94 contentType = response.ContentType()
95 }
96
97 if err := invoke.success(response, contentType); err != nil {
98 return fmt.Errorf("unexpected error occurred when sending the function functionResponse to the API: %v", err)
99 }
100

Callers 1

doRuntimeAPILoopFunction · 0.85

Calls 11

NewContextFunction · 0.92
MaxConcurrencyFunction · 0.92
parseDeadlineFunction · 0.85
reportFailureFunction · 0.85
lambdaErrorResponseFunction · 0.85
parseClientContextFunction · 0.85
parseCognitoIdentityFunction · 0.85
callBytesHandlerFuncFunction · 0.85
successMethod · 0.80
ContentTypeMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…