From 44982c775edcba6fc9eed156da584cdd324a3029 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 9 Apr 2019 10:07:30 +0200 Subject: [PATCH] Add client.WithTimeout() option Signed-off-by: Sebastiaan van Stijn --- client/options.go | 9 +++++++++ client/options_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 client/options_test.go diff --git a/client/options.go b/client/options.go index 33921867bf..6c2d7968b4 100644 --- a/client/options.go +++ b/client/options.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "path/filepath" + "time" "github.com/docker/go-connections/sockets" "github.com/docker/go-connections/tlsconfig" @@ -102,6 +103,14 @@ func WithHTTPClient(client *http.Client) Opt { } } +// WithTimeout configures the time limit for requests made by the HTTP client +func WithTimeout(timeout time.Duration) Opt { + return func(c *Client) error { + c.client.Timeout = timeout + return nil + } +} + // WithHTTPHeaders overrides the client default http headers func WithHTTPHeaders(headers map[string]string) Opt { return func(c *Client) error { diff --git a/client/options_test.go b/client/options_test.go new file mode 100644 index 0000000000..a0e0b07913 --- /dev/null +++ b/client/options_test.go @@ -0,0 +1,16 @@ +package client + +import ( + "testing" + "time" + + "gotest.tools/assert" +) + +func TestOptionWithTimeout(t *testing.T) { + timeout := 10 * time.Second + c, err := NewClientWithOpts(WithTimeout(timeout)) + assert.NilError(t, err) + assert.Check(t, c.client != nil) + assert.Equal(t, c.client.Timeout, timeout) +}