-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add custom list and backoff factor to http_retry_middleware #3120
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
Merged
fselmo
merged 1 commit into
ethereum:main
from
fselmo:exception-retry-middleware-updates
Oct 10, 2023
Merged
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add ``allow_list`` kwarg for ``exception_retry_middleware`` to allow for a custom list of RPC endpoints. Add a sleep between retries and a customizable ``backoff_factor`` to control the sleep time between retry attempts. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import asyncio | ||
| import time | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
| Any, | ||
| Callable, | ||
| Collection, | ||
| List, | ||
| Optional, | ||
| Type, | ||
| ) | ||
|
|
@@ -28,7 +30,7 @@ | |
| Web3, | ||
| ) | ||
|
|
||
| whitelist = [ | ||
| DEFAULT_ALLOWLIST = [ | ||
| "admin", | ||
| "miner", | ||
| "net", | ||
|
|
@@ -87,11 +89,16 @@ | |
| ] | ||
|
|
||
|
|
||
| def check_if_retry_on_failure(method: RPCEndpoint) -> bool: | ||
| def check_if_retry_on_failure( | ||
| method: str, allow_list: Optional[List[str]] = None | ||
| ) -> bool: | ||
| if allow_list is None: | ||
| allow_list = DEFAULT_ALLOWLIST | ||
|
|
||
| root = method.split("_")[0] | ||
| if root in whitelist: | ||
| if root in allow_list: | ||
|
Contributor
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. 💯 |
||
| return True | ||
| elif method in whitelist: | ||
| elif method in allow_list: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
@@ -102,19 +109,22 @@ def exception_retry_middleware( | |
| _w3: "Web3", | ||
| errors: Collection[Type[BaseException]], | ||
| retries: int = 5, | ||
| backoff_factor: float = 0.3, | ||
| allow_list: Optional[List[str]] = None, | ||
| ) -> Callable[[RPCEndpoint, Any], RPCResponse]: | ||
| """ | ||
| Creates middleware that retries failed HTTP requests. Is a default | ||
| middleware for HTTPProvider. | ||
| """ | ||
|
|
||
| def middleware(method: RPCEndpoint, params: Any) -> Optional[RPCResponse]: | ||
| if check_if_retry_on_failure(method): | ||
| if check_if_retry_on_failure(method, allow_list): | ||
| for i in range(retries): | ||
| try: | ||
| return make_request(method, params) | ||
| except tuple(errors): | ||
| if i < retries - 1: | ||
| time.sleep(backoff_factor) | ||
| continue | ||
| else: | ||
| raise | ||
|
|
@@ -133,20 +143,24 @@ def http_retry_request_middleware( | |
| ) | ||
|
|
||
|
|
||
| # -- async -- # | ||
|
|
||
|
|
||
| async def async_exception_retry_middleware( | ||
| make_request: Callable[[RPCEndpoint, Any], Any], | ||
| _async_w3: "AsyncWeb3", | ||
| errors: Collection[Type[BaseException]], | ||
| retries: int = 5, | ||
| backoff_factor: float = 0.3, | ||
| allow_list: Optional[List[str]] = None, | ||
| ) -> AsyncMiddlewareCoroutine: | ||
| """ | ||
| Creates middleware that retries failed HTTP requests. | ||
| Is a default middleware for AsyncHTTPProvider. | ||
| """ | ||
|
|
||
| async def middleware(method: RPCEndpoint, params: Any) -> Optional[RPCResponse]: | ||
| if check_if_retry_on_failure(method): | ||
| if check_if_retry_on_failure(method, allow_list): | ||
| for i in range(retries): | ||
| try: | ||
| return await make_request(method, params) | ||
|
|
||
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.