Skip to content

Max subscriptions semaphore added #107

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
merged 1 commit into from
May 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def __init__(
# TODO reconnection logic
self.ws_url = ws_url
self.ws: Optional["ClientConnection"] = None
self.max_subscriptions = max_subscriptions
self.max_subscriptions = asyncio.Semaphore(max_subscriptions)
self.max_connections = max_connections
self.shutdown_timer = shutdown_timer
self._received = {}
Expand Down Expand Up @@ -631,6 +631,7 @@ async def send(self, payload: dict) -> int:
# async with self._lock:
original_id = get_next_id()
# self._open_subscriptions += 1
await self.max_subscriptions.acquire()
try:
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
return original_id
Expand All @@ -649,7 +650,9 @@ async def retrieve(self, item_id: int) -> Optional[dict]:
retrieved item
"""
try:
return self._received.pop(item_id)
item = self._received.pop(item_id)
self.max_subscriptions.release()
return item
except KeyError:
await asyncio.sleep(0.001)
return None
Expand Down Expand Up @@ -876,7 +879,7 @@ async def decode_scale(
scale_bytes: bytes,
_attempt=1,
_retries=3,
return_scale_obj=False,
return_scale_obj: bool = False,
) -> Union[ScaleObj, Any]:
"""
Helper function to decode arbitrary SCALE-bytes (e.g. 0x02000000) according to given RUST type_string
Expand Down Expand Up @@ -2528,13 +2531,13 @@ async def runtime_call(
Returns:
ScaleType from the runtime call
"""
await self.init_runtime(block_hash=block_hash)
runtime = await self.init_runtime(block_hash=block_hash)

if params is None:
params = {}

try:
metadata_v15_value = self.runtime.metadata_v15.value()
metadata_v15_value = runtime.metadata_v15.value()

apis = {entry["name"]: entry for entry in metadata_v15_value["apis"]}
api_entry = apis[api]
Expand Down
Loading