feat(getter): add timeout option (#7950)

To allow finer grain control over the request when utilizing
`getter` as a package.

Signed-off-by: Hidde Beydals <hello@hidde.co>
pull/8124/head
Hidde Beydals 5 years ago committed by GitHub
parent 374c721240
commit ae738d7d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,6 +18,7 @@ package getter
import ( import (
"bytes" "bytes"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -36,6 +37,7 @@ type options struct {
username string username string
password string password string
userAgent string userAgent string
timeout time.Duration
} }
// Option allows specifying various settings configurable by the user for overriding the defaults // Option allows specifying various settings configurable by the user for overriding the defaults
@ -81,6 +83,13 @@ func WithTLSClientConfig(certFile, keyFile, caFile string) Option {
} }
} }
// WithTimeout sets the timeout for requests
func WithTimeout(timeout time.Duration) Option {
return func(opts *options) {
opts.timeout = timeout
}
}
// Getter is an interface to support GET to the specified URL. // Getter is an interface to support GET to the specified URL.
type Getter interface { type Getter interface {
// Get file content by url string // Get file content by url string

@ -119,6 +119,7 @@ func (g *HTTPGetter) httpClient() (*http.Client, error) {
client := &http.Client{ client := &http.Client{
Transport: transport, Transport: transport,
Timeout: g.opts.timeout,
} }
return client, nil return client, nil

@ -26,6 +26,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -48,6 +49,7 @@ func TestHTTPGetter(t *testing.T) {
join := filepath.Join join := filepath.Join
ca, pub, priv := join(cd, "rootca.crt"), join(cd, "crt.pem"), join(cd, "key.pem") ca, pub, priv := join(cd, "rootca.crt"), join(cd, "crt.pem"), join(cd, "key.pem")
insecure := false insecure := false
timeout := time.Second * 5
// Test with options // Test with options
g, err = NewHTTPGetter( g, err = NewHTTPGetter(
@ -55,6 +57,7 @@ func TestHTTPGetter(t *testing.T) {
WithUserAgent("Groot"), WithUserAgent("Groot"),
WithTLSClientConfig(pub, priv, ca), WithTLSClientConfig(pub, priv, ca),
WithInsecureSkipVerifyTLS(insecure), WithInsecureSkipVerifyTLS(insecure),
WithTimeout(timeout),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -93,6 +96,10 @@ func TestHTTPGetter(t *testing.T) {
t.Errorf("Expected NewHTTPGetter to contain %t as InsecureSkipVerifyTLs flag, got %t", false, hg.opts.insecureSkipVerifyTLS) t.Errorf("Expected NewHTTPGetter to contain %t as InsecureSkipVerifyTLs flag, got %t", false, hg.opts.insecureSkipVerifyTLS)
} }
if hg.opts.timeout != timeout {
t.Errorf("Expected NewHTTPGetter to contain %s as Timeout flag, got %s", timeout, hg.opts.timeout)
}
// Test if setting insecureSkipVerifyTLS is being passed to the ops // Test if setting insecureSkipVerifyTLS is being passed to the ops
insecure = true insecure = true

Loading…
Cancel
Save