Skip to content

Commit

Permalink
Merge pull request #6 from acmcelwee/master
Browse files Browse the repository at this point in the history
Add --execution-limit to test varied lambda handler deadlines
  • Loading branch information
blmayer authored Apr 28, 2023
2 parents 7c592c3 + 6ed8e01 commit 3aab684
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
23 changes: 18 additions & 5 deletions awslambdarpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,13 +28,14 @@ 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/blmayer/awslambdarpc/client"
)
Expand All @@ -42,11 +45,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
Expand All @@ -57,6 +61,7 @@ Examples:
func main() {
addr := "localhost:8080"
payload := []byte("{}")
executionLimit := 15 * time.Second
var eventFile string

for i := 1; i < len(os.Args); i++ {
Expand Down Expand Up @@ -92,6 +97,14 @@ func main() {
case "-d", "--data":
i++
payload = []byte(os.Args[i])
case "-l", "--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)
Expand All @@ -102,7 +115,7 @@ func main() {
}
}

res, err := client.Invoke(addr, payload)
res, err := client.Invoke(addr, payload, executionLimit)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(-2)
Expand Down
9 changes: 7 additions & 2 deletions client/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client
import (
"fmt"
"net/rpc"
"time"

"github.com/aws/aws-lambda-go/lambda/messages"
)
Expand All @@ -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) ([]byte, error) {
request := messages.InvokeRequest{Payload: data}
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
Expand Down

0 comments on commit 3aab684

Please sign in to comment.