Skip to content

Commit 3daef00

Browse files
authored
fix(retries): change default retry_interval to 1 second (was 0.1) (#122)
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.
1 parent a15df4b commit 3daef00

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,26 @@ def __init__(self,
9797
if not isinstance(self.authenticator, Authenticator):
9898
raise ValueError('authenticator should be of type Authenticator')
9999

100-
def enable_retries(self, max_retries: int = 4, retry_interval: float = 0.1) -> None:
101-
"""Setup http_client with retry_config and http_adapter"""
100+
def enable_retries(self, max_retries: int = 4, retry_interval: float = 1.0) -> None:
101+
"""Enable automatic retries on the underlying http client used by the BaseService instance.
102+
103+
Args:
104+
max_retries: the maximum number of retries to attempt for a failed retryable request
105+
retry_interval: the default wait time (in seconds) to use for the first retry attempt.
106+
In general, if a response includes the Retry-After header, that will be used for
107+
the wait time associated with the retry attempt. If the Retry-After header is not
108+
present, then the wait time is based on the retry_interval and retry attempt number:
109+
wait_time = retry_interval * (2 ^ (n-1)), where n is the retry attempt number
110+
"""
102111
self.retry_config = Retry(
103-
total = max_retries,
104-
backoff_factor = retry_interval,
112+
total=max_retries,
113+
backoff_factor=retry_interval,
105114
# List of HTTP status codes to retry on in addition to Timeout/Connection Errors
106-
status_forcelist = [429, 500, 502, 503, 504],
115+
status_forcelist=[429, 500, 502, 503, 504],
107116
# List of HTTP methods to retry on
108117
# Omitting this will default to all methods except POST
109-
allowed_methods=['HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'POST']
118+
allowed_methods=['HEAD', 'GET', 'PUT',
119+
'DELETE', 'OPTIONS', 'TRACE', 'POST']
110120
)
111121
self.http_adapter = HTTPAdapter(max_retries=self.retry_config)
112122
self.http_client.mount('http://', self.http_adapter)
@@ -163,7 +173,8 @@ def configure_service(self, service_name: str) -> None:
163173
if config.get('MAX_RETRIES'):
164174
kwargs["max_retries"] = int(config.get('MAX_RETRIES'))
165175
if config.get('RETRY_INTERVAL'):
166-
kwargs["retry_interval"] = float(config.get('RETRY_INTERVAL'))
176+
kwargs["retry_interval"] = float(
177+
config.get('RETRY_INTERVAL'))
167178
self.enable_retries(**kwargs)
168179

169180
def _set_user_agent_header(self, user_agent_string: str) -> None:

test/test_base_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def test_retry_config_default():
605605
authenticator=NoAuthAuthenticator())
606606
service.enable_retries()
607607
assert service.retry_config.total == 4
608-
assert service.retry_config.backoff_factor == 0.1
608+
assert service.retry_config.backoff_factor == 1.0
609609
assert service.http_client.get_adapter('https://').max_retries.total == 4
610610

611611
# Ensure retries fail after 4 retries

0 commit comments

Comments
 (0)