Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: use cleanhttp.DefaultPooledTransport for default API client #12492

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/12492.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: default to using `DefaultPooledTransport` client to support keep-alive by default
```
14 changes: 13 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (t *TLSConfig) Copy() *TLSConfig {
}

func defaultHttpClient() *http.Client {
httpClient := cleanhttp.DefaultClient()
httpClient := cleanhttp.DefaultPooledClient()
transport := httpClient.Transport.(*http.Transport)
transport.TLSHandshakeTimeout = 10 * time.Second
transport.TLSClientConfig = &tls.Config{
Expand Down Expand Up @@ -476,6 +476,18 @@ func NewClient(config *Config) (*Client, error) {
return client, nil
}

// Close closes the client's idle keep-alived connections. The default
// client configuration uses keep-alive to maintain connections and
// you should instantiate a single Client and reuse it for all
// requests from the same host. Connections will be closed
// automatically once the client is garbage collected. If you are
// creating multiple clients on the same host (for example, for
// testing), it may be useful to call Close() to avoid hitting
// connection limits.
func (c *Client) Close() {
c.httpClient.CloseIdleConnections()
}

// Address return the address of the Nomad agent
func (c *Client) Address() string {
return c.config.Address
Expand Down
2 changes: 2 additions & 0 deletions command/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,8 @@ func TestHTTPServer_Limits_OK(t *testing.T) {
conf.Address = a.HTTPAddr()
conf.TLSConfig.Insecure = true
client, err := api.NewClient(conf)
defer client.Close()

require.NoError(t, err)

// Assert a blocking query isn't timed out by the
Expand Down