Summary
There appears to be a regression in github.com/gin-contrib/timeout starting from v1.2.1.
The timeout middleware does not terminate the request at the configured timeout duration and instead waits until the handler completes.
Environment
- github.com/gin-contrib/timeout: v1.2.1
- github.com/gin-gonic/gin: v1.12.0
Reproduction
- Use the following test code
package main
import (
"log"
"net/http"
"time"
"github.com/gin-contrib/timeout"
"github.com/gin-gonic/gin"
)
func testResponse(c *gin.Context) {
c.String(http.StatusRequestTimeout, "timeout")
}
func timeoutMiddleware() gin.HandlerFunc {
return timeout.New(
timeout.WithTimeout(time.Second),
timeout.WithResponse(testResponse),
)
}
func main() {
r := gin.New()
r.Use(timeoutMiddleware())
r.GET("/slow", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.Status(http.StatusOK)
})
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
- Run the server
- Call the endpoint
$ curl http://localhost:8080/slow -w "%{time_total} sec\n"
Results
Using gin-contrib/timeout v1.2.1:
Using gin-contrib/timeout v1.2.0:
Expected Behavior
When a timeout of 1 second is configured, the request should be terminated after ~1 second, and the timeout response should be returned.
Actual Behavior
- With v1.2.1, the request completes after ~5 seconds (the handler duration), not respecting the timeout.
- With v1.2.0, the timeout works correctly and returns after ~1 second.
Summary
There appears to be a regression in github.com/gin-contrib/timeout starting from v1.2.1.
The timeout middleware does not terminate the request at the configured timeout duration and instead waits until the handler completes.
Environment
Reproduction
$ curl http://localhost:8080/slow -w "%{time_total} sec\n"Results
Using gin-contrib/timeout v1.2.1:
Using gin-contrib/timeout v1.2.0:
Expected Behavior
When a timeout of 1 second is configured, the request should be terminated after ~1 second, and the timeout response should be returned.
Actual Behavior