Skip to content

Commit f70722d

Browse files
committed
Treat client aborts like Caddy reverse_proxy
1 parent c7886b3 commit f70722d

3 files changed

Lines changed: 148 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ The default 4 MiB limit leaves some headroom, but unusually large headers may re
107107
| `503` | AWS rejected the invocation with `TooManyRequestsException`. |
108108
| `504` | Lambda invocation exceeded the configured timeout. |
109109

110+
If the client cancels the request during invocation, the handler returns without
111+
writing a response or invoking Caddy's error handlers.
112+
110113
The plugin does not generate application statuses such as `400`, `401`, `403`,
111114
or `404`; Lambda responses should supply those statuses when appropriate.
112115

@@ -206,9 +209,9 @@ Responses use the standard `statusCode`, `headers`, `cookies`, `body`, and
206209
`Set-Cookie` header.
207210

208211
The API Gateway v2 adapter maps Caddy's request path directly to `rawPath`;
209-
it does not trim a base path. AWS invocation errors, Lambda function
210-
errors, timeouts, throttling, and malformed responses are returned as Caddy
211-
handler errors.
212+
it does not trim a base path. AWS invocation errors (except client cancellations),
213+
Lambda function errors, timeouts, throttling, and malformed responses are returned
214+
as Caddy handler errors.
212215

213216
Every invocation has a request ID. If the incoming request includes an
214217
`X-Request-ID` header, its value is preserved; otherwise, caddy-lambda-licious
@@ -222,10 +225,14 @@ HTTP access logging remains Caddy's responsibility. This module does not log
222225
credentials, headers, or request/response bodies.
223226

224227
Successful Lambda invocations log their function name, duration, and request ID
225-
at `DEBUG`. Throttling is logged at `WARN`; other invocation failures are logged
226-
at `ERROR`. Failed invocations include the contextual error and are returned to
227-
Caddy for normal HTTP error handling, so Caddy may also log the resulting 5xx
228-
response.
228+
at `DEBUG`. Invocations canceled by the client are also logged at `DEBUG` and
229+
skip normal HTTP error handling. Throttling is logged at `WARN`; other invocation
230+
failures, including timeouts, are logged at `ERROR`. Failed invocations include
231+
the contextual error and are returned to Caddy for normal HTTP error handling,
232+
so Caddy may also log the resulting 5xx response.
233+
234+
Response body write failures are logged at `WARN` as "aborting with incomplete
235+
response" and abort the response with `http.ErrAbortHandler`.
229236

230237
## Development
231238

‎middleware.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ func (m *Lambda) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.H
174174
resp, err := m.invokeLambda(r.Context(), req, loggedRequestID)
175175

176176
if err != nil {
177+
if errors.Is(r.Context().Err(), context.Canceled) {
178+
return nil
179+
}
177180
return caddyhttp.Error(lambdaErrorStatus(err), err)
178181
}
179182

@@ -254,8 +257,11 @@ func (m *Lambda) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.H
254257
if reply.Meta.Status == http.StatusNoContent || reply.Meta.Status == http.StatusNotModified {
255258
return nil
256259
}
257-
_, err = w.Write(bodyBytes)
258-
return err
260+
if _, err := w.Write(bodyBytes); err != nil {
261+
m.log.Warn("aborting with incomplete response", zap.Error(err))
262+
panic(http.ErrAbortHandler)
263+
}
264+
return nil
259265
}
260266

261267
// newRequestID returns a collision-resistant correlation ID in the same
@@ -277,8 +283,8 @@ func lambdaErrorStatus(err error) int {
277283
return http.StatusBadGateway
278284
}
279285

280-
func (m *Lambda) invokeLambda(ctx context.Context, req any, requestID string) (payload []byte, err error) {
281-
ctx, cancel := context.WithTimeout(ctx, time.Duration(m.Timeout))
286+
func (m *Lambda) invokeLambda(parentCtx context.Context, req any, requestID string) (payload []byte, err error) {
287+
ctx, cancel := context.WithTimeout(parentCtx, time.Duration(m.Timeout))
282288
defer cancel()
283289

284290
fields := []zap.Field{zap.String("function", m.FunctionName)}
@@ -291,7 +297,9 @@ func (m *Lambda) invokeLambda(ctx context.Context, req any, requestID string) (p
291297
logFields := []zap.Field{zap.Duration("duration", time.Since(startTime))}
292298
if err != nil {
293299
logFields = append(logFields, zap.Error(err))
294-
if lambdaErrorStatus(err) == http.StatusServiceUnavailable {
300+
if errors.Is(parentCtx.Err(), context.Canceled) {
301+
log.Debug("Lambda invocation canceled by client", logFields...)
302+
} else if lambdaErrorStatus(err) == http.StatusServiceUnavailable {
295303
log.Warn("Lambda invocation failed", logFields...)
296304
} else {
297305
log.Error("Lambda invocation failed", logFields...)

‎middleware_test.go‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414
"github.com/aws/aws-sdk-go-v2/service/lambda"
1515
"github.com/aws/smithy-go"
1616
"github.com/caddyserver/caddy/v2"
17+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
1718
"github.com/google/uuid"
1819
"go.uber.org/zap"
20+
"go.uber.org/zap/zapcore"
1921
"go.uber.org/zap/zaptest/observer"
2022
)
2123

@@ -477,6 +479,125 @@ func TestInvokeLambdaReturnsTimeout(t *testing.T) {
477479
}
478480
}
479481

482+
type trackingResponseWriter struct {
483+
*httptest.ResponseRecorder
484+
wroteHeader bool
485+
writeErr error
486+
}
487+
488+
func (w *trackingResponseWriter) WriteHeader(status int) {
489+
w.wroteHeader = true
490+
w.ResponseRecorder.WriteHeader(status)
491+
}
492+
493+
func (w *trackingResponseWriter) Write(body []byte) (int, error) {
494+
if w.writeErr != nil {
495+
return 0, w.writeErr
496+
}
497+
return w.ResponseRecorder.Write(body)
498+
}
499+
500+
func TestServeHTTPClientCancellation(t *testing.T) {
501+
for _, invokeErr := range []error{context.Canceled, errors.New("invoke failed after cancellation")} {
502+
t.Run(invokeErr.Error(), func(t *testing.T) {
503+
ctx, cancel := context.WithCancel(context.Background())
504+
defer cancel()
505+
started := make(chan struct{})
506+
go func() {
507+
<-started
508+
cancel()
509+
}()
510+
fake := &fakeLambdaInvoker{invoke: func(ctx context.Context, _ *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
511+
close(started)
512+
<-ctx.Done()
513+
return nil, invokeErr
514+
}}
515+
core, logs := observer.New(zap.DebugLevel)
516+
m := &Lambda{FunctionName: "test-function", Timeout: caddy.Duration(time.Second), log: zap.New(core), svc: fake}
517+
w := &trackingResponseWriter{ResponseRecorder: httptest.NewRecorder()}
518+
r := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
519+
r.Header.Set("X-Request-ID", "request-123")
520+
521+
if err := m.ServeHTTP(w, r, nil); err != nil {
522+
t.Fatalf("ServeHTTP() error = %v, want nil", err)
523+
}
524+
if w.wroteHeader || w.Body.Len() != 0 {
525+
t.Fatalf("response written: headers = %v, body = %q", w.wroteHeader, w.Body.String())
526+
}
527+
entries := logs.All()
528+
if len(entries) != 1 || entries[0].Level != zap.DebugLevel || entries[0].Message != "Lambda invocation canceled by client" {
529+
t.Fatalf("logs = %#v, want one client cancellation debug entry", entries)
530+
}
531+
fields := entries[0].ContextMap()
532+
if fields["function"] != "test-function" || fields["request_id"] != "request-123" || fields["duration"] == nil || fields["error"] == nil {
533+
t.Fatalf("log fields = %#v, want function, request_id, duration, and error", fields)
534+
}
535+
})
536+
}
537+
}
538+
539+
func TestServeHTTPInvocationErrors(t *testing.T) {
540+
for _, test := range []struct {
541+
name string
542+
err error
543+
status int
544+
level zapcore.Level
545+
}{
546+
{"timeout", context.DeadlineExceeded, http.StatusGatewayTimeout, zap.ErrorLevel},
547+
{"generic", errors.New("invoke failed"), http.StatusBadGateway, zap.ErrorLevel},
548+
{"throttled", &smithy.GenericAPIError{Code: "TooManyRequestsException"}, http.StatusServiceUnavailable, zap.WarnLevel},
549+
} {
550+
t.Run(test.name, func(t *testing.T) {
551+
fake := &fakeLambdaInvoker{invoke: func(ctx context.Context, _ *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
552+
if test.err == context.DeadlineExceeded {
553+
<-ctx.Done()
554+
return nil, ctx.Err()
555+
}
556+
return nil, test.err
557+
}}
558+
core, logs := observer.New(zap.DebugLevel)
559+
m := &Lambda{FunctionName: "test-function", Timeout: caddy.Duration(time.Millisecond), log: zap.New(core), svc: fake}
560+
w := &trackingResponseWriter{ResponseRecorder: httptest.NewRecorder()}
561+
err := m.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil), nil)
562+
var handlerErr caddyhttp.HandlerError
563+
if !errors.As(err, &handlerErr) || handlerErr.StatusCode != test.status || !errors.Is(err, test.err) {
564+
t.Fatalf("ServeHTTP() error = %v, want HandlerError %d wrapping %v", err, test.status, test.err)
565+
}
566+
if w.wroteHeader || w.Body.Len() != 0 {
567+
t.Fatal("ServeHTTP() wrote a response before returning the error")
568+
}
569+
entries := logs.All()
570+
if len(entries) != 1 || entries[0].Level != test.level || entries[0].Message != "Lambda invocation failed" {
571+
t.Fatalf("logs = %#v, want one invocation failure at %s", entries, test.level)
572+
}
573+
})
574+
}
575+
}
576+
577+
func TestServeHTTPBodyWriteFailureAborts(t *testing.T) {
578+
writeErr := errors.New("connection closed")
579+
w := &trackingResponseWriter{ResponseRecorder: httptest.NewRecorder(), writeErr: writeErr}
580+
fake := &fakeLambdaInvoker{output: &lambda.InvokeOutput{Payload: []byte(`{"statusCode":200,"body":"ok"}`)}}
581+
core, logs := observer.New(zap.WarnLevel)
582+
m := &Lambda{FunctionName: "test-function", Timeout: caddy.Duration(time.Second), log: zap.New(core), svc: fake}
583+
584+
defer func() {
585+
if got := recover(); got != http.ErrAbortHandler {
586+
t.Fatalf("panic = %v, want http.ErrAbortHandler", got)
587+
}
588+
if !w.wroteHeader || w.Code != http.StatusOK {
589+
t.Fatal("expected response headers before body write failure")
590+
}
591+
entries := logs.All()
592+
if len(entries) != 1 || entries[0].Level != zap.WarnLevel || entries[0].Message != "aborting with incomplete response" || entries[0].ContextMap()["error"] != writeErr.Error() {
593+
t.Fatalf("logs = %#v, want one incomplete response warning with write error", entries)
594+
}
595+
}()
596+
if err := m.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil), nil); err != nil {
597+
t.Fatalf("ServeHTTP() returned error instead of panicking: %v", err)
598+
}
599+
}
600+
480601
func TestInvokeLambdaReturnsThrottlingError(t *testing.T) {
481602
wantErr := errors.New("throttled")
482603
fake := &fakeLambdaInvoker{err: wantErr}

0 commit comments

Comments
 (0)