Skip to content

Commit caace58

Browse files
authored
Revert the lambda/ package changes to the v1.38.0 version (#489)
1 parent d00ce2e commit caace58

File tree

4 files changed

+5
-61
lines changed

4 files changed

+5
-61
lines changed

lambda/handler.go

-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type handlerOptions struct {
2828
jsonResponseIndentValue string
2929
enableSIGTERM bool
3030
sigtermCallbacks []func()
31-
setupFuncs []func() error
3231
}
3332

3433
type Option func(*handlerOptions)
@@ -103,15 +102,6 @@ func WithEnableSIGTERM(callbacks ...func()) Option {
103102
})
104103
}
105104

106-
// WithSetup enables capturing of errors or panics that occur before the function is ready to handle invokes.
107-
// The provided functions will be run a single time, in order, before the runtime reports itself ready to recieve invokes.
108-
// If any of the provided functions returns an error, or panics, the error will be serialized and reported to the Runtime API.
109-
func WithSetup(funcs ...func() error) Option {
110-
return Option(func(h *handlerOptions) {
111-
h.setupFuncs = append(h.setupFuncs, funcs...)
112-
})
113-
}
114-
115105
// handlerTakesContext returns whether the handler takes a context.Context as its first argument.
116106
func handlerTakesContext(handler reflect.Type) (bool, error) {
117107
switch handler.NumIn() {

lambda/invoke_loop.go

-31
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ func unixMS(ms int64) time.Time {
3131
func startRuntimeAPILoop(api string, handler Handler) error {
3232
client := newRuntimeAPIClient(api)
3333
h := newHandler(handler)
34-
35-
if err := handleSetup(client, h); err != nil {
36-
return err
37-
}
3834
for {
3935
invoke, err := client.next()
4036
if err != nil {
@@ -46,21 +42,6 @@ func startRuntimeAPILoop(api string, handler Handler) error {
4642
}
4743
}
4844

49-
// handleSetup returns an error if any of the handler's optional setup functions return and error or panic
50-
func handleSetup(client *runtimeAPIClient, handler *handlerOptions) error {
51-
for _, setup := range handler.setupFuncs {
52-
if setupErr := callSetupFunc(setup); setupErr != nil {
53-
errorPayload := safeMarshal(setupErr)
54-
log.Printf("%s", errorPayload)
55-
if err := client.initError(bytes.NewReader(errorPayload), contentTypeJSON); err != nil {
56-
return fmt.Errorf("unexpected error occurred when sending the setup error to the API: %v", err)
57-
}
58-
return fmt.Errorf("setting up the handler function resulted in an error, the process should exit")
59-
}
60-
}
61-
return nil
62-
}
63-
6445
// handleInvoke returns an error if the function panics, or some other non-recoverable error occurred
6546
func handleInvoke(invoke *invoke, handler *handlerOptions) error {
6647
// set the deadline
@@ -129,18 +110,6 @@ func reportFailure(invoke *invoke, invokeErr *messages.InvokeResponse_Error) err
129110
return nil
130111
}
131112

132-
func callSetupFunc(f func() error) (setupErr *messages.InvokeResponse_Error) {
133-
defer func() {
134-
if err := recover(); err != nil {
135-
setupErr = lambdaPanicResponse(err)
136-
}
137-
}()
138-
if err := f(); err != nil {
139-
return lambdaErrorResponse(err)
140-
}
141-
return nil
142-
}
143-
144113
func callBytesHandlerFunc(ctx context.Context, payload []byte, handler handlerFunc) (response io.Reader, invokeErr *messages.InvokeResponse_Error) {
145114
defer func() {
146115
if err := recover(); err != nil {

lambda/rpc_function.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,11 @@ func init() {
3333
}
3434

3535
func startFunctionRPC(port string, handler Handler) error {
36-
rpcFunction := NewFunction(handler)
37-
if len(rpcFunction.handler.setupFuncs) > 0 {
38-
runtimeAPIClient := newRuntimeAPIClient(os.Getenv("AWS_LAMBDA_RUNTIME_API"))
39-
if err := handleSetup(runtimeAPIClient, rpcFunction.handler); err != nil {
40-
return err
41-
}
42-
}
43-
4436
lis, err := net.Listen("tcp", "localhost:"+port)
4537
if err != nil {
4638
log.Fatal(err)
4739
}
48-
err = rpc.Register(rpcFunction)
40+
err = rpc.Register(NewFunction(handler))
4941
if err != nil {
5042
log.Fatal("failed to register handler function")
5143
}

lambda/runtime_api_client.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,11 @@ func newRuntimeAPIClient(address string) *runtimeAPIClient {
3737
client := &http.Client{
3838
Timeout: 0, // connections to the runtime API are never expected to time out
3939
}
40-
endpoint := "http://" + address + "/" + apiVersion + "/runtime"
40+
endpoint := "http://" + address + "/" + apiVersion + "/runtime/invocation/"
4141
userAgent := "aws-lambda-go/" + runtime.Version()
4242
return &runtimeAPIClient{endpoint, userAgent, client, bytes.NewBuffer(nil)}
4343
}
4444

45-
// initError connects to the Runtime API and reports that a failure occured during initialization.
46-
// Note: After calling this function, the caller should call os.Exit()
47-
func (c *runtimeAPIClient) initError(body io.Reader, contentType string) error {
48-
url := c.baseURL + "/init/error"
49-
return c.post(url, body, contentType)
50-
}
51-
5245
type invoke struct {
5346
id string
5447
payload []byte
@@ -60,7 +53,7 @@ type invoke struct {
6053
// Notes:
6154
// - An invoke is not complete until next() is called again!
6255
func (i *invoke) success(body io.Reader, contentType string) error {
63-
url := i.client.baseURL + "/invocation/" + i.id + "/response"
56+
url := i.client.baseURL + i.id + "/response"
6457
return i.client.post(url, body, contentType)
6558
}
6659

@@ -70,14 +63,14 @@ func (i *invoke) success(body io.Reader, contentType string) error {
7063
// - A Lambda Function continues to be re-used for future invokes even after a failure.
7164
// If the error is fatal (panic, unrecoverable state), exit the process immediately after calling failure()
7265
func (i *invoke) failure(body io.Reader, contentType string) error {
73-
url := i.client.baseURL + "/invocation/" + i.id + "/error"
66+
url := i.client.baseURL + i.id + "/error"
7467
return i.client.post(url, body, contentType)
7568
}
7669

7770
// next connects to the Runtime API and waits for a new invoke Request to be available.
7871
// Note: After a call to Done() or Error() has been made, a call to next() will complete the in-flight invoke.
7972
func (c *runtimeAPIClient) next() (*invoke, error) {
80-
url := c.baseURL + "/invocation/next"
73+
url := c.baseURL + "next"
8174
req, err := http.NewRequest(http.MethodGet, url, nil)
8275
if err != nil {
8376
return nil, fmt.Errorf("failed to construct GET request to %s: %v", url, err)

0 commit comments

Comments
 (0)