Skip to content

Commit ae89f09

Browse files
authored
Merge pull request #2553 from opentensor/feat/roman/hadle-the-maximun-connection-limit
[SDK] Handle server connection limit
2 parents 935c81e + bd368df commit ae89f09

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

bittensor/core/subtensor.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from scalecodec.type_registry import load_type_registry_preset
1818
from scalecodec.types import ScaleType
1919
from substrateinterface.base import QueryMapResult, SubstrateInterface
20+
from websockets.exceptions import InvalidStatus
2021
from websockets.sync import client as ws_client
2122

2223
from bittensor.core import settings
@@ -233,6 +234,7 @@ def _get_substrate(self, force: bool = False):
233234
open_timeout=self._connection_timeout,
234235
max_size=2**32,
235236
)
237+
236238
self.substrate = SubstrateInterface(
237239
ss58_format=settings.SS58_FORMAT,
238240
use_remote_preset=True,
@@ -244,19 +246,26 @@ def _get_substrate(self, force: bool = False):
244246
f"Connected to {self.network} network and {self.chain_endpoint}."
245247
)
246248

247-
except (ConnectionRefusedError, ssl.SSLError) as error:
248-
logging.error(
249-
f"<red>Could not connect to</red> <blue>{self.network}</blue> <red>network with</red> <blue>{self.chain_endpoint}</blue> <red>chain endpoint.</red>",
249+
except ConnectionRefusedError as error:
250+
logging.critical(
251+
f"[red]Could not connect to[/red] [blue]{self.network}[/blue] [red]network with[/red] [blue]{self.chain_endpoint}[/blue] [red]chain endpoint.[/red]",
250252
)
251253
raise ConnectionRefusedError(error.args)
252-
except ssl.SSLError as e:
254+
255+
except ssl.SSLError as error:
253256
logging.critical(
254257
"SSL error occurred. To resolve this issue, run the following command in your terminal:"
255258
)
256259
logging.critical("[blue]sudo python -m bittensor certifi[/blue]")
257260
raise RuntimeError(
258261
"SSL configuration issue, please follow the instructions above."
259-
) from e
262+
) from error
263+
264+
except InvalidStatus as error:
265+
logging.critical(
266+
f"[red]You have reached the limit of simultaneous connections to the server.[/red]"
267+
)
268+
raise InvalidStatus(error.response) from error
260269

261270
@staticmethod
262271
def config() -> "Config":

tests/unit_tests/test_subtensor.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,3 +2880,27 @@ def test_set_weights_with_commit_reveal_enabled(subtensor, mocker):
28802880
wait_for_finalization=fake_wait_for_finalization,
28812881
)
28822882
assert result == mocked_commit_reveal_v3_extrinsic.return_value
2883+
2884+
2885+
def test_connection_limit(mocker):
2886+
"""Test connection limit is not exceeded."""
2887+
# Technically speaking, this test should exist in integration tests. But to reduce server costs we will leave this
2888+
# test here.
2889+
2890+
# Preps
2891+
mocker.patch.object(
2892+
subtensor_module.ws_client,
2893+
"connect",
2894+
side_effect=subtensor_module.InvalidStatus(
2895+
response=mocker.Mock(
2896+
response=mocker.Mock(
2897+
status_code=429, message="test connection limit error"
2898+
)
2899+
)
2900+
),
2901+
)
2902+
# Call with assertions
2903+
2904+
with pytest.raises(subtensor_module.InvalidStatus):
2905+
for i in range(2):
2906+
Subtensor("test")

0 commit comments

Comments
 (0)