mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #39055 from thaJeztah/add_with_timeout_option
Add client.WithTimeout() option
This commit is contained in:
commit
ad9362bb15
2 changed files with 25 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/go-connections/sockets"
|
"github.com/docker/go-connections/sockets"
|
||||||
"github.com/docker/go-connections/tlsconfig"
|
"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
|
// WithHTTPHeaders overrides the client default http headers
|
||||||
func WithHTTPHeaders(headers map[string]string) Opt {
|
func WithHTTPHeaders(headers map[string]string) Opt {
|
||||||
return func(c *Client) error {
|
return func(c *Client) error {
|
||||||
|
|
16
client/options_test.go
Normal file
16
client/options_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue