You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the timeout middleware waits for a certain duration.
If the handler returns before that time, it works as per normal.
If the handler takes longer, it waits for handler to finish.
Then it returns a timeout error.
It can be redesigned so that if the timeout completes first, the timeout error is immediately returned EVEN IF the handler continues until completion. At least the client gets a response.
How to Reproduce
As it operates normally
Expected Behavior
After timeout, return immediately (even if request handler continues)
Fiber Version
v3
Code Snippet (optional)
// New enforces a timeout for each incoming request. If the timeout expires or// any of the specified errors occur, fiber.ErrRequestTimeout is returned.funcNew(h fiber.Handler, timeout time.Duration, tErrs...error) fiber.Handler {
returnfunc(ctx fiber.Ctx) error {
// If timeout <= 0, skip context.WithTimeout and run the handler as-is.iftimeout<=0 {
returnrunHandler(ctx, h, tErrs)
}
// Create a context with the specified timeout; any operation exceeding// this deadline will be canceled automatically.timeoutContext, cancel:=context.WithTimeout(ctx, timeout)
defercancel()
// Run the handler and check for relevant errors.err:=runHandler(ctx, h, tErrs)
// If the context actually timed out, return a timeout error.iferrors.Is(timeoutContext.Err(), context.DeadlineExceeded) {
returnfiber.ErrRequestTimeout
}
returnerr
}
}
// runHandler executes the handler and returns fiber.ErrRequestTimeout if it// sees a deadline exceeded error or one of the custom "timeout-like" errors.funcrunHandler(c fiber.Ctx, h fiber.Handler, tErrs []error) error {
// Execute the wrapped handler synchronously.err:=h(c)
// If the context has timed out, return a request timeout error.iferr!=nil&& (errors.Is(err, context.DeadlineExceeded) ||isCustomError(err, tErrs)) {
returnfiber.ErrRequestTimeout
}
returnerr
}
// isCustomError checks whether err matches any error in errList using errors.Is.funcisCustomError(errerror, errList []error) bool {
for_, e:=rangeerrList {
iferrors.Is(err, e) {
returntrue
}
}
returnfalse
}
I understand that it's implemented this way because currently Fiber internals prevent a request from responding until the handler no longer is using the fiber.Context.
Bug Description
Currently, the timeout middleware waits for a certain duration.
If the handler returns before that time, it works as per normal.
If the handler takes longer, it waits for handler to finish.
Then it returns a timeout error.
It can be redesigned so that if the timeout completes first, the timeout error is immediately returned EVEN IF the handler continues until completion. At least the client gets a response.
How to Reproduce
As it operates normally
Expected Behavior
After timeout, return immediately (even if request handler continues)
Fiber Version
v3
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: