gin-contrib / timeout

Timeout middleware for Gin
MIT License
185 stars 38 forks source link

Incorrect timeout #14

Closed mahongran closed 2 years ago

mahongran commented 2 years ago

image

The timeout time is 4S. After the first request timeout, the SQL does not finish executing. The timeout of the second request will be greater than 4S

image image

appleboy commented 2 years ago

@mahongran I can't reproduce the issue.

Server 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(6*time.Second),
        timeout.WithHandler(func(c *gin.Context) {
            c.Next()
        }),
        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)
    }
}

Client

$ time curl -v http://localhost:8080/slow
*   Trying ::1:8080...
* Connected to localhost (::1) port 8080 (#0)
> GET /slow HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Thu, 23 Jun 2022 09:12:59 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

real    0m5.041s
user    0m0.005s
sys 0m0.010s