Browse code

Merge pull request #39055 from thaJeztah/add_with_timeout_option

Add client.WithTimeout() option

Akihiro Suda authored on 2019/04/16 17:05:40
Showing 2 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"net/http"
7 7
 	"os"
8 8
 	"path/filepath"
9
+	"time"
9 10
 
10 11
 	"github.com/docker/go-connections/sockets"
11 12
 	"github.com/docker/go-connections/tlsconfig"
... ...
@@ -102,6 +103,14 @@ func WithHTTPClient(client *http.Client) Opt {
102 102
 	}
103 103
 }
104 104
 
105
+// WithTimeout configures the time limit for requests made by the HTTP client
106
+func WithTimeout(timeout time.Duration) Opt {
107
+	return func(c *Client) error {
108
+		c.client.Timeout = timeout
109
+		return nil
110
+	}
111
+}
112
+
105 113
 // WithHTTPHeaders overrides the client default http headers
106 114
 func WithHTTPHeaders(headers map[string]string) Opt {
107 115
 	return func(c *Client) error {
108 116
new file mode 100644
... ...
@@ -0,0 +1,16 @@
0
+package client
1
+
2
+import (
3
+	"testing"
4
+	"time"
5
+
6
+	"gotest.tools/assert"
7
+)
8
+
9
+func TestOptionWithTimeout(t *testing.T) {
10
+	timeout := 10 * time.Second
11
+	c, err := NewClientWithOpts(WithTimeout(timeout))
12
+	assert.NilError(t, err)
13
+	assert.Check(t, c.client != nil)
14
+	assert.Equal(t, c.client.Timeout, timeout)
15
+}