Skip to content
This repository has been archived by the owner on Mar 18, 2023. It is now read-only.

Commit

Permalink
docs(connection_manager): #13 document connection manager
Browse files Browse the repository at this point in the history
  • Loading branch information
miragecentury committed Feb 9, 2022
1 parent 329a725 commit e577e89
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/application/service/device/connection_manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit e577e89

Please sign in to comment.