Browse code

Merge pull request #26402 from qudongfang/ensure_client_transport_be_closed

ensure transport.Client be closed

Tibor Vass authored on 2016/10/27 03:51:51
Showing 2 changed files
... ...
@@ -168,6 +168,19 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
168 168
 	}, nil
169 169
 }
170 170
 
171
+// Close ensures that transport.Client is closed
172
+// especially needed while using NewClient with *http.Client = nil
173
+// for example
174
+// client.NewClient("unix:///var/run/docker.sock", nil, "v1.18", map[string]string{"User-Agent": "engine-api-cli-1.0"})
175
+func (cli *Client) Close() error {
176
+
177
+	if t, ok := cli.client.Transport.(*http.Transport); ok {
178
+		t.CloseIdleConnections()
179
+	}
180
+
181
+	return nil
182
+}
183
+
171 184
 // getAPIPath returns the versioned request path to call the api.
172 185
 // It appends the query parameters to the path if they are not empty.
173 186
 func (cli *Client) getAPIPath(p string, query url.Values) string {
... ...
@@ -162,6 +162,11 @@ func TestGetAPIPath(t *testing.T) {
162 162
 		if g != cs.e {
163 163
 			t.Fatalf("Expected %s, got %s", cs.e, g)
164 164
 		}
165
+
166
+		err = c.Close()
167
+		if nil != err {
168
+			t.Fatalf("close client failed, error message: %s", err)
169
+		}
165 170
 	}
166 171
 }
167 172