-
Notifications
You must be signed in to change notification settings - Fork 348
chore(api): RequestTimeout + distinguish between client cancel and request timeout in telemetry #2165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
chore(api): RequestTimeout + distinguish between client cancel and request timeout in telemetry #2165
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
49d11bc
chore(api): distinguish between client cancel and request timeout
jakubno fdcc1d5
fix: set correct status
jakubno f0fea80
fix: correctly cancel timeout
jakubno 7c94809
fix: lint
jakubno d9cf409
chore: simplify
jakubno 2ab9d1e
chore: clean up
jakubno dd4e0b5
fix: setting the error
jakubno d5a9c1c
chore: don't reset status code if not client cancel
jakubno 00218f8
chore: simplify the logic in timeout middleware
jakubno 2035871
chore: remove unnecessary exlude routes
jakubno 36c1e81
chore: remove dead code
jakubno 44afa40
fix: remove dead code
jakubno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // ErrRequestTimeout is the cancel cause set when the per-request timeout fires. | ||
| // Callers can distinguish this from a client disconnection by checking: | ||
| // | ||
| // errors.Is(CancelCause(c), middleware.ErrRequestTimeout) | ||
| var ErrRequestTimeout = errors.New("request timeout exceeded") | ||
|
|
||
| // cancelCauseKey is the gin context key where RequestTimeout snapshots the | ||
| // cancel cause before defer-cancel runs. | ||
| const cancelCauseKey = "middleware.cancelCause" | ||
|
|
||
| // CancelCause returns the cancel cause captured by the timeout middleware. | ||
| // It returns nil for normal (non-canceled/non-timed-out) requests. | ||
| func CancelCause(c *gin.Context) error { | ||
| if val, exists := c.Get(cancelCauseKey); exists { | ||
| if err, ok := val.(error); ok { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // StatusClientClosedRequest is the de-facto status code (used by nginx) for a | ||
| // client that closed the connection before the server could send a response. | ||
| // It is not an official IANA code but is widely recognised in logs and metrics. | ||
| const StatusClientClosedRequest = 499 | ||
|
|
||
| // RequestTimeout returns a Gin middleware that sets a context deadline on each | ||
| // request. This is needed because http.Server.WriteTimeout does NOT cancel | ||
| // r.Context() (see https://github.com/golang/go/issues/59602), so without an | ||
| // explicit deadline, blocking calls like pgxpool.Acquire will wait indefinitely | ||
| // when the connection pool is saturated. | ||
| // | ||
| // After the handler returns, the middleware checks context.Cause to distinguish | ||
| // two cancellation scenarios and sets an appropriate status if nothing was | ||
| // written yet: | ||
| // - server-side timeout (cause is ErrRequestTimeout) → 408 Request Timeout | ||
| // - client disconnect (ctx.Err() == context.Canceled) → 499 Client Closed Request | ||
| // | ||
| // Routes matching any of the excludedRoutes patterns are skipped (useful for | ||
| // health checks and long-polling endpoints). | ||
| func RequestTimeout(timeout time.Duration) gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| ctx, cancel := context.WithTimeoutCause(c.Request.Context(), timeout, ErrRequestTimeout) | ||
| defer cancel() | ||
|
|
||
| c.Request = c.Request.WithContext(ctx) | ||
| c.Next() | ||
|
jakubno marked this conversation as resolved.
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| // Snapshot the cause *before* defer-cancel fires so outer | ||
| // middlewares can distinguish timeout vs client-disconnect | ||
| // via CancelCause(c) without racing with the deferred cancel. | ||
| if err := context.Cause(ctx); err != nil { | ||
| c.Set(cancelCauseKey, err) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.