For testing purposes I'm trying to create a net/http.Client
in Go that has connection pooling disabled. What I'm trying to achieve is a new TCP connection is established to the address on every HTTP/1.x request.
Currently I have:
c = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
Any ideas how should I tweak this?
I'm seeing that if I set c.Transport.MaxIdleConns = 1
this could work, but I'm not exactly sure if this still allows 1 in-use + 1 idle (2 total) TCP connections:
// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int
Similarly, it seems like c.Dialer.KeepAlive = -1
could do this, too:
// KeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are enabled if supported by the protocol
// and operating system. Network protocols or operating systems
// that do not support keep-alives ignore this field.
// If negative, keep-alives are disabled.
but I'm not sure about the behavior for TCP connections + Keep-Alive + HTTP.
Another approach is to try to kill idle TCP connections as soon as possible, so I set c.Transport.IdleConnTimeout = 1*time.Nanosecond
.
When I did this, my Client.Do()
now occassionally returns error:
tls: use of closed connection
I'm suspecting this is a Go stdlib issue (perhaps a race) that it uses a connection that should've been taken out of a pool.