-
Notifications
You must be signed in to change notification settings - Fork 501
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
services/horizon/internal: Use custom timeout middleware #1870
services/horizon/internal: Use custom timeout middleware #1870
Conversation
5fb2238
to
d032dc6
Compare
d032dc6
to
5e8fa3b
Compare
5e8fa3b
to
0c966d9
Compare
cancel() | ||
if ctx.Err() == context.DeadlineExceeded { | ||
if mw, ok := w.(middleware.WrapResponseWriter); !ok { | ||
log.Warn("expected ResponseWriter instance to be WrapResponseWriter") |
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.
I think this relies on WrapResponseWriter
created in loggerMiddleware
. If we ever remove this or change the order of middlewares code here will stop working. Can't we just create a new WrapResponseWriter
here? It will also remove a need for type checking.
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.
@bartekn fixed
To prevent WriteHeader from being called multiple times we have created a timeout middleware which checks that the response status hasn't already been written before setting the status to http.StatusGatewayTimeout
0c966d9
to
c1f0ce9
Compare
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.
LGTM! Last question: you check if w
is middleware.WrapResponseWriter
but we don't do this in loggerMiddleware
. I'm wondering if two WrapResponseWriter
s can work together, if not we should add a check to loggerMiddleware
too.
ctx, cancel := context.WithTimeout(r.Context(), timeout) | ||
defer func() { | ||
cancel() | ||
if ctx.Err() == context.DeadlineExceeded { |
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.
Do we do any other handling up the hierarchy chain if ct.Err()
is different to context.DeadlineExceeded
?
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.
I don't think so
PR Checklist
PR Structure
otherwise).
services/friendbot
, orall
ordoc
if the changes are broad or impact manypackages.
Thoroughness
.md
files, etc... affected by this change). Take a look in the
docs
folder for a given service,like this one.
Release planning
needed with deprecations, added features, breaking changes, and DB schema changes.
semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
Summary
Close #1684
Instances of "multiple response.WriteHeader calls" appear in horizon logs. The cause of these warnings is that there is a timeout middleware which terminates a request and responds with
http.StatusGatewayTimeout
if the request exceeds some amount of time.This PR fixes this issue by using a custom timeout middleware after the logging middleware. The logging middleware wraps the
http.ResponseWriter
in a chiWrapResponseWriter
instance. The custom timeout middleware then checks that the response status has not been set before callingWriteHeader(http.StatusGatewayTimeout)