Re: [go-nuts] early close of http.Client's Response.Body

2019-11-25 Thread Andy Balholm
Read-to-EOF is one of several conditions for connection reuse:

- Was the response body fully read (to EOF)?
- Does the server support HTTP keep-alives?
- Is DisableKeepAlives set to false (the default) on the http.Transport that 
the client uses?

A connection will only be reused if all of these are met. Otherwise Close will 
close the connection.

There is no need to discard a Client after an early Close. A Client is not tied 
to a single connection.

Andy

> On Nov 25, 2019, at 11:10 AM, Liam Breck  wrote:
> 
> 
> 
> On Mon, Nov 25, 2019, 10:32 AM Andy Balholm  > wrote:
> 
> 
> > On Nov 25, 2019, at 9:54 AM, Liam  > > wrote:
> > 
> > - does the read-to-EOF stipulation also apply to Client.Get/Post() ?
> 
> Yes. Those methods are fairly simple wrappers around Do.
> 
> > - why does Response.Body.Close() before io.EOF not release unread buffers 
> > or otherwise prepare it for persistence?
> 
> The connection can’t be reused for another request before all the data from 
> the first request is read. The server is sending those bytes over the wire, 
> and they need to go somewhere before another response can be read. So there 
> were two options for how to implement Close when the whole body hasn’t been 
> read:
> 
> 1. Copy the rest of the body to /dev/null (or equivalent), and reuse the 
> connection like normal.
> 2. Close the connection, thus communicating to the server that we don’t need 
> that data after all.
> 
> Option 1 would generally be preferable for short responses, and option 2 for 
> long ones. For consistency, and because we don’t always know the response 
> length in advance (e.g. if it is chunked), it always does option 2.
> 
> So after read-to-EOF, Close() behaves differently, i.e. it does reset for 
> next request?
> 
> In the event of early Close(), should I discard that Client instance? Or will 
> it work for subsequent requests?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5DB3F0EE-D976-412C-860F-C4468AF99B1F%40gmail.com.


Re: [go-nuts] early close of http.Client's Response.Body

2019-11-25 Thread Liam Breck
On Mon, Nov 25, 2019, 10:32 AM Andy Balholm  wrote:

>
>
> > On Nov 25, 2019, at 9:54 AM, Liam  wrote:
> >
> > - does the read-to-EOF stipulation also apply to Client.Get/Post() ?
>
> Yes. Those methods are fairly simple wrappers around Do.
>
> > - why does Response.Body.Close() before io.EOF not release unread
> buffers or otherwise prepare it for persistence?
>
> The connection can’t be reused for another request before all the data
> from the first request is read. The server is sending those bytes over the
> wire, and they need to go somewhere before another response can be read. So
> there were two options for how to implement Close when the whole body
> hasn’t been read:
>
> 1. Copy the rest of the body to /dev/null (or equivalent), and reuse the
> connection like normal.
> 2. Close the connection, thus communicating to the server that we don’t
> need that data after all.
>
> Option 1 would generally be preferable for short responses, and option 2
> for long ones. For consistency, and because we don’t always know the
> response length in advance (e.g. if it is chunked), it always does option 2.
>

So after read-to-EOF, Close() behaves differently, i.e. it does reset for
next request?

In the event of early Close(), should I discard that Client instance? Or
will it work for subsequent requests?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAKvHMgRH6K5WBdAoMVnq6KsHi7Hnqqhm39ZcUHX%3DWBCbfKsiwg%40mail.gmail.com.


Re: [go-nuts] early close of http.Client's Response.Body

2019-11-25 Thread Andy Balholm



> On Nov 25, 2019, at 9:54 AM, Liam  wrote:
> 
> - does the read-to-EOF stipulation also apply to Client.Get/Post() ?

Yes. Those methods are fairly simple wrappers around Do.

> - why does Response.Body.Close() before io.EOF not release unread buffers or 
> otherwise prepare it for persistence?

The connection can’t be reused for another request before all the data from the 
first request is read. The server is sending those bytes over the wire, and 
they need to go somewhere before another response can be read. So there were 
two options for how to implement Close when the whole body hasn’t been read:

1. Copy the rest of the body to /dev/null (or equivalent), and reuse the 
connection like normal.
2. Close the connection, thus communicating to the server that we don’t need 
that data after all.

Option 1 would generally be preferable for short responses, and option 2 for 
long ones. For consistency, and because we don’t always know the response 
length in advance (e.g. if it is chunked), it always does option 2.

Andy

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/228DAB0B-E2AB-4A5E-BBC5-76D611ED11F9%40gmail.com.


[go-nuts] early close of http.Client's Response.Body

2019-11-25 Thread Liam
Scenario:
Sending a client request and only reading the whole Response.Body for 
certain .StatusCode values, and/or reading only the first N bytes.

Docs for Client.Do()
"If the Body is not both read to EOF and closed, the Client's underlying 
RoundTripper (typically Transport) may not be able to re-use a persistent 
TCP connection..."

Docs for Client.Get/Post()
"Caller should close resp.Body when done reading from it."

Questions:
- does the read-to-EOF stipulation also apply to Client.Get/Post() ?
- why does Response.Body.Close() before io.EOF not release unread buffers 
or otherwise prepare it for persistence?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/96d2e1d8-22e9-4cc0-99b2-b33721790773%40googlegroups.com.