Skip to content

Commit 311b706

Browse files
committed
Internalize the max connection retries for now
- Due to the exponential backoff, it is already pretty reasonable to have this number at 5 but more than this might add long wait times. If there is demand, we can open this up to an init arg in the future.
1 parent ca8985f commit 311b706

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

web3/providers/websocket/websocket_v2.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,18 @@ def get_default_endpoint() -> URI:
5252
class WebsocketProviderV2(PersistentConnectionProvider):
5353
logger = logging.getLogger("web3.providers.WebsocketProviderV2")
5454
is_async: bool = True
55+
_max_connection_retries: int = 5
5556

5657
def __init__(
5758
self,
5859
endpoint_uri: Optional[Union[URI, str]] = None,
5960
websocket_kwargs: Optional[Dict[str, Any]] = None,
6061
call_timeout: Optional[int] = None,
61-
max_connection_retries: Optional[int] = 5,
6262
) -> None:
6363
self.endpoint_uri = URI(endpoint_uri)
6464
if self.endpoint_uri is None:
6565
self.endpoint_uri = get_default_endpoint()
6666

67-
self.max_connection_retries = max_connection_retries
68-
6967
if not any(
7068
self.endpoint_uri.startswith(prefix)
7169
for prefix in VALID_WEBSOCKET_URI_PREFIXES
@@ -112,19 +110,15 @@ async def connect(self) -> None:
112110
_backoff_rate_change = 1.75
113111
_backoff_time = 1.75
114112

115-
while True:
113+
while _connection_attempts != self._max_connection_retries:
116114
try:
117115
_connection_attempts += 1
118116
self.ws = await connect(self.endpoint_uri, **self.websocket_kwargs)
119-
break
120117
except WebSocketException as e:
121-
if (
122-
self.max_connection_retries
123-
and _connection_attempts > self.max_connection_retries
124-
):
118+
if _connection_attempts == self._max_connection_retries:
125119
raise ProviderConnectionError(
126120
f"Could not connect to endpoint: {self.endpoint_uri}. "
127-
f"Retries exceeded max of {self.max_connection_retries}."
121+
f"Retries exceeded max of {self._max_connection_retries}."
128122
) from e
129123
self.logger.info(
130124
f"Could not connect to endpoint: {self.endpoint_uri}. Retrying in "

0 commit comments

Comments
 (0)