Skip to content

Commit

Permalink
Allow connection close for custom streams (#1603)
Browse files Browse the repository at this point in the history
* feat: allow connection close for custom streams

* fix: avoid req access since might already be released

* fix: fix aloc test fails

* fix: race condition when clossing body stream
  • Loading branch information
becheran authored Sep 2, 2023
1 parent 8236f8d commit 34e7da1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2894,8 +2894,8 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret

br := hc.acquireReader(conn)
err = resp.ReadLimitBody(br, hc.MaxResponseBodySize)
hc.releaseReader(br)
if err != nil {
hc.releaseReader(br)
hc.closeConn(cc)
// Don't retry in case of ErrBodyTooLarge since we will just get the same again.
needRetry := err != ErrBodyTooLarge
Expand All @@ -2906,17 +2906,20 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
if customStreamBody && resp.bodyStream != nil {
rbs := resp.bodyStream
resp.bodyStream = newCloseReader(rbs, func() error {
hc.releaseReader(br)
if r, ok := rbs.(*requestStream); ok {
releaseRequestStream(r)
}
if closeConn {
if closeConn || resp.ConnectionClose() {
hc.closeConn(cc)
} else {
hc.releaseConn(cc)
}
return nil
})
return false, nil
} else {
hc.releaseReader(br)
}

if closeConn {
Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,8 @@ func (resp *Response) Reset() {
if responseBodyPoolSizeLimit >= 0 && resp.body != nil {
resp.ReleaseBody(responseBodyPoolSizeLimit)
}
resp.Header.Reset()
resp.resetSkipHeader()
resp.Header.Reset()
resp.SkipBody = false
resp.raddr = nil
resp.laddr = nil
Expand Down

0 comments on commit 34e7da1

Please sign in to comment.