-
Notifications
You must be signed in to change notification settings - Fork 4
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
server: cancel the request context on timeout #821
Conversation
c03e842
to
f22281d
Compare
msg := err.Error() | ||
errStruct := apiTypes.HumanReadableError{Msg: msg} | ||
// If request context is closed, don't bother writing a response. | ||
if r.Context().Err() != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this mean we leave the request hanging if our handling times out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the context is canceled the client already received an EOF (due to the WriteTimeout being reached), so doing anything here will have no effect.
But maybe instead of using our own 'ContextTimeoutHandler' which only cancels the context, we should use upstream TimeoutHandler. That one also handles context cancellation, but it also returns a 503, instead of just closing the connection as it is done now. Thats probably nicer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we use http.TimeoutHandler
, the client receives a 503 and the connection is closed when the timeout is reached. So like before, if the context is canceled there is nothing to do. Even if we didn't return here, the Writes below would fail with ErrHandlerTimeout
.
cmd/api/api.go
Outdated
ReadTimeout: 10 * time.Second, | ||
WriteTimeout: 10 * time.Second, | ||
ReadTimeout: requestsTimeout, | ||
WriteTimeout: requestsTimeout, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReadTimeout and WriteTimeout are for request I/O and not handling right? these don't have to be the same as the requestsTimeout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, WriteTimeout is not just the IO for writing the response, it includes the server processing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, should that be some amount higher than the processing time limit then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, i think we should; and then by also using the upstream Timeout handler (i mentioned in the other comment) the client will get a 503 in case the processing timeout is reached.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
97c031b
to
f9c3bda
Compare
510a7b0
to
16cc9b5
Compare
16cc9b5
to
064658f
Compare
I'm merging since we want to do a new release today, can apply potential requestsed changes in a future PR. |
Added
ContextTimeoutMiddleware
to enforce request context timeout and ensure proper cancellation of timed-out requests. This ensures that the context for requests exceeding the defined timeout limit are properly canceled.I noticed cases like the one below, where request takes 15 seconds:
Note that the server logs show a 200 OK status, but the response is not delivered to the client. Instead, the client receives an EOF since the configured server timeout is reached, while the server continues processing the request.
Ref upstream issue: golang/go#59602