
Description
Hi,
Currently it seems that it's only possible to enable or disable HTTP KeepAlive
. The following code shows how to disable it:
client = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
There does not seem to be configuration which allows you to specify a KeepAlive timeout, which would abandon idle connections that are inactive for longer than the timeout.
This is important as clients operating behind a Firewall / Proxy or targeting a server running behind a Firewall / Load Balancer might get their TCP connection closed after some period (e.g. 5 minutes) without getting informed. When this happens, Go fails the next HTTP call with read: connection reset by peer
.
I faced such a scenario and checked Ruby to see how it would handle. It has multiple protections in place so that it does not error with read: connection reset by peer
.
- It retries idempotent requests before failing. (Though I never got to this fallback)
- Most imporantly, it has an HTTP KeepAlive timeout configuration that set to 2 seconds by default: https://github.com/ruby/ruby/blob/ec0198fd8cfee7ae499602235fcf3a9318f9b691/lib/net/http.rb#L1478
It is important to note that I am asking for an HTTP KeepAlive Timeout configuration. There is already a TCP KeepAlive Timeout configuration available in Go, which works just fine, but has a different purpose - to health-check a TCP connection by pushing regular ACK packages.