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

fix(retries): change default retry_interval to 1 second (was 0.1) #122

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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