From 9403a5b63e1ba5e86ffeacfb19f35e67a192cbdd Mon Sep 17 00:00:00 2001 From: qudongfang Date: Thu, 8 Sep 2016 09:57:54 +0800 Subject: [PATCH] ensures that transport.Client is closed while using cli.NewClient with *http.Client = nil. Signed-off-by: qudongfang --- client.go | 13 +++++++++++++ client_test.go | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/client.go b/client.go index deccb4ab74..bee429b8ca 100644 --- a/client.go +++ b/client.go @@ -108,6 +108,19 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map }, 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. // It appends the query parameters to the path if they are not empty. func (cli *Client) getAPIPath(p string, query url.Values) string { diff --git a/client_test.go b/client_test.go index 60e44dc299..222f23d45e 100644 --- a/client_test.go +++ b/client_test.go @@ -133,6 +133,11 @@ func TestGetAPIPath(t *testing.T) { if g != cs.e { t.Fatalf("Expected %s, got %s", cs.e, g) } + + err = c.Close() + if nil != err { + t.Fatalf("close client failed, error message: %s", err) + } } }