Skip to content

[RFQ] Add maxRetrySeconds configuration and RetryTooLong exception #307

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions zscaler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@


from zscaler.oneapi_client import Client as ZscalerClient # noqa
from zscaler.exceptions.exceptions import RetryTooLong # noqa
3 changes: 2 additions & 1 deletion zscaler/config/config_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ConfigSetter:
"proxy": {"port": "", "host": "", "username": "", "password": ""},
"rateLimit": {
"maxRetries": 2,
"maxRetrySeconds": 0,
},
"testing": {"disableHttpsCheck": ""},
}
Expand Down Expand Up @@ -136,7 +137,7 @@ def _apply_default_values(self):

self._config["client"]["userAgent"] = ""
self._config["client"]["requestTimeout"] = 0
self._config["client"]["rateLimit"] = {"maxRetries": 2}
self._config["client"]["rateLimit"] = {"maxRetries": 2, "maxRetrySeconds": 0}

# Add a check for the 'testing' key before accessing it
if "testing" not in self._config:
Expand Down
7 changes: 7 additions & 0 deletions zscaler/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ class TokenRefreshError(Exception):

class HeaderUpdateError(Exception):
pass


class RetryTooLong(Exception):
def __init__(self, retry_seconds, max_seconds):
self.retry_seconds = retry_seconds
self.max_seconds = max_seconds
super().__init__(f"Retry time of {retry_seconds} seconds exceeds maximum allowed retry time of {max_seconds} seconds")
11 changes: 11 additions & 0 deletions zscaler/request_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def __init__(
if self._max_retries < 0:
raise ValueError(f"Invalid max retries: {self._max_retries}. Must be 0 or greater.")

# Validate and set max retry seconds for rate limiting
self._max_retry_seconds = config["client"]["rateLimit"].get("maxRetrySeconds", 0)
if self._max_retry_seconds < 0:
raise ValueError(f"Invalid max retry seconds: {self._max_retry_seconds}. Must be 0 or greater.")

# Set configuration and cache
self._config = config
self._cache = cache
Expand Down Expand Up @@ -500,6 +505,12 @@ def fire_request_helper(self, request, attempts, request_start_time):
if backoff_seconds is None:
return None, response, response.text, Exception(ERROR_MESSAGE_429_MISSING_DATE_X_RESET)

# Check if backoff time exceeds maximum retry seconds limit
if self._max_retry_seconds > 0 and backoff_seconds > self._max_retry_seconds:
logger.warning(f"Retry time of {backoff_seconds} seconds exceeds maximum allowed retry time of {self._max_retry_seconds} seconds.")
from zscaler.exceptions.exceptions import RetryTooLong
return request, response, response.text, RetryTooLong(backoff_seconds, self._max_retry_seconds)

logger.info(f"Hit rate limit or retryable status {response.status_code}. "
f"Retrying request in {backoff_seconds} seconds.")
time.sleep(backoff_seconds)
Expand Down