Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support setting the API client retry policy #7331

Merged
merged 2 commits into from Oct 28, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion api/client.go
Expand Up @@ -88,6 +88,9 @@ type Config struct {
// The Backoff function to use; a default is used if not provided
Backoff retryablehttp.Backoff

// The CheckRetry function to use; a default is used if not provided
CheckRetry retryablehttp.CheckRetry

// Limiter is the rate limiter used by the client.
// If this pointer is nil, then there will be no limit set.
// In contrast, if this pointer is set, even to an empty struct,
Expand Down Expand Up @@ -171,6 +174,7 @@ func DefaultConfig() *Config {
}

config.Backoff = retryablehttp.LinearJitterBackoff
config.CheckRetry = retryablehttp.DefaultRetryPolicy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment isn't necessary since you have a nil check later, and it would be good to not explicitly assign it so that anyone checking whether a func has already been set will see that it hasn't without having to see if it's been set to DefaultRetryPolicy. Just makes logic nicer in some cases.

config.MaxRetries = 2

return config
Expand Down Expand Up @@ -488,6 +492,16 @@ func (c *Client) SetMaxRetries(retries int) {
c.config.MaxRetries = retries
}

// SetCheckRetry sets the CheckRetry function to be used for future requests.
func (c *Client) SetCheckRetry(checkRetry retryablehttp.CheckRetry) {
c.modifyLock.RLock()
c.config.modifyLock.Lock()
defer c.config.modifyLock.Unlock()
c.modifyLock.RUnlock()

c.config.CheckRetry = checkRetry
}

// SetClientTimeout sets the client request timeout
func (c *Client) SetClientTimeout(timeout time.Duration) {
c.modifyLock.RLock()
Expand Down Expand Up @@ -643,6 +657,7 @@ func (c *Client) Clone() (*Client, error) {
MaxRetries: config.MaxRetries,
Timeout: config.Timeout,
Backoff: config.Backoff,
CheckRetry: config.CheckRetry,
Limiter: config.Limiter,
}
config.modifyLock.RUnlock()
Expand Down Expand Up @@ -740,6 +755,7 @@ func (c *Client) RawRequestWithContext(ctx context.Context, r *Request) (*Respon
c.config.modifyLock.RLock()
limiter := c.config.Limiter
maxRetries := c.config.MaxRetries
checkRetry := c.config.CheckRetry
backoff := c.config.Backoff
httpClient := c.config.HttpClient
timeout := c.config.Timeout
Expand Down Expand Up @@ -784,13 +800,17 @@ START:
backoff = retryablehttp.LinearJitterBackoff
}

if checkRetry == nil {
checkRetry = retryablehttp.DefaultRetryPolicy
}

client := &retryablehttp.Client{
HTTPClient: httpClient,
RetryWaitMin: 1000 * time.Millisecond,
RetryWaitMax: 1500 * time.Millisecond,
RetryMax: maxRetries,
CheckRetry: retryablehttp.DefaultRetryPolicy,
Backoff: backoff,
CheckRetry: checkRetry,
ErrorHandler: retryablehttp.PassthroughErrorHandler,
}

Expand Down