Skip to content

Commit

Permalink
Allow TimeoutHandler connections to be kept alive (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer authored Aug 16, 2020
1 parent a995d43 commit 01acd76
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
5 changes: 1 addition & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2170,12 +2170,9 @@ func (s *Server) serveConn(c net.Conn) (err error) {

timeoutResponse = ctx.timeoutResponse
if timeoutResponse != nil {
// Acquire a new ctx because the old one will still be in use by the timeout out handler.
ctx = s.acquireCtx(c)
timeoutResponse.CopyTo(&ctx.Response)
if br != nil {
// Close connection, since br may be attached to the old ctx via ctx.fbr.
ctx.SetConnectionClose()
}
}

if !ctx.IsGet() && ctx.IsHead() {
Expand Down
42 changes: 42 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,45 @@ func TestTimeoutHandlerTimeout(t *testing.T) {
}
}

func TestTimeoutHandlerTimeoutReuse(t *testing.T) {
t.Parallel()

ln := fasthttputil.NewInmemoryListener()
h := func(ctx *RequestCtx) {
if string(ctx.Path()) == "/timeout" {
time.Sleep(time.Second)
}
ctx.SetBodyString("ok")
}
s := &Server{
Handler: TimeoutHandler(h, 20*time.Millisecond, "timeout!!!"),
}
go func() {
if err := s.Serve(ln); err != nil {
t.Errorf("unexepcted error: %s", err)
}
}()

conn, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
br := bufio.NewReader(conn)
if _, err = conn.Write([]byte("GET /timeout HTTP/1.1\r\nHost: google.com\r\n\r\n")); err != nil {
t.Fatalf("unexpected error: %s", err)
}
verifyResponse(t, br, StatusRequestTimeout, string(defaultContentType), "timeout!!!")

if _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")); err != nil {
t.Fatalf("unexpected error: %s", err)
}
verifyResponse(t, br, StatusOK, string(defaultContentType), "ok")

if err := ln.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}

func TestServerGetOnly(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2556,6 +2595,7 @@ func TestServerTimeoutErrorWithResponse(t *testing.T) {

br := bufio.NewReader(&rw.w)
verifyResponse(t, br, 456, "foo/bar", "path=/foo")
verifyResponse(t, br, 456, "foo/bar", "path=/bar")

data, err := ioutil.ReadAll(br)
if err != nil {
Expand Down Expand Up @@ -2599,6 +2639,7 @@ func TestServerTimeoutErrorWithCode(t *testing.T) {

br := bufio.NewReader(&rw.w)
verifyResponse(t, br, StatusBadRequest, string(defaultContentType), "stolen ctx")
verifyResponse(t, br, StatusBadRequest, string(defaultContentType), "stolen ctx")

data, err := ioutil.ReadAll(br)
if err != nil {
Expand Down Expand Up @@ -2642,6 +2683,7 @@ func TestServerTimeoutError(t *testing.T) {

br := bufio.NewReader(&rw.w)
verifyResponse(t, br, StatusRequestTimeout, string(defaultContentType), "stolen ctx")
verifyResponse(t, br, StatusRequestTimeout, string(defaultContentType), "stolen ctx")

data, err := ioutil.ReadAll(br)
if err != nil {
Expand Down

0 comments on commit 01acd76

Please sign in to comment.