From e577e89b081b1e0b666a22699febe9cfcf612c8f Mon Sep 17 00:00:00 2001 From: Victorien VANROYE Date: Thu, 18 Nov 2021 22:30:47 +0100 Subject: [PATCH] docs(connection_manager): #13 document connection manager --- .../service/device/connection_manager.py | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/application/service/device/connection_manager.py b/src/application/service/device/connection_manager.py index 2526c3e..f375007 100644 --- a/src/application/service/device/connection_manager.py +++ b/src/application/service/device/connection_manager.py @@ -1,10 +1,12 @@ from fastapi import WebSocket from typing import Dict -from asyncio import gather +import logging from application.cache import Cache +logger = logging.getLogger(__package__) + class ConnectionManager: """ Connection Manager is responsible to manage and store reference of device's connection @@ -38,22 +40,34 @@ async def unregister_application(self) -> None: # TODO : How manage the crash of application ? (liveness probing) pass - def _get_device_key(self, device_id: str): + def _get_device_key(self, device_id: str) -> str: """ - Format Device Key for Redis + Format Device Key for Redis. + + Returns: + String representing the Key in Redis for a device. """ return f"{self.PREFIX_KEY_DEVICE}{device_id}" - async def register_connection(self, websocket: WebSocket, device_id: str): + async def register_connection(self, websocket: WebSocket, device_id: str) -> bool: """ - Register Connection for Device Connected to this specific instance + Register Connection for Device Connected to this specific instance. + + Returns: + Success Execution """ - async with self.cache.acquire() as _cache_connection: - await _cache_connection.set( - key=self._get_device_key(device_id=device_id), - value=self.application_instance_name - ) - self.local_device_store[device_id] = websocket + try: + async with self.cache.acquire() as _cache_connection: + await _cache_connection.set( + key=self._get_device_key(device_id=device_id), + value=self.application_instance_name + ) + self.local_device_store[device_id] = websocket + except RuntimeError as e: + logger.exception(e) + return False + else: + return True async def unregister_connection(self, websocket: WebSocket, device_id: str): """