ContextTimeout Middleware is undocumented
I cannot find any mention of the ContextTimeout middleware in the documentation.
At the same time, a number of issues as well as the Timeout middleware source code warns against it, but no such warning is present in the documentation.
- Any reason the documentation only lists the recommended-against Timeout middleware?
- Would it be possible to document the recommended ContextTimeout middleware, perhaps with a usage example showing what the handler should look like to leverage the ContextTimeout?
ContextTimeout middleware replaces Request context with context that has timeout configured to it. This is useful for situations when you would want long running processes not to last longer than configured timeout. Maybe you have some kind of SQL reporting query that could take really long time under when SQL server is overloaded. by providing context that limits is duration you can assure that your clients will not wait longer than that time. as that SQL method call will end with context.DeadlineExceeded error.
https://github.com/labstack/echo/blob/ce0b12ae531b8c6523797af2425c9c0b6d772c4e/middleware/context_timeout.go#L59-L72
this is conceptually different from Timeout middleware as Timeout middleware does not end your SQL call. Timeout MW will just send response to client that request timeoutted but that request will actually will be still running in separate goroutine on your server and will end when handler finishes. Most of the time you do not need this kind of behaviour - maybe only in situations when you need to commit transactions regardless if clients sees that or not.
Thank you for the explanation. I understand the difference and agree with the recommendation. My question was: why is ContextTimeout (and the difference and use cases of the two middleware) not documented?
there are no specific reason why ContextTimeout is not documented. I has been forgotten. I am thinking why have we not removed https://echo.labstack.com/docs/middleware/timeout as we actually do not want to advertise that middleware.