-
Notifications
You must be signed in to change notification settings - Fork 708
fix: Add retry logic for RedisStorageClient and SqlStorageClient
#1838
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
Mantisus
wants to merge
8
commits into
apify:master
Choose a base branch
from
Mantisus:storages-retry
base: master
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11f9e34
Add retry logic for `KeyValueStore.set_value` and `Dataset.push_data`
Mantisus 2d6e3ad
add tests
Mantisus cd64f6f
fix
Mantisus 5f20eb4
Merge branch 'master' into storages-retry
Mantisus d09828c
move retry logic to storage clients
Mantisus c87b904
fix tests
Mantisus 20a1c0e
Update src/crawlee/_utils/retry.py
Mantisus ad941b4
lint fix
Mantisus 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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from asyncio import sleep as _retry_sleep # Using alias for testing purposes | ||
| from datetime import timedelta | ||
| from functools import wraps | ||
| from typing import TYPE_CHECKING, ParamSpec, TypeVar | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Awaitable, Callable | ||
|
|
||
| P = ParamSpec('P') | ||
| T = TypeVar('T') | ||
|
|
||
|
|
||
| def retry_on_error( | ||
| *exception_types: type[Exception], | ||
| max_attempts: int = 3, | ||
| base_delay: timedelta = timedelta(milliseconds=500), | ||
| ) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]: | ||
| """Retry an async function with exponential backoff on specified exceptions. | ||
|
|
||
| Args: | ||
| *exception_types: Exception types to catch and retry on. | ||
| max_attempts: Maximum number of attempts including the first one. | ||
| base_delay: Base delay between retries; doubles on each subsequent attempt. | ||
| """ | ||
| if max_attempts < 1: | ||
| raise ValueError('max_attempts must be at least 1') | ||
|
|
||
| if base_delay < timedelta(0): | ||
| raise ValueError('base_delay must be a non-negative timedelta') | ||
|
|
||
| if not exception_types: | ||
| raise ValueError('At least one exception type must be specified') | ||
|
|
||
| def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]: | ||
| @wraps(func) | ||
| async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: | ||
| base_delay_seconds = base_delay.total_seconds() | ||
| for attempt in range(max_attempts): | ||
| try: | ||
| return await func(*args, **kwargs) | ||
| except exception_types: # noqa: PERF203 | ||
| if attempt >= max_attempts - 1: | ||
| raise | ||
| await _retry_sleep(base_delay_seconds * (2**attempt)) | ||
| raise RuntimeError('Unreachable') | ||
|
|
||
| return wrapper | ||
|
|
||
| return decorator | ||
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.