Skip to content
Open
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
24 changes: 21 additions & 3 deletions chimerapy/engine/worker/http_client_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __init__(

# Services
self.http_client = aiohttp.ClientSession()
# Debounce
self._debounce_delay = 500 # ms
self._update_task: Optional[asyncio.Task] = None

async def async_init(self):

Expand All @@ -60,7 +63,7 @@ async def async_init(self):
),
"WorkerState.changed": TypedObserver(
"WorkerState.changed",
on_asend=self._async_node_status_update,
on_asend=self._debounced_async_node_status_update,
handle_event="drop",
),
"send_archive": TypedObserver(
Expand Down Expand Up @@ -338,8 +341,23 @@ async def _send_archive_remotely(self, host: str, port: int) -> bool:

return False

async def _async_node_status_update(self) -> bool:
async def _debounced_async_node_status_update(self) -> bool:
if not self.connected_to_manager:
return False

# Debounce
if self._update_task is not None:
self._update_task.cancel()

self._update_task = asyncio.ensure_future(self._delayed_node_status_update())

return True

async def _delayed_node_status_update(self) -> bool:
await asyncio.sleep(self._debounce_delay / 1000)
return await self._async_node_status_update()

async def _async_node_status_update(self) -> bool:
if not self.connected_to_manager:
return False

Expand All @@ -348,6 +366,6 @@ async def _async_node_status_update(self) -> bool:
self.manager_url + "/workers/node_status", data=self.state.to_json()
) as resp:
return resp.ok
except aiohttp.client_exceptions.ClientOSError:
except Exception:
self.logger.error(traceback.format_exc())
return False