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: use retry_interval as retry backoff_max #184

Merged
merged 1 commit into from
Dec 11, 2023
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
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "package-lock.json|^.secrets.baseline$",
"lines": null
},
"generated_at": "2023-11-10T17:28:14Z",
"generated_at": "2023-12-11T17:30:56Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -416,15 +416,15 @@
"hashed_secret": "2863fa4b5510c46afc2bd2998dfbc0cf3d6df032",
"is_secret": false,
"is_verified": false,
"line_number": 528,
"line_number": 527,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "b9cad336062c0dc3bb30145b1a6697fccfe755a6",
"is_secret": false,
"is_verified": false,
"line_number": 589,
"line_number": 588,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down
11 changes: 6 additions & 5 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,21 @@ def __init__(
self.http_client.mount('http://', self.http_adapter)
self.http_client.mount('https://', self.http_adapter)

def enable_retries(self, max_retries: int = 4, retry_interval: float = 1.0) -> None:
def enable_retries(self, max_retries: int = 4, retry_interval: float = 30.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.
retry_interval: the maximum wait time (in seconds) to use for retry attempts.
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
present, then the wait time is based on an exponential backoff policy with a maximum
backoff time of "retry_interval".
"""
self.retry_config = Retry(
total=max_retries,
backoff_factor=retry_interval,
backoff_factor=1.0,
backoff_max=retry_interval,
# List of HTTP status codes to retry on in addition to Timeout/Connection Errors
status_forcelist=[429, 500, 502, 503, 504],
# List of HTTP methods to retry on
Expand Down
2 changes: 1 addition & 1 deletion resources/ibm-credentials-retry.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ INCLUDE_EXTERNAL_CONFIG_APIKEY=mockkey
INCLUDE_EXTERNAL_CONFIG_AUTH_TYPE=iam
INCLUDE_EXTERNAL_CONFIG_URL=https://mockurl
INCLUDE_EXTERNAL_CONFIG_MAX_RETRIES=3
INCLUDE_EXTERNAL_CONFIG_RETRY_INTERVAL=0.2
INCLUDE_EXTERNAL_CONFIG_RETRY_INTERVAL=25.0
INCLUDE_EXTERNAL_CONFIG_ENABLE_RETRIES=true
9 changes: 6 additions & 3 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ def test_retry_config_default():
service.enable_retries()
assert service.retry_config.total == 4
assert service.retry_config.backoff_factor == 1.0
assert service.retry_config.backoff_max == 30.0
assert service.http_client.get_adapter('https://').max_retries.total == 4

# Ensure retries fail after 4 retries
Expand Down Expand Up @@ -756,9 +757,10 @@ def test_retry_config_disable():

def test_retry_config_non_default():
service = BaseService(service_url='https://mockurl/', authenticator=NoAuthAuthenticator())
service.enable_retries(2, 0.3)
service.enable_retries(2, 10.0)
assert service.retry_config.total == 2
assert service.retry_config.backoff_factor == 0.3
assert service.retry_config.backoff_factor == 1.0
assert service.retry_config.backoff_max == 10.0

# Ensure retries fail after 2 retries
error = ConnectTimeoutError()
Expand All @@ -775,7 +777,8 @@ def test_retry_config_external():
os.environ['IBM_CREDENTIALS_FILE'] = file_path
service = IncludeExternalConfigService('v1', authenticator=NoAuthAuthenticator())
assert service.retry_config.total == 3
assert service.retry_config.backoff_factor == 0.2
assert service.retry_config.backoff_factor == 1.0
assert service.retry_config.backoff_max == 25.0

# Ensure retries fail after 3 retries
error = ConnectTimeoutError()
Expand Down