Search code examples
httpgoconnection-pooling

In Golang http client, why there is MaxConnsPerHost but no MaxConns


  1. I see in Go http package:

    MaxIdleConnsPerHost int

    But I do not see any MaxConns for the whole http client, why?

  2. If I only limit MaxIdleConns (per the whole client) is it possible to have more than this limit of active connections? (I assume the answer is a simple yes, but is it only theorethical?)

From the docs:

// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int



// 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.
//
// Zero means no limit.
MaxConnsPerHost int

Solution

  • I do not see any MaxConns for the whole http client, why?

    Because there is none. If you do not want to make more than n concurrent requests with that client, just don't do it (If the client would limit, it would be a major pita to use).

    If I only limit MaxIdleConns (per the whole client) is it possible to have more than this limit of active connections?

    Yes. MaxIdlConns limits idle connection not non-idle ones.