Skip to content

Commit

Permalink
reverted PR #54 :timeout handling in timeout.go and timeout_test.go (#62
Browse files Browse the repository at this point in the history
)

- Remove the import statement for "context" in timeout.go
- Remove the code block related to context cancellation in the New function in timeout.go
- Replace the case statement in the New function in timeout.go with a case statement using time.After
- Remove the import statement for "sync" in timeout_test.go
- Remove the TestDeadlineExceeded function in timeout_test.go

reverted PR #54 

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Nov 25, 2023
1 parent ae97451 commit f2805fd
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 40 deletions.
7 changes: 1 addition & 6 deletions timeout.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package timeout

import (
"context"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -47,10 +46,6 @@ func New(opts ...Option) gin.HandlerFunc {
c.Writer = tw
buffer.Reset()

ctx, cancel := context.WithTimeout(c, t.timeout)
c.Request = c.Request.WithContext(ctx)
defer cancel()

go func() {
defer func() {
if p := recover(); p != nil {
Expand Down Expand Up @@ -82,7 +77,7 @@ func New(opts ...Option) gin.HandlerFunc {
tw.FreeBuffer()
bufPool.Put(buffer)

case <-c.Request.Context().Done():
case <-time.After(t.timeout):
c.Abort()
tw.mu.Lock()
defer tw.mu.Unlock()
Expand Down
34 changes: 0 additions & 34 deletions timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -101,36 +100,3 @@ func TestPanic(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "", w.Body.String())
}

func TestDeadlineExceeded(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)

r := gin.New()
r.GET("/", New(
WithTimeout(50*time.Millisecond),
WithHandler(func(c *gin.Context) {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
assert.Equal(t, "value", c.Request.Header.Get("X-Test"))
assert.Equal(t, context.DeadlineExceeded, c.Request.Context().Err())
select {
case <-c.Request.Context().Done():
// OK
case <-time.After(1 * time.Second):
assert.Fail(t, "context is not done")
}
}),
WithResponse(testResponse),
))

w := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
req.Header.Set("X-Test", "value")
r.ServeHTTP(w, req)

assert.Equal(t, http.StatusRequestTimeout, w.Code)
assert.Equal(t, "test response", w.Body.String())

wg.Wait()
}

0 comments on commit f2805fd

Please sign in to comment.