HTTP

Making request faster by reusing connection

http.DefaultClient has keep-alive enabled. If you create your own customed http client, you should configure it to use keep-alive

tr := &http.Transport{
TLSClientConfig:
&tls.Config{RootCAs: pool},
DisableCompression: true,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://example.com")
  • The body of the response would need to be closed before another HTTP request and response could use the connection

Last updated on