client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
KeepAlive: 5 * time.Second,
}).DialContext,
IdleConnTimeout: 3 * time.Second,
},
}
What's the difference between KeepAlive and IdleConnTimeout?
Which of them exactly does what?
From the documentation:
https://github.com/golang/go/blob/go1.13.8/src/net/dial.go#L72
KeepAlive specifies the interval between keep-alive probes for an active network connection. If zero, keep-alive probes are sent with a default value (currently 15 seconds), 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-alive probes are disabled.
So, if you set the KeepAlive
greater than 0, it will represent the time elapsed from the next probe in order to understand if the network connection is still active.
From the other side, the IdleConnTimeout
:
https://github.com/golang/go/blob/go1.13.8/src/net/dial.go#L72
IdleConnTimeout is the maximum amount of time an idle (keep-alive) connection will remain idle before closing itself. Zero means no limit.
So, if you set an IdleConnTimeout
greater than 0, it represents the time that the connection is still open.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With