Aller au contenu

Http Client Lifetime (ARCH5083)

Ce contenu n’est pas encore disponible dans votre langue.

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.

func get(url string) (*http.Response, error) {
client := &http.Client{Timeout: 10 * time.Second}
return client.Get(url)
}
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.