mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 18:17:55 +00:00 
			
		
		
		
	api/client: allow configurable values for RetryWaitMin and RetryWaitMax (#11773)
This commit is contained in:
		| @@ -79,6 +79,14 @@ type Config struct { | |||||||
| 	// (or http.DefaultClient). | 	// (or http.DefaultClient). | ||||||
| 	HttpClient *http.Client | 	HttpClient *http.Client | ||||||
|  |  | ||||||
|  | 	// MinRetryWait controls the minimum time to wait before retrying when a 5xx | ||||||
|  | 	// error occurs. Defaults to 1000 milliseconds. | ||||||
|  | 	MinRetryWait time.Duration | ||||||
|  |  | ||||||
|  | 	// MaxRetryWait controls the maximum time to wait before retrying when a 5xx | ||||||
|  | 	// error occurs. Defaults to 1500 milliseconds. | ||||||
|  | 	MaxRetryWait time.Duration | ||||||
|  |  | ||||||
| 	// MaxRetries controls the maximum number of times to retry when a 5xx | 	// MaxRetries controls the maximum number of times to retry when a 5xx | ||||||
| 	// error occurs. Set to 0 to disable retrying. Defaults to 2 (for a total | 	// error occurs. Set to 0 to disable retrying. Defaults to 2 (for a total | ||||||
| 	// of three tries). | 	// of three tries). | ||||||
| @@ -153,11 +161,13 @@ type TLSConfig struct { | |||||||
| // If an error is encountered, this will return nil. | // If an error is encountered, this will return nil. | ||||||
| func DefaultConfig() *Config { | func DefaultConfig() *Config { | ||||||
| 	config := &Config{ | 	config := &Config{ | ||||||
| 		Address:    "https://127.0.0.1:8200", | 		Address:      "https://127.0.0.1:8200", | ||||||
| 		HttpClient: cleanhttp.DefaultPooledClient(), | 		HttpClient:   cleanhttp.DefaultPooledClient(), | ||||||
| 		Timeout:    time.Second * 60, | 		Timeout:      time.Second * 60, | ||||||
| 		MaxRetries: 2, | 		MinRetryWait: time.Millisecond * 1000, | ||||||
| 		Backoff:    retryablehttp.LinearJitterBackoff, | 		MaxRetryWait: time.Millisecond * 1500, | ||||||
|  | 		MaxRetries:   2, | ||||||
|  | 		Backoff:      retryablehttp.LinearJitterBackoff, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	transport := config.HttpClient.Transport.(*http.Transport) | 	transport := config.HttpClient.Transport.(*http.Transport) | ||||||
| @@ -416,6 +426,14 @@ func NewClient(c *Config) (*Client, error) { | |||||||
| 	c.modifyLock.Lock() | 	c.modifyLock.Lock() | ||||||
| 	defer c.modifyLock.Unlock() | 	defer c.modifyLock.Unlock() | ||||||
|  |  | ||||||
|  | 	if c.MinRetryWait == 0 { | ||||||
|  | 		c.MinRetryWait = def.MinRetryWait | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if c.MaxRetryWait == 0 { | ||||||
|  | 		c.MaxRetryWait = def.MaxRetryWait | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if c.HttpClient == nil { | 	if c.HttpClient == nil { | ||||||
| 		c.HttpClient = def.HttpClient | 		c.HttpClient = def.HttpClient | ||||||
| 	} | 	} | ||||||
| @@ -476,6 +494,8 @@ func (c *Client) CloneConfig() *Config { | |||||||
| 	newConfig := DefaultConfig() | 	newConfig := DefaultConfig() | ||||||
| 	newConfig.Address = c.config.Address | 	newConfig.Address = c.config.Address | ||||||
| 	newConfig.AgentAddress = c.config.AgentAddress | 	newConfig.AgentAddress = c.config.AgentAddress | ||||||
|  | 	newConfig.MinRetryWait = c.config.MinRetryWait | ||||||
|  | 	newConfig.MaxRetryWait = c.config.MaxRetryWait | ||||||
| 	newConfig.MaxRetries = c.config.MaxRetries | 	newConfig.MaxRetries = c.config.MaxRetries | ||||||
| 	newConfig.Timeout = c.config.Timeout | 	newConfig.Timeout = c.config.Timeout | ||||||
| 	newConfig.Backoff = c.config.Backoff | 	newConfig.Backoff = c.config.Backoff | ||||||
| @@ -540,6 +560,44 @@ func (c *Client) Limiter() *rate.Limiter { | |||||||
| 	return c.config.Limiter | 	return c.config.Limiter | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // SetMinRetryWait sets the minimum time to wait before retrying in the case of certain errors. | ||||||
|  | func (c *Client) SetMinRetryWait(retryWait time.Duration) { | ||||||
|  | 	c.modifyLock.RLock() | ||||||
|  | 	defer c.modifyLock.RUnlock() | ||||||
|  | 	c.config.modifyLock.Lock() | ||||||
|  | 	defer c.config.modifyLock.Unlock() | ||||||
|  |  | ||||||
|  | 	c.config.MinRetryWait = retryWait | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (c *Client) MinRetryWait() time.Duration { | ||||||
|  | 	c.modifyLock.RLock() | ||||||
|  | 	defer c.modifyLock.RUnlock() | ||||||
|  | 	c.config.modifyLock.RLock() | ||||||
|  | 	defer c.config.modifyLock.RUnlock() | ||||||
|  |  | ||||||
|  | 	return c.config.MinRetryWait | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // SetMaxRetryWait sets the maximum time to wait before retrying in the case of certain errors. | ||||||
|  | func (c *Client) SetMaxRetryWait(retryWait time.Duration) { | ||||||
|  | 	c.modifyLock.RLock() | ||||||
|  | 	defer c.modifyLock.RUnlock() | ||||||
|  | 	c.config.modifyLock.Lock() | ||||||
|  | 	defer c.config.modifyLock.Unlock() | ||||||
|  |  | ||||||
|  | 	c.config.MaxRetryWait = retryWait | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (c *Client) MaxRetryWait() time.Duration { | ||||||
|  | 	c.modifyLock.RLock() | ||||||
|  | 	defer c.modifyLock.RUnlock() | ||||||
|  | 	c.config.modifyLock.RLock() | ||||||
|  | 	defer c.config.modifyLock.RUnlock() | ||||||
|  |  | ||||||
|  | 	return c.config.MaxRetryWait | ||||||
|  | } | ||||||
|  |  | ||||||
| // SetMaxRetries sets the number of retries that will be used in the case of certain errors | // SetMaxRetries sets the number of retries that will be used in the case of certain errors | ||||||
| func (c *Client) SetMaxRetries(retries int) { | func (c *Client) SetMaxRetries(retries int) { | ||||||
| 	c.modifyLock.RLock() | 	c.modifyLock.RLock() | ||||||
| @@ -770,6 +828,8 @@ func (c *Client) Clone() (*Client, error) { | |||||||
| 	newConfig := &Config{ | 	newConfig := &Config{ | ||||||
| 		Address:          config.Address, | 		Address:          config.Address, | ||||||
| 		HttpClient:       config.HttpClient, | 		HttpClient:       config.HttpClient, | ||||||
|  | 		MinRetryWait:     config.MinRetryWait, | ||||||
|  | 		MaxRetryWait:     config.MaxRetryWait, | ||||||
| 		MaxRetries:       config.MaxRetries, | 		MaxRetries:       config.MaxRetries, | ||||||
| 		Timeout:          config.Timeout, | 		Timeout:          config.Timeout, | ||||||
| 		Backoff:          config.Backoff, | 		Backoff:          config.Backoff, | ||||||
| @@ -873,6 +933,8 @@ func (c *Client) RawRequestWithContext(ctx context.Context, r *Request) (*Respon | |||||||
|  |  | ||||||
| 	c.config.modifyLock.RLock() | 	c.config.modifyLock.RLock() | ||||||
| 	limiter := c.config.Limiter | 	limiter := c.config.Limiter | ||||||
|  | 	minRetryWait := c.config.MinRetryWait | ||||||
|  | 	maxRetryWait := c.config.MaxRetryWait | ||||||
| 	maxRetries := c.config.MaxRetries | 	maxRetries := c.config.MaxRetries | ||||||
| 	checkRetry := c.config.CheckRetry | 	checkRetry := c.config.CheckRetry | ||||||
| 	backoff := c.config.Backoff | 	backoff := c.config.Backoff | ||||||
| @@ -934,8 +996,8 @@ START: | |||||||
|  |  | ||||||
| 	client := &retryablehttp.Client{ | 	client := &retryablehttp.Client{ | ||||||
| 		HTTPClient:   httpClient, | 		HTTPClient:   httpClient, | ||||||
| 		RetryWaitMin: 1000 * time.Millisecond, | 		RetryWaitMin: minRetryWait, | ||||||
| 		RetryWaitMax: 1500 * time.Millisecond, | 		RetryWaitMax: maxRetryWait, | ||||||
| 		RetryMax:     maxRetries, | 		RetryMax:     maxRetries, | ||||||
| 		Backoff:      backoff, | 		Backoff:      backoff, | ||||||
| 		CheckRetry:   checkRetry, | 		CheckRetry:   checkRetry, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Rajwinder Mahal
					Rajwinder Mahal