mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
api: use cleanhttp.DefaultPooledTransport for default API client (#12492)
We expect every Nomad API client to use a single connection to any given agent, so take advantage of keep-alive by switching the default transport to `DefaultPooledClient`. Provide a facility to close idle connections for testing purposes. Restores the previously reverted #12409 Co-authored-by: Ben Buzbee <bbuzbee@cloudflare.com>
This commit is contained in:
14
api/api.go
14
api/api.go
@@ -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{
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user