Skip to content

Commit 7ffbff9

Browse files
authored
Merge pull request #107 from opentensor/feat/thewhaleking/max-connections
2 parents b771d7f + c3f6006 commit 7ffbff9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def __init__(
517517
# TODO reconnection logic
518518
self.ws_url = ws_url
519519
self.ws: Optional["ClientConnection"] = None
520-
self.max_subscriptions = max_subscriptions
520+
self.max_subscriptions = asyncio.Semaphore(max_subscriptions)
521521
self.max_connections = max_connections
522522
self.shutdown_timer = shutdown_timer
523523
self._received = {}
@@ -632,6 +632,7 @@ async def send(self, payload: dict) -> int:
632632
# async with self._lock:
633633
original_id = get_next_id()
634634
# self._open_subscriptions += 1
635+
await self.max_subscriptions.acquire()
635636
try:
636637
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
637638
return original_id
@@ -650,7 +651,9 @@ async def retrieve(self, item_id: int) -> Optional[dict]:
650651
retrieved item
651652
"""
652653
try:
653-
return self._received.pop(item_id)
654+
item = self._received.pop(item_id)
655+
self.max_subscriptions.release()
656+
return item
654657
except KeyError:
655658
await asyncio.sleep(0.001)
656659
return None
@@ -877,7 +880,7 @@ async def decode_scale(
877880
scale_bytes: bytes,
878881
_attempt=1,
879882
_retries=3,
880-
return_scale_obj=False,
883+
return_scale_obj: bool = False,
881884
) -> Union[ScaleObj, Any]:
882885
"""
883886
Helper function to decode arbitrary SCALE-bytes (e.g. 0x02000000) according to given RUST type_string
@@ -2764,13 +2767,13 @@ async def runtime_call(
27642767
Returns:
27652768
ScaleType from the runtime call
27662769
"""
2767-
await self.init_runtime(block_hash=block_hash)
2770+
runtime = await self.init_runtime(block_hash=block_hash)
27682771

27692772
if params is None:
27702773
params = {}
27712774

27722775
try:
2773-
metadata_v15_value = self.runtime.metadata_v15.value()
2776+
metadata_v15_value = runtime.metadata_v15.value()
27742777

27752778
apis = {entry["name"]: entry for entry in metadata_v15_value["apis"]}
27762779
api_entry = apis[api]

0 commit comments

Comments
 (0)