|
| 1 | +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 2 | + |
| 3 | +package lambda |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "strconv" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/aws/aws-lambda-go/lambda/messages" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + serializationErrorFormat = `{"errorType": "Runtime.SerializationError", "errorMessage": "%s"}` |
| 17 | + msPerS = int64(time.Second / time.Millisecond) |
| 18 | + nsPerMS = int64(time.Millisecond / time.Nanosecond) |
| 19 | +) |
| 20 | + |
| 21 | +// startRuntimeAPILoop will return an error if handling a particular invoke resulted in a non-recoverable error |
| 22 | +func startRuntimeAPILoop(ctx context.Context, api string, handler Handler) error { |
| 23 | + client := newRuntimeAPIClient(api) |
| 24 | + function := NewFunction(handler).withContext(ctx) |
| 25 | + for { |
| 26 | + invoke, err := client.next() |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + err = handleInvoke(invoke, function) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// handleInvoke returns an error if the function panics, or some other non-recoverable error occurred |
| 39 | +func handleInvoke(invoke *invoke, function *Function) error { |
| 40 | + functionRequest, err := convertInvokeRequest(invoke) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("unexpected error occured when parsing the invoke: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + functionResponse := &messages.InvokeResponse{} |
| 46 | + if err := function.Invoke(functionRequest, functionResponse); err != nil { |
| 47 | + return fmt.Errorf("unexpected error occured when invoking the handler: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + if functionResponse.Error != nil { |
| 51 | + payload := safeMarshal(functionResponse.Error) |
| 52 | + if err := invoke.failure(payload, contentTypeJSON); err != nil { |
| 53 | + return fmt.Errorf("unexpected error occured when sending the function error to the API: %v", err) |
| 54 | + } |
| 55 | + if functionResponse.Error.ShouldExit { |
| 56 | + return fmt.Errorf("calling the handler function resulted in a panic, the process should exit") |
| 57 | + } |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + if err := invoke.success(functionResponse.Payload, contentTypeJSON); err != nil { |
| 62 | + return fmt.Errorf("unexpected error occured when sending the function functionResponse to the API: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// convertInvokeRequest converts an invoke from the Runtime API, and unpacks it to be compatible with the shape of a `lambda.Function` InvokeRequest. |
| 69 | +func convertInvokeRequest(invoke *invoke) (*messages.InvokeRequest, error) { |
| 70 | + deadlineEpochMS, err := strconv.ParseInt(invoke.headers.Get(headerDeadlineMS), 10, 64) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("failed to parse contents of header: %s", headerDeadlineMS) |
| 73 | + } |
| 74 | + deadlineS := deadlineEpochMS / msPerS |
| 75 | + deadlineNS := (deadlineEpochMS % msPerS) * nsPerMS |
| 76 | + |
| 77 | + res := &messages.InvokeRequest{ |
| 78 | + InvokedFunctionArn: invoke.headers.Get(headerInvokedFunctionARN), |
| 79 | + XAmznTraceId: invoke.headers.Get(headerTraceID), |
| 80 | + Deadline: messages.InvokeRequest_Timestamp{ |
| 81 | + Seconds: deadlineS, |
| 82 | + Nanos: deadlineNS, |
| 83 | + }, |
| 84 | + Payload: invoke.payload, |
| 85 | + } |
| 86 | + |
| 87 | + clientContextJSON := invoke.headers.Get(headerClientContext) |
| 88 | + if clientContextJSON != "" { |
| 89 | + res.ClientContext = []byte(clientContextJSON) |
| 90 | + } |
| 91 | + |
| 92 | + cognitoIdentityJSON := invoke.headers.Get(headerCognitoIdentity) |
| 93 | + if cognitoIdentityJSON != "" { |
| 94 | + if err := json.Unmarshal([]byte(invoke.headers.Get(headerCognitoIdentity)), res); err != nil { |
| 95 | + return nil, fmt.Errorf("failed to unmarshal cognito identity json: %v", err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return res, nil |
| 100 | +} |
| 101 | + |
| 102 | +func safeMarshal(v interface{}) []byte { |
| 103 | + payload, err := json.Marshal(v) |
| 104 | + if err != nil { |
| 105 | + return []byte(fmt.Sprintf(serializationErrorFormat, err.Error())) |
| 106 | + } |
| 107 | + return payload |
| 108 | +} |
0 commit comments