Skip to content

Commit

Permalink
fix(retries): change default retry_interval to 1 second (was 0.1) (#122)
Browse files Browse the repository at this point in the history
This commit changes the default value of the retry_interval
parameter (in the BaseService.enable_retries method) from 0.1
to 1.0.   This will result in default retry intervals
of 1, 2, 4, 8, ... seconds instead of 0.1, 0.2, 0.4, 0.8, ... seconds.
This change will align the python core retry support with Go.
Also added some detailed info in the docstring for the method as well.
  • Loading branch information
padamstx authored Aug 20, 2021
1 parent a15df4b commit 3daef00
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,26 @@ def __init__(self,
if not isinstance(self.authenticator, Authenticator):
raise ValueError('authenticator should be of type Authenticator')

def enable_retries(self, max_retries: int = 4, retry_interval: float = 0.1) -> None:
"""Setup http_client with retry_config and http_adapter"""
def enable_retries(self, max_retries: int = 4, retry_interval: float = 1.0) -> None:
"""Enable automatic retries on the underlying http client used by the BaseService instance.
Args:
max_retries: the maximum number of retries to attempt for a failed retryable request
retry_interval: the default wait time (in seconds) to use for the first retry attempt.
In general, if a response includes the Retry-After header, that will be used for
the wait time associated with the retry attempt. If the Retry-After header is not
present, then the wait time is based on the retry_interval and retry attempt number:
wait_time = retry_interval * (2 ^ (n-1)), where n is the retry attempt number
"""
self.retry_config = Retry(
total = max_retries,
backoff_factor = retry_interval,
total=max_retries,
backoff_factor=retry_interval,
# List of HTTP status codes to retry on in addition to Timeout/Connection Errors
status_forcelist = [429, 500, 502, 503, 504],
status_forcelist=[429, 500, 502, 503, 504],
# List of HTTP methods to retry on
# Omitting this will default to all methods except POST
allowed_methods=['HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'POST']
allowed_methods=['HEAD', 'GET', 'PUT',
'DELETE', 'OPTIONS', 'TRACE', 'POST']
)
self.http_adapter = HTTPAdapter(max_retries=self.retry_config)
self.http_client.mount('http://', self.http_adapter)
Expand Down Expand Up @@ -163,7 +173,8 @@ def configure_service(self, service_name: str) -> None:
if config.get('MAX_RETRIES'):
kwargs["max_retries"] = int(config.get('MAX_RETRIES'))
if config.get('RETRY_INTERVAL'):
kwargs["retry_interval"] = float(config.get('RETRY_INTERVAL'))
kwargs["retry_interval"] = float(
config.get('RETRY_INTERVAL'))
self.enable_retries(**kwargs)

def _set_user_agent_header(self, user_agent_string: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def test_retry_config_default():
authenticator=NoAuthAuthenticator())
service.enable_retries()
assert service.retry_config.total == 4
assert service.retry_config.backoff_factor == 0.1
assert service.retry_config.backoff_factor == 1.0
assert service.http_client.get_adapter('https://').max_retries.total == 4

# Ensure retries fail after 4 retries
Expand Down

0 comments on commit 3daef00

Please sign in to comment.