Http Client Lifetime (ARCH5083)
Key go.performance.http-client-lifetime · Severity error · Category performance
Constructing a new http.Client on every request prevents connection-pool reuse and leads to socket exhaustion. Reuse the client as a single shared instance.
Incorrect
Section titled “Incorrect”func get(url string) (*http.Response, error) { client := &http.Client{Timeout: 10 * time.Second} return client.Get(url)}Correct
Section titled “Correct”var sharedClient = &http.Client{Timeout: 10 * time.Second}
func get(url string) (*http.Response, error) { return sharedClient.Get(url)}Create the http.Client once as a shared instance and reuse it.