最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - Interactions between Golang http.Client.Timeout and http.Transport.MaxConnsPerHost - Stack Overflow

programmeradmin6浏览0评论

The docs for http.Client.Timeout says: The timeout includes connection time, any redirects, and reading the response body

The docs for http.Transport.MaxConnsPerHost says: MaxConnsPerHost optionally limits the total number of connections per host, including connections in the dialing, active, and idle states. On limit violation, dials will block.

I have a few questions:

  1. Does timeout connection time include time stuck in the queue waiting on available connection if current number of connections equals http.Transport.MaxConnsPerHost?

  2. What does a dial encompass?

  3. If yes to question 1, is it possible to set a timeout for client request that does not including waiting for available connection due to MaxConnsPerHost limit?

I currently have both set and I'm getting client timeouts where I have lots of simultaneous connections even though my timeout is plenty enough for any single http request and server side doesn't seem to be a bottleneck

The docs for http.Client.Timeout says: The timeout includes connection time, any redirects, and reading the response body

The docs for http.Transport.MaxConnsPerHost says: MaxConnsPerHost optionally limits the total number of connections per host, including connections in the dialing, active, and idle states. On limit violation, dials will block.

I have a few questions:

  1. Does timeout connection time include time stuck in the queue waiting on available connection if current number of connections equals http.Transport.MaxConnsPerHost?

  2. What does a dial encompass?

  3. If yes to question 1, is it possible to set a timeout for client request that does not including waiting for available connection due to MaxConnsPerHost limit?

I currently have both set and I'm getting client timeouts where I have lots of simultaneous connections even though my timeout is plenty enough for any single http request and server side doesn't seem to be a bottleneck

Share Improve this question asked Feb 5 at 10:23 bobbuilderbobbuilder 211 silver badge4 bronze badges 1
  • It should have been addressed in the staging ground, but this would be better if you can turn this into a single clear question, rather than a few about the behavior of of the http client. One option you have is to use a separate semaphore to limit your concurrency, so the request isn't started until you know it can proceed, and the overall deadline won't be calculated until you want the request to begin. – Mr_Pink Commented Feb 5 at 15:35
Add a comment  | 

1 Answer 1

Reset to default 3

Does timeout connection time include time stuck in the queue waiting on available connection if current number of connections equals http.Transport.MaxConnsPerHost?

MaxConnsPerHost applies per RoundTrip call, so yes, http.Client.Timeout includes the time spent waiting in the queue, which can even be multiple times - for the initial request and for each redirect.

What does a dial encompass?

In Go terminology, "dialing" means establishing a connection. In case of HTTP it involves making a TCP connect call to the server.

If yes to question 1, is it possible to set a timeout for client request that does not including waiting for available connection due to MaxConnsPerHost limit?

Since an HTTP request potentially involves making multiple round-trips to the server, the simple answer to this question is no.

You can set other timeouts though:

  • net.Dialer.Timeout
  • http.transport.TLSHandshakeTimeout
  • http.Transport.ResponseHeaderTimeout

Or write your own RoundTripper wrapper that implements MaxConnsPerHost functionality and sets the context timeout before passing it down to the default transport.

It all depends on what you're trying to achieve.

发布评论

评论列表(0)

  1. 暂无评论