-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ENH]: Client side retries #5419
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
sanketkedia
wants to merge
1
commit into
09-02-_enh_consolidate_retries
Choose a base branch
from
09-04-_enh_client_side_retries
base: 09-02-_enh_consolidate_retries
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,14 @@ | |
import httpx | ||
import urllib.parse | ||
from overrides import override | ||
from tenacity import ( | ||
retry, | ||
stop_after_attempt, | ||
wait_exponential, | ||
retry_if_exception_type, | ||
before_sleep_log, | ||
RetryError | ||
) | ||
|
||
from chromadb.api.collection_configuration import ( | ||
CreateCollectionConfiguration, | ||
|
@@ -58,6 +66,23 @@ | |
|
||
logger = logging.getLogger(__name__) | ||
|
||
def is_retryable_exception(exception: BaseException) -> bool: | ||
if isinstance(exception, ( | ||
httpx.ConnectError, | ||
httpx.ConnectTimeout, | ||
httpx.ReadTimeout, | ||
httpx.WriteTimeout, | ||
httpx.PoolTimeout, | ||
httpx.NetworkError, | ||
httpx.RemoteProtocolError, | ||
)): | ||
return True | ||
|
||
if isinstance(exception, httpx.HTTPStatusError): | ||
# Retry on server errors that might be temporary | ||
return exception.response.status_code in [502, 503, 504] | ||
|
||
return False | ||
|
||
class FastAPI(BaseHTTPClient, ServerAPI): | ||
def __init__(self, system: System): | ||
|
@@ -99,20 +124,38 @@ def __init__(self, system: System): | |
self._session.headers[header] = value.get_secret_value() | ||
|
||
def _make_request(self, method: str, path: str, **kwargs: Dict[str, Any]) -> Any: | ||
# If the request has json in kwargs, use orjson to serialize it, | ||
# remove it from kwargs, and add it to the content parameter | ||
# This is because httpx uses a slower json serializer | ||
if "json" in kwargs: | ||
data = orjson.dumps(kwargs.pop("json")) | ||
kwargs["content"] = data | ||
|
||
# Unlike requests, httpx does not automatically escape the path | ||
escaped_path = urllib.parse.quote(path, safe="/", encoding=None, errors=None) | ||
url = self._api_url + escaped_path | ||
|
||
response = self._session.request(method, url, **cast(Any, kwargs)) | ||
BaseHTTPClient._raise_chroma_error(response) | ||
return orjson.loads(response.text) | ||
@retry( | ||
stop=stop_after_attempt(3), | ||
wait=wait_exponential( | ||
multiplier=2, | ||
min=1, | ||
max=60 | ||
), | ||
retry=retry_if_exception_type(is_retryable_exception), | ||
before_sleep=before_sleep_log(logger, logging.INFO), | ||
reraise=True | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably ok for now but maybe good to make this configurable in the future? |
||
def _request_with_retry(): | ||
# If the request has json in kwargs, use orjson to serialize it, | ||
# remove it from kwargs, and add it to the content parameter | ||
# This is because httpx uses a slower json serializer | ||
if "json" in kwargs: | ||
data = orjson.dumps(kwargs.pop("json")) | ||
kwargs["content"] = data | ||
|
||
# Unlike requests, httpx does not automatically escape the path | ||
escaped_path = urllib.parse.quote(path, safe="/", encoding=None, errors=None) | ||
url = self._api_url + escaped_path | ||
|
||
response = self._session.request(method, url, **cast(Any, kwargs)) | ||
BaseHTTPClient._raise_chroma_error(response) | ||
return orjson.loads(response.text) | ||
|
||
try: | ||
return _request_with_retry() | ||
except RetryError as e: | ||
# Re-raise the last exception that caused the retry to fail | ||
raise e.last_attempt.exception() from None | ||
|
||
@trace_method("FastAPI.heartbeat", OpenTelemetryGranularity.OPERATION) | ||
@override | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[CriticalError]
The retry condition uses
retry_if_exception_type(is_retryable_exception)
butis_retryable_exception
returns a boolean, not an exception type. This should useretry_if_exception(is_retryable_exception)
instead. The current code will cause tenacity to fail when trying to match exception types.⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Context for Agents