-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvoke.go
81 lines (67 loc) · 1.94 KB
/
invoke.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"net/rpc"
"time"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/google/uuid"
)
type Option func(*LambdaRPCClient)
type LambdaRPCClient struct {
// address is the address of the locally running lambda.
address string
// executionLimit is the maximum allowed duration of the lambda request
executionLimit time.Duration
// serviceMethod is the name of the RPC method that is called
serviceMethod string
}
// WithServiceMethod sets the service method for the RPC call.
func WithServiceMethod(serviceMethod string) Option {
return func(lambda *LambdaRPCClient) {
lambda.serviceMethod = serviceMethod
}
}
// NewLambdaLambdaRPCClient is a constructor for LambdaRPCClient struct.
func NewLambdaLambdaRPCClient(address string, executionLimit time.Duration, options ...Option) LambdaRPCClient {
lambdaRPC := LambdaRPCClient{
address: address,
executionLimit: executionLimit,
serviceMethod: "Function.Invoke",
}
for _, option := range options {
option(&lambdaRPC)
}
return lambdaRPC
}
// Invoke sends an RPC request to invoke a lambda function with the given payload data.
func (l LambdaRPCClient) Invoke(data []byte) (messages.InvokeResponse, error) {
deadline := time.Now().Add(l.executionLimit)
request := messages.InvokeRequest{
Payload: data,
RequestId: uuid.New().String(),
Deadline: messages.InvokeRequest_Timestamp{
Seconds: deadline.Unix(),
Nanos: int64(deadline.Nanosecond()),
},
}
client, err := rpc.Dial("tcp", l.address)
if err != nil {
return messages.InvokeResponse{}, fmt.Errorf(
"[in lambdalocal.invoke] rpcDial error, address '%s': %w",
l.address,
err,
)
}
defer func() {
_ = client.Close()
}()
var response messages.InvokeResponse
err = client.Call(l.serviceMethod, request, &response)
if err != nil {
return messages.InvokeResponse{}, fmt.Errorf(
"[in lambdalocal.invoke] client.Call error: %w",
err,
)
}
return response, nil
}