From cfbd9dc1bdabcbcc3bad9f45b4b1d93ac3c7cb4f Mon Sep 17 00:00:00 2001 From: Adam McElwee Date: Thu, 27 Apr 2023 14:23:40 -0600 Subject: [PATCH 1/2] Add --execution-limit to test varied lambda handler deadlines --- awslambdarpc.go | 26 +++++++++++++++++++++----- client/rpc.go | 4 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/awslambdarpc.go b/awslambdarpc.go index 0e98d64..7df4463 100644 --- a/awslambdarpc.go +++ b/awslambdarpc.go @@ -26,14 +26,16 @@ the contents of a file, events/input.json as payload: awslambdarpc -a localhost:3000 -e events/input.json You can do passing the data directly with the -d flag: - awslambdarpc -a localhost:3000 -d '{"body": "Hello World!"}' + awslambdarpc -a localhost:3000 -d '{"body": "Hello World!"}' */ package main import ( "os" + "time" + "github.com/aws/aws-lambda-go/lambda/messages" "github.com/blmayer/awslambdarpc/client" ) @@ -42,11 +44,12 @@ Usage: awslambdarpc [options] Available options: -a - --address the address of your local running function, defaults to localhost:8080 + --address the address of your local running function, defaults to localhost:8080 -e - --event path to the event JSON to be used as input + --event path to the event JSON to be used as input -d - --data data passed to the function as input, in JSON format, defaults to "{}" + --data data passed to the function as input, in JSON format, defaults to "{}" + --execution-limit maximum execution limit for your handler, expressed as a duration, defaults to 15s help -h --help show this help @@ -57,6 +60,7 @@ Examples: func main() { addr := "localhost:8080" payload := []byte("{}") + executionLimit := 15 * time.Second var eventFile string for i := 1; i < len(os.Args); i++ { @@ -92,6 +96,14 @@ func main() { case "-d", "--data": i++ payload = []byte(os.Args[i]) + case "--execution-limit": + i++ + duration, err := time.ParseDuration(os.Args[i]) + if err != nil { + os.Stderr.WriteString("error parsing execution limit: " + err.Error() + "\n") + os.Exit(-6) + } + executionLimit = duration case "-h", "--help", "help": println(help) os.Exit(0) @@ -102,7 +114,11 @@ func main() { } } - res, err := client.Invoke(addr, payload) + deadline := time.Now().Add(executionLimit) + res, err := client.Invoke(addr, payload, messages.InvokeRequest_Timestamp{ + Seconds: deadline.Unix(), + Nanos: int64(deadline.Nanosecond()), + }) if err != nil { os.Stderr.WriteString(err.Error() + "\n") os.Exit(-2) diff --git a/client/rpc.go b/client/rpc.go index 456235a..bc3013c 100644 --- a/client/rpc.go +++ b/client/rpc.go @@ -15,8 +15,8 @@ import ( // to the lambda function as body. // If the lambda returned an error then this function will return // the error message in the error interface -func Invoke(addr string, data []byte) ([]byte, error) { - request := messages.InvokeRequest{Payload: data} +func Invoke(addr string, data []byte, deadline messages.InvokeRequest_Timestamp) ([]byte, error) { + request := messages.InvokeRequest{Payload: data, Deadline: deadline} client, err := rpc.Dial("tcp", addr) if err != nil { return nil, err From 6ed8e011c50e75602dac27f87e8a6e0c85931757 Mon Sep 17 00:00:00 2001 From: Adam McElwee Date: Fri, 28 Apr 2023 09:39:14 -0600 Subject: [PATCH 2/2] Address PR feedback: add short form -l + pass duration to Invoke --- awslambdarpc.go | 11 ++++------- client/rpc.go | 9 +++++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/awslambdarpc.go b/awslambdarpc.go index 7df4463..dd47a2d 100644 --- a/awslambdarpc.go +++ b/awslambdarpc.go @@ -18,6 +18,8 @@ Available options: path to the event JSON to be used as input -d, --data data passed to the function as input, in JSON format, defaults to "{}" + -l, --execution-limit + maximum execution limit for your handler, expressed as a duration, defaults to 15s help, -h, --help show this help @@ -35,7 +37,6 @@ import ( "os" "time" - "github.com/aws/aws-lambda-go/lambda/messages" "github.com/blmayer/awslambdarpc/client" ) @@ -96,7 +97,7 @@ func main() { case "-d", "--data": i++ payload = []byte(os.Args[i]) - case "--execution-limit": + case "-l", "--execution-limit": i++ duration, err := time.ParseDuration(os.Args[i]) if err != nil { @@ -114,11 +115,7 @@ func main() { } } - deadline := time.Now().Add(executionLimit) - res, err := client.Invoke(addr, payload, messages.InvokeRequest_Timestamp{ - Seconds: deadline.Unix(), - Nanos: int64(deadline.Nanosecond()), - }) + res, err := client.Invoke(addr, payload, executionLimit) if err != nil { os.Stderr.WriteString(err.Error() + "\n") os.Exit(-2) diff --git a/client/rpc.go b/client/rpc.go index bc3013c..87d0b01 100644 --- a/client/rpc.go +++ b/client/rpc.go @@ -5,6 +5,7 @@ package client import ( "fmt" "net/rpc" + "time" "github.com/aws/aws-lambda-go/lambda/messages" ) @@ -15,8 +16,12 @@ import ( // to the lambda function as body. // If the lambda returned an error then this function will return // the error message in the error interface -func Invoke(addr string, data []byte, deadline messages.InvokeRequest_Timestamp) ([]byte, error) { - request := messages.InvokeRequest{Payload: data, Deadline: deadline} +func Invoke(addr string, data []byte, executionLimit time.Duration) ([]byte, error) { + deadline := time.Now().Add(executionLimit) + request := messages.InvokeRequest{Payload: data, Deadline: messages.InvokeRequest_Timestamp{ + Seconds: deadline.Unix(), + Nanos: int64(deadline.Nanosecond()), + }} client, err := rpc.Dial("tcp", addr) if err != nil { return nil, err