11package internal
22
33import (
4- "fmt"
5- "net/http"
6- "time"
4+ "fmt"
5+ "crypto/tls"
6+ "net/http"
7+ "time"
78
8- "github.com/hashicorp/go-retryablehttp"
9+ "github.com/hashicorp/go-retryablehttp"
910)
1011
1112// HeaderTransport is a custom RoundTripper that adds default headers to requests
@@ -28,29 +29,43 @@ func (t *HeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
2829 return base .RoundTrip (req )
2930}
3031
31- // RetryableClient returns a new http.Client with a retryablehttp.Client
32- // configured with the provided parameters.
33- func RetryableClient (retries int , timeout time.Duration , rps int , logger interface {}) (* http.Client , error ) {
34- if retries < 0 {
32+ // RetryableClientOptions configures the retryable HTTP client.
33+ type RetryableClientOptions struct {
34+ Retries int
35+ Timeout time.Duration
36+ RPS int
37+ Logger interface {}
38+ Insecure bool
39+ }
40+
41+ // RetryableClient returns a new http.Client with a retryablehttp.Client configured per opts.
42+ func RetryableClient (opts RetryableClientOptions ) (* http.Client , error ) {
43+ if opts .Retries < 0 {
3544 return nil , fmt .Errorf ("retries must be greater than 0" )
3645 }
37- if timeout < 0 {
46+ if opts . Timeout < 0 {
3847 return nil , fmt .Errorf ("timeout must be greater than 0" )
3948 }
40- if rps < 0 {
49+ if opts . RPS < 0 {
4150 return nil , fmt .Errorf ("rps must be greater than 0" )
4251 }
4352
4453 retryClient := retryablehttp .NewClient ()
45- retryClient .RetryMax = retries
54+ retryClient .RetryMax = opts . Retries
4655 retryClient .RetryWaitMin = 1 * time .Second
4756 retryClient .RetryWaitMax = 30 * time .Second
48- retryClient .HTTPClient .Timeout = timeout
49- retryClient .Logger = logger
50- if rps > 0 {
51- retryClient .Backoff = func (min , max time.Duration , attemptNum int , resp * http.Response ) time.Duration {
57+ retryClient .HTTPClient .Timeout = opts .Timeout
58+ retryClient .Logger = opts .Logger
59+ if opts .Insecure {
60+ // Minimal transport overriding to skip TLS verification.
61+ retryClient .HTTPClient .Transport = & http.Transport {
62+ TLSClientConfig : & tls.Config {InsecureSkipVerify : true },
63+ }
64+ }
65+ if opts .RPS > 0 {
66+ retryClient .Backoff = func (min , max time.Duration , attemptNum int , resp * http.Response ) time.Duration {
5267 // Ensure we wait at least 1/rps between requests
53- minWait := time .Second / time .Duration (rps )
68+ minWait := time .Second / time .Duration (opts . RPS )
5469 if min < minWait {
5570 min = minWait
5671 }
0 commit comments