mirror of https://github.com/docker/cli.git
Merge pull request #26402 from qudongfang/ensure_client_transport_be_closed
ensure transport.Client be closed
This commit is contained in:
commit
49fa58a2e9
13
client.go
13
client.go
|
@ -168,6 +168,19 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close ensures that transport.Client is closed
|
||||||
|
// especially needed while using NewClient with *http.Client = nil
|
||||||
|
// for example
|
||||||
|
// client.NewClient("unix:///var/run/docker.sock", nil, "v1.18", map[string]string{"User-Agent": "engine-api-cli-1.0"})
|
||||||
|
func (cli *Client) Close() error {
|
||||||
|
|
||||||
|
if t, ok := cli.client.Transport.(*http.Transport); ok {
|
||||||
|
t.CloseIdleConnections()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// getAPIPath returns the versioned request path to call the api.
|
// getAPIPath returns the versioned request path to call the api.
|
||||||
// It appends the query parameters to the path if they are not empty.
|
// It appends the query parameters to the path if they are not empty.
|
||||||
func (cli *Client) getAPIPath(p string, query url.Values) string {
|
func (cli *Client) getAPIPath(p string, query url.Values) string {
|
||||||
|
|
|
@ -162,6 +162,11 @@ func TestGetAPIPath(t *testing.T) {
|
||||||
if g != cs.e {
|
if g != cs.e {
|
||||||
t.Fatalf("Expected %s, got %s", cs.e, g)
|
t.Fatalf("Expected %s, got %s", cs.e, g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = c.Close()
|
||||||
|
if nil != err {
|
||||||
|
t.Fatalf("close client failed, error message: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue