-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Echo Timeout middleware returning multiple responses for a single request on another handler #2754
Comments
Using another approach based on the comment here, I’m still encountering the same issue. another approach: package main
import (
"context"
"net/http"
"time"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
handlerTimeout := 500 * time.Millisecond
g := e.Group("/built-in-mdw")
g.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
timeoutCtx, cancel := context.WithTimeout(c.Request().Context(), handlerTimeout)
c.SetRequest(c.Request().WithContext(timeoutCtx))
defer cancel()
return next(c)
}
})
g.GET("", func(c echo.Context) error {
doneCh := make(chan error)
go func(ctx context.Context) {
// long running process here
time.Sleep(time.Second)
doneCh <- c.String(http.StatusOK, "built-in-mdw: param counter = "+c.QueryParam("counter"))
}(c.Request().Context())
select {
case err := <-doneCh:
if err != nil {
return err
}
return c.String(http.StatusOK, "OK")
case <-c.Request().Context().Done():
if c.Request().Context().Err() == context.DeadlineExceeded {
return c.String(http.StatusServiceUnavailable, "timeout: built-in-mdw: param counter = "+c.QueryParam("counter"))
}
return c.Request().Context().Err()
}
})
e.GET("/health", func(c echo.Context) error {
time.Sleep(100 * time.Millisecond)
return c.String(http.StatusOK, "healthz: OK")
})
e.Logger.Fatal(e.Start(":1323"))
} |
Please do not use timeout middleware. and about |
if you are selecting from 2 channels |
We’ve encountered an issue where multiple unexpected responses are returned when using the Timeout middleware on a single handler.
Example code:
How to test:
We occasionally receive unexpected responses in the second terminal. For example:
We’re seeing multiple responses for what should be a single request. I’m using Echo’s Timeout feature as provided in the documentation. Can anyone help me investigate this issue?
The text was updated successfully, but these errors were encountered: