M
M
memba2017-09-04 13:34:11
go
memba, 2017-09-04 13:34:11

How does KeepAlive work in net.Dialer?

Hey!
The standard http.Client in Go looks something like this:

http.Client{
  Transport: &http.Transport{
    Dial: (&net.Dialer{
      Timeout:   30 * time.Second,
      KeepAlive: 30 * time.Second,
      DualStack: true,
    }).DialContext,
  MaxIdleConns:          100,
  IdleConnTimeout:       90 * time.Second,
  TLSHandshakeTimeout:   10 * time.Second,
  ExpectContinueTimeout: 1 * time.Second,
  },
}

As I understand it:
MaxIdleConns - the maximum number of connections that can be in the pool of free connections for ALL hosts.
IdleConnTimeout - The maximum time idle connections can be kept in the pool.
There is also
MaxIdleConnsPerHost - which by default is equal to DefaultMaxIdleConnsPerHost ... i.e. 2
So if I establish 10 connections with one host, then only 2 free connections will be stored in the MaxIdleConns pool ... and not 100.
And there is also KeepAlive : 30 sec. How does it interact with IdleConnTimeout ?
If I establish a connection, make a request, then it will fall into the pool. So will it die in 30s or 90s as specified by IdleConnTimeout ? And if the request took 15 seconds, then the connection fell into the pool, lay there for 25 seconds, and again I make a request for 15 seconds. Connection will die during request? It's just that Client.Timeout works that way.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question