net/http.Client not reusing connections

478 views
Skip to first unread message

Dave Cheney

unread,
Jul 4, 2013, 9:23:49 PM7/4/13
to golang-nuts
Hello,

I'm having trouble with controlling the reuse of http connections.
Under Go 1.0.x this code works properly

http://play.golang.org/p/2oiM-3nDYp

Under Go tip (c224c549a3c7+), the code fails to reuse the existing connection.

...
2013/07/04 21:26:00 expected 1 connection to [::]:37350, got 100
exit status 1

I expect it is something simple, but I can't see it. Any suggestions

Cheers

Dave

Jesse McNelis

unread,
Jul 4, 2013, 9:56:54 PM7/4/13
to Dave Cheney, golang-nuts
You're not reading the response body.
I believe Go 1.0 would read the response until complete so as to reuse the connection.
This was a problem for responses that never completed.

I believe Go 1.1 closes the connection if the body isn't read.

Brad Fitzpatrick

unread,
Jul 5, 2013, 10:00:18 PM7/5/13
to Dave Cheney, golang-nuts
Correct. Go 1.1 is more strict about closing the connection if you haven't read the entire response body.




Dave

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Dave Cheney

unread,
Jul 5, 2013, 11:04:44 PM7/5/13
to Brad Fitzpatrick, golang-nuts
Thanks Brad and Jesse,

http://play.golang.org/p/_uHKxseT9G

Now works under Go 1.1 / tip.

Cheers

Dave

Eli Janssen

unread,
Jul 6, 2013, 5:46:42 PM7/6/13
to Dave Cheney, Brad Fitzpatrick, golang-nuts
I do something similar, but use:

ioutil.ReadAll(resp.Body)


Is io.Copy to ioutil.Discard less resource intensive?

Brad Fitzpatrick

unread,
Jul 6, 2013, 6:12:20 PM7/6/13
to Eli Janssen, golang-nuts, Dave Cheney

Yes. It doesn't allocate.

Reply all
Reply to author
Forward
0 new messages