Ian Davis
unread,Sep 22, 2015, 7:13:13 AM9/22/15Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com
Hi all,
Can someone clarify the behaviour of http.Client's Timeout field for me?
When I've used it in my code then I've found that the timeout is
absolute and applies across all requests made by that client. The
comment on the field suggests this is the case:
// Timeout specifies a time limit for requests made by this
// Client. The timeout includes connection time, any
// redirects, and reading the response body. The timer remains
// running after Get, Head, Post, or Do return and will
// interrupt reading of the Response.Body.
That wording also talks about request in the singular, i.e. "the"
response body. However in the comment for the Client struct itself, it's
suggested that clients should be reused for multiple requests:
The Client's Transport typically has internal state (cached TCP
connections), so Clients should be reused instead of created as
needed.
My reading of the stdlib code suggests that the timeout behaves as
though it we an absolute one across all requests made by the client but
this looks almost accidental because in Client.doFollowingRedirects a
new timer is started for every request (actually an AfterFunc) and no
attempt is made to stop this between requests. For an absolute timeout
I'd expect it to use sync.Once to start the timer on the first request,
so I'm not sure what the intended behaviour is here.
Maybe I am missing something obvious. If not, then perhaps the
documentation needs to be clarified by rewording to something like
"Timeout specifies an overall time limit for all requests made by this
Client."
Cheers,
Ian