Skip to content

Commit b8c1f82

Browse files
Haoran Wangfselmo
authored andcommitted
Implement exponential backoff logic for request retries for http providers.
- update docs - decrease default backoff - fix lint - add newsfragment
1 parent 87348bd commit b8c1f82

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

docs/internals.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ Retry Requests for HTTP Providers
146146

147147
``HTTPProvider`` and ``AsyncHTTPProvider`` instances retry certain requests by default
148148
on exceptions. This can be configured via configuration options on the provider
149-
instance. Below is an example showing the default options for the retry configuration
150-
and how to override them.
149+
instance. The retry mechanism employs an exponential backoff strategy, starting from
150+
the initial value determined by the ``backoff_factor``, and doubling the delay with
151+
each attempt, up to the ``retries`` value. Below is an example showing the default
152+
options for the retry configuration and how to override them.
151153

152154
.. code-block:: python
153155
@@ -165,8 +167,8 @@ and how to override them.
165167
# number of retries to attempt
166168
retries=5,
167169
168-
# how long to wait between retries
169-
backoff_factor=0.5,
170+
# initial delay multiplier, doubles with each retry attempt
171+
backoff_factor=0.125,
170172
171173
# an in-house default list of retryable methods
172174
method_allowlist=REQUEST_RETRY_ALLOWLIST,

newsfragments/3358.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Employ an exponential backoff strategy using the ``backoff_factor`` in ``ExceptionRetryConfiguration`` for ``HTTPProvider`` and ``AsyncHTTPProvider``. Reduce the default initial delay to ``0.125`` seconds.

web3/providers/rpc/async_rpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async def _make_request(self, method: RPCEndpoint, request_data: bytes) -> bytes
123123
except tuple(self.exception_retry_configuration.errors):
124124
if i < self.exception_retry_configuration.retries - 1:
125125
await asyncio.sleep(
126-
self.exception_retry_configuration.backoff_factor
126+
self.exception_retry_configuration.backoff_factor * 2**i
127127
)
128128
continue
129129
else:

web3/providers/rpc/rpc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def _make_request(self, method: RPCEndpoint, request_data: bytes) -> bytes:
130130
)
131131
except tuple(self.exception_retry_configuration.errors) as e:
132132
if i < self.exception_retry_configuration.retries - 1:
133-
time.sleep(self.exception_retry_configuration.backoff_factor)
133+
time.sleep(
134+
self.exception_retry_configuration.backoff_factor * 2**i
135+
)
134136
continue
135137
else:
136138
raise e

web3/providers/rpc/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(
8484
self,
8585
errors: Sequence[Type[BaseException]] = None,
8686
retries: int = 5,
87-
backoff_factor: float = 0.5,
87+
backoff_factor: float = 0.125,
8888
method_allowlist: Sequence[str] = None,
8989
):
9090
super().__init__(

0 commit comments

Comments
 (0)