Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of DialContext KeepAlive and Transport IdleTimeout of go http.Client

Tags:

go

httpclient

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?

like image 683
Subby Avatar asked Oct 11 '17 10:10

Subby


1 Answers

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.

like image 150
alessiosavi Avatar answered Oct 26 '22 12:10

alessiosavi