Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for immediate header flush #575

Merged
merged 2 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type Response struct {
// Copying Header by value is forbidden. Use pointer to Header instead.
Header ResponseHeader

// Flush headers as soon as possible without waiting for first body bytes.
// Relevant for bodyStream only.
ImmediateHeaderFlush bool

bodyStream io.Reader
w responseBodyWriter
body *bytebufferpool.ByteBuffer
Expand Down Expand Up @@ -870,6 +874,7 @@ func (resp *Response) Reset() {
resp.SkipBody = false
resp.raddr = nil
resp.laddr = nil
resp.ImmediateHeaderFlush = false
}

func (resp *Response) resetSkipHeader() {
Expand Down Expand Up @@ -1455,12 +1460,22 @@ func (resp *Response) writeBodyStream(w *bufio.Writer, sendBody bool) error {
}
if contentLength >= 0 {
if err = resp.Header.Write(w); err == nil && sendBody {
err = writeBodyFixedSize(w, resp.bodyStream, int64(contentLength))
if resp.ImmediateHeaderFlush {
err = w.Flush()
}
if err == nil {
err = writeBodyFixedSize(w, resp.bodyStream, int64(contentLength))
}
}
} else {
resp.Header.SetContentLength(-1)
if err = resp.Header.Write(w); err == nil && sendBody {
err = writeBodyChunked(w, resp.bodyStream)
if resp.ImmediateHeaderFlush {
err = w.Flush()
}
if err == nil {
err = writeBodyChunked(w, resp.bodyStream)
}
}
}
err1 := resp.closeBodyStream()
Expand Down
130 changes: 130 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1921,3 +1921,133 @@ func TestResponseRawBodyCopyTo(t *testing.T) {

testResponseCopyTo(t, &resp)
}

type testReader struct {
read chan (int)
cb chan (struct{})
}

func (r *testReader) Read(b []byte) (int, error) {
read := <-r.read

if read == -1 {
return 0, io.EOF
}

r.cb <- struct{}{}

for i := 0; i < read; i++ {
b[i] = 'x'
}

return read, nil
}

func TestResponseImmediateHeaderFlushRegressionFixedLength(t *testing.T) {
var r Response

expectedS := "aaabbbccc"
buf := bytes.NewBufferString(expectedS)
r.SetBodyStream(buf, len(expectedS))
r.ImmediateHeaderFlush = true

testBodyWriteTo(t, &r, expectedS, false)
}

func TestResponseImmediateHeaderFlushRegressionChunked(t *testing.T) {
var r Response

expectedS := "aaabbbccc"
buf := bytes.NewBufferString(expectedS)
r.SetBodyStream(buf, -1)
r.ImmediateHeaderFlush = true

testBodyWriteTo(t, &r, expectedS, false)
}

func TestResponseImmediateHeaderFlushFixedLength(t *testing.T) {
var r Response

r.ImmediateHeaderFlush = true

ch := make(chan int)
cb := make(chan struct{})

buf := &testReader{read: ch, cb: cb}

r.SetBodyStream(buf, 3)

b := []byte{}
w := bytes.NewBuffer(b)
bb := bufio.NewWriter(w)

bw := &r

waitForIt := make(chan struct{})

go func() {
if err := bw.Write(bb); err != nil {
t.Fatalf("unexpected error: %s", err)
}
waitForIt <- struct{}{}
}()

ch <- 3

if !strings.Contains(w.String(), "Content-Length: 3") {
t.Fatalf("Expected headers to be flushed")
}

if strings.Contains(w.String(), "xxx") {
t.Fatalf("Did not expext body to be written yet")
}

<-cb
ch <- -1

<-waitForIt
}

func TestResponseImmediateHeaderFlushChunked(t *testing.T) {
var r Response

r.ImmediateHeaderFlush = true

ch := make(chan int)
cb := make(chan struct{})

buf := &testReader{read: ch, cb: cb}

r.SetBodyStream(buf, -1)

b := []byte{}
w := bytes.NewBuffer(b)
bb := bufio.NewWriter(w)

bw := &r

waitForIt := make(chan struct{})

go func() {
if err := bw.Write(bb); err != nil {
t.Fatalf("unexpected error: %s", err)
}

waitForIt <- struct{}{}
}()

ch <- 3

if !strings.Contains(w.String(), "Transfer-Encoding: chunked") {
t.Fatalf("Expected headers to be flushed")
}

if strings.Contains(w.String(), "xxx") {
t.Fatalf("Did not expext body to be written yet")
}

<-cb
ch <- -1

<-waitForIt
}