From f6bd8f31afa43a9d84d17cef810ea50010f5d6bc Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sun, 21 Jan 2018 18:00:04 +0200 Subject: [PATCH 1/6] Make HTTP client transport replacable Closes: #705 --- client.go | 5 +++++ client_unix.go | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index b0a97804..519ad7d0 100644 --- a/client.go +++ b/client.go @@ -222,6 +222,11 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro return c, nil } +func (c *Client) WithTransport(tr *http.Transport) { + c.initializeNativeClientTransport(tr) + c.HTTPClient = &http.Client{Transport: tr} +} + // NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient. func NewVersionnedTLSClient(endpoint string, cert, key, ca, apiVersionString string) (*Client, error) { return NewVersionedTLSClient(endpoint, cert, key, ca, apiVersionString) diff --git a/client_unix.go b/client_unix.go index ab73cf2b..da0e86fe 100644 --- a/client_unix.go +++ b/client_unix.go @@ -9,6 +9,7 @@ package docker import ( "context" "net" + "net/http" ) // initializeNativeClient initializes the native Unix domain socket client on @@ -17,13 +18,17 @@ func (c *Client) initializeNativeClient() { if c.endpointURL.Scheme != unixProtocol { return } - socketPath := c.endpointURL.Path tr := defaultTransport() + c.initializeNativeClientTransport(tr) +} + +func (c *Client) initializeNativeClientTransport(tr *http.Transport) { + sockPath := c.endpointURL.Path tr.Dial = func(network, addr string) (net.Conn, error) { - return c.Dialer.Dial(unixProtocol, socketPath) + return c.Dialer.Dial(unixProtocol, sockPath) } tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { - return c.Dialer.Dial(unixProtocol, socketPath) + return c.Dialer.Dial(unixProtocol, sockPath) } c.HTTPClient.Transport = tr } From a8f5e915cb6e97a1be70f33da8036a262654f975 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sun, 21 Jan 2018 21:38:15 +0200 Subject: [PATCH 2/6] Fixing tests --- client.go | 4 ++-- client_unix.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 519ad7d0..8fe33a73 100644 --- a/client.go +++ b/client.go @@ -223,8 +223,8 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro } func (c *Client) WithTransport(tr *http.Transport) { - c.initializeNativeClientTransport(tr) - c.HTTPClient = &http.Client{Transport: tr} + initializeNativeClientTransport(c, tr) + c.HTTPClient.Transport = tr } // NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient. diff --git a/client_unix.go b/client_unix.go index da0e86fe..2e8d4fd2 100644 --- a/client_unix.go +++ b/client_unix.go @@ -19,10 +19,10 @@ func (c *Client) initializeNativeClient() { return } tr := defaultTransport() - c.initializeNativeClientTransport(tr) + initializeNativeClientTransport(c, tr) } -func (c *Client) initializeNativeClientTransport(tr *http.Transport) { +func initializeNativeClientTransport(c *Client, tr *http.Transport) { sockPath := c.endpointURL.Path tr.Dial = func(network, addr string) (net.Conn, error) { return c.Dialer.Dial(unixProtocol, sockPath) From c13236aee68231c6cb7ece694d49758aa33c2bb0 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sun, 21 Jan 2018 21:46:08 +0200 Subject: [PATCH 3/6] Refactor base method to make it become reusable whenever it's needed --- client.go | 9 ++++----- client_unix.go | 10 ++++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 8fe33a73..ce201bb2 100644 --- a/client.go +++ b/client.go @@ -218,13 +218,12 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro eventMonitor: new(eventMonitoringState), requestedAPIVersion: requestedAPIVersion, } - c.initializeNativeClient() + initializeNativeClient(c, defaultTransport) return c, nil } -func (c *Client) WithTransport(tr *http.Transport) { - initializeNativeClientTransport(c, tr) - c.HTTPClient.Transport = tr +func (c *Client) WithTransport(trFunc func () *http.Transport) { + initializeNativeClient(c, trFunc) } // NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient. @@ -344,7 +343,7 @@ func NewVersionedTLSClientFromBytes(endpoint string, certPEMBlock, keyPEMBlock, eventMonitor: new(eventMonitoringState), requestedAPIVersion: requestedAPIVersion, } - c.initializeNativeClient() + initializeNativeClient(c, defaultTransport) return c, nil } diff --git a/client_unix.go b/client_unix.go index 2e8d4fd2..ab93845d 100644 --- a/client_unix.go +++ b/client_unix.go @@ -14,16 +14,14 @@ import ( // initializeNativeClient initializes the native Unix domain socket client on // Unix-style operating systems -func (c *Client) initializeNativeClient() { +func initializeNativeClient(c *Client, trFunc func () *http.Transport) { if c.endpointURL.Scheme != unixProtocol { return } - tr := defaultTransport() - initializeNativeClientTransport(c, tr) -} - -func initializeNativeClientTransport(c *Client, tr *http.Transport) { sockPath := c.endpointURL.Path + + tr := trFunc() + tr.Dial = func(network, addr string) (net.Conn, error) { return c.Dialer.Dial(unixProtocol, sockPath) } From d35e79ddfc48d2d722d6affde4dc074d0fef1044 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sun, 21 Jan 2018 21:51:33 +0200 Subject: [PATCH 4/6] Trying to address appveyor issues --- client.go | 6 +++--- client_unix.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index ce201bb2..bdb4c85a 100644 --- a/client.go +++ b/client.go @@ -218,12 +218,12 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro eventMonitor: new(eventMonitoringState), requestedAPIVersion: requestedAPIVersion, } - initializeNativeClient(c, defaultTransport) + c.initializeNativeClient(defaultTransport) return c, nil } func (c *Client) WithTransport(trFunc func () *http.Transport) { - initializeNativeClient(c, trFunc) + c.initializeNativeClient(trFunc) } // NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient. @@ -343,7 +343,7 @@ func NewVersionedTLSClientFromBytes(endpoint string, certPEMBlock, keyPEMBlock, eventMonitor: new(eventMonitoringState), requestedAPIVersion: requestedAPIVersion, } - initializeNativeClient(c, defaultTransport) + c.initializeNativeClient(defaultTransport) return c, nil } diff --git a/client_unix.go b/client_unix.go index ab93845d..b1dfe115 100644 --- a/client_unix.go +++ b/client_unix.go @@ -14,7 +14,7 @@ import ( // initializeNativeClient initializes the native Unix domain socket client on // Unix-style operating systems -func initializeNativeClient(c *Client, trFunc func () *http.Transport) { +func (c *Client) initializeNativeClient(trFunc func () *http.Transport) { if c.endpointURL.Scheme != unixProtocol { return } From 560fb41b51f416daf849985bb37f101d99f2cfdc Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sun, 21 Jan 2018 21:54:59 +0200 Subject: [PATCH 5/6] Adding comment for WithTransport --- client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client.go b/client.go index bdb4c85a..5266fede 100644 --- a/client.go +++ b/client.go @@ -222,6 +222,8 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro return c, nil } +// WithTransport replaces underlying HTTP client of Docker Client by accepting +// a function that returns pointer to a transport object. func (c *Client) WithTransport(trFunc func () *http.Transport) { c.initializeNativeClient(trFunc) } From c8359a51d6c175831c4f72f2a2f58c874ab8a2dd Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sun, 21 Jan 2018 22:01:39 +0200 Subject: [PATCH 6/6] Fixing Windows stuff --- client_windows.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/client_windows.go b/client_windows.go index c9ecc187..c2dda464 100644 --- a/client_windows.go +++ b/client_windows.go @@ -10,6 +10,7 @@ import ( "context" "net" "time" + "net/http" "github.com/Microsoft/go-winio" ) @@ -25,7 +26,7 @@ func (p pipeDialer) Dial(network, address string) (net.Conn, error) { } // initializeNativeClient initializes the native Named Pipe client for Windows -func (c *Client) initializeNativeClient() { +func (c *Client) initializeNativeClient(trFunc func () *http.Transport) { if c.endpointURL.Scheme != namedPipeProtocol { return } @@ -34,7 +35,7 @@ func (c *Client) initializeNativeClient() { timeout := namedPipeConnectTimeout return winio.DialPipe(namedPipePath, &timeout) } - tr := defaultTransport() + tr := trFunc() tr.Dial = dialFunc tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { return dialFunc(network, addr)