Browse code

ensures that transport.Client is closed while using cli.NewClient with *http.Client = nil.

Signed-off-by: qudongfang <qudongfang@gmail.com>

qudongfang authored on 2016/09/08 10:57:54
Showing 2 changed files
... ...
@@ -108,6 +108,19 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
108 108
 	}, nil
109 109
 }
110 110
 
111
+// Close ensures that transport.Client is closed
112
+// especially needed while using NewClient with *http.Client = nil
113
+// for example
114
+// client.NewClient("unix:///var/run/docker.sock", nil, "v1.18", map[string]string{"User-Agent": "engine-api-cli-1.0"})
115
+func (cli *Client) Close() error {
116
+
117
+	if t, ok := cli.client.Transport.(*http.Transport); ok {
118
+		t.CloseIdleConnections()
119
+	}
120
+
121
+	return nil
122
+}
123
+
111 124
 // getAPIPath returns the versioned request path to call the api.
112 125
 // It appends the query parameters to the path if they are not empty.
113 126
 func (cli *Client) getAPIPath(p string, query url.Values) string {
... ...
@@ -133,6 +133,11 @@ func TestGetAPIPath(t *testing.T) {
133 133
 		if g != cs.e {
134 134
 			t.Fatalf("Expected %s, got %s", cs.e, g)
135 135
 		}
136
+
137
+		err = c.Close()
138
+		if nil != err {
139
+			t.Fatalf("close client failed, error message: %s", err)
140
+		}
136 141
 	}
137 142
 }
138 143