gin-contrib / timeout

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

fix(context): (context.Context).Err() returns DeadlineExceeded #54

Closed demouth closed 1 year ago

demouth commented 1 year ago

Overview

Currently, when a timeout occurs, the ctx.Request.Context().Err() method returns a context.Canceled. However, in the case of a timeout, we expect the context.DeadlineExceeded to be returned.

This pull request addresses the timeout error handling. By modifying to return context.DeadlineExceeded when a timeout occurs, we enhance the consistency of error handling.

r := gin.New()
r.GET("/", timeout.New(
    timeout.WithTimeout(50*time.Millisecond),
    timeout.WithHandler(func(c *gin.Context) {
        time.Sleep(100 * time.Millisecond)

        // expected: context.DeadlineExceeded
        // actual: context.Canceled
        fmt.Printf("%v\n", c.Request.Context().Err())
    }),
))
r.Run("0.0.0.0:8083")

Changes Made

Reviewed and revised timeout error handling to return the context.DeadlineExceeded instead of the context.Canceled using ctx.Request.Context().Err().

Testing

Added test cases that trigger timeouts with the applied changes. This ensures that the modifications function correctly and the expected error is returned.

appleboy commented 12 months ago

@demouth I reverted the PR. Can't pass the testing.