Merge pull request #31016 from krousey/tcp_reuse
Automatic merge from submit-queue Attempt to ensure entire resp body is read **What this PR does / why we need it**: Enables the re-use of TCP connections when code fails to read the entire body of the response. **Which issue this PR fixes**: fixes #30975 **Special notes for your reviewer**: This is a best effort approach. It only attempts to drain the body of the response if it's less than 1k. It seems like a reasonable barrier at which to give up and just use a new TCP connection. cc: @wojtek-t @smarterclayton @lavalamp @kubernetes/sig-api-machinery
This commit is contained in:
@@ -821,9 +821,16 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
done := func() bool {
|
done := func() bool {
|
||||||
// ensure the response body is closed before we reconnect, so that we reuse the same
|
// Ensure the response body is fully read and closed
|
||||||
// TCP connection
|
// before we reconnect, so that we reuse the same TCP
|
||||||
defer resp.Body.Close()
|
// connection.
|
||||||
|
defer func() {
|
||||||
|
const maxBodySlurpSize = 2 << 10
|
||||||
|
if resp.ContentLength <= maxBodySlurpSize {
|
||||||
|
io.Copy(ioutil.Discard, &io.LimitedReader{R: resp.Body, N: maxBodySlurpSize})
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
retries++
|
retries++
|
||||||
if seconds, wait := checkWait(resp); wait && retries < maxRetries {
|
if seconds, wait := checkWait(resp); wait && retries < maxRetries {
|
||||||
|
|||||||
Reference in New Issue
Block a user