Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion lambda/src/services/channel_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Channel Service — WebSocket message relay for device pairing."""

import json
import logging
import time
import uuid

from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)

MAX_CONNECTIONS_PER_CHANNEL = 3
MAX_MESSAGES_PER_CHANNEL = 10
CHANNEL_TTL_SECONDS = 300
Expand All @@ -28,6 +31,7 @@ def handle(self, event, context):
"""Dispatch on WebSocket route key."""
route_key = event["requestContext"]["routeKey"]
connection_id = event["requestContext"]["connectionId"]
logger.info("route=%s connection=%s", route_key, connection_id)

if route_key == "$connect":
return self._handle_connect(event, connection_id)
Expand All @@ -53,6 +57,7 @@ def _handle_connect(self, event, connection_id):
def _create_channel(self, event, connection_id, expiry):
"""Create a new channel with this connection as the first member."""
channel_id = str(uuid.uuid4())
logger.info("Creating channel=%s for connection=%s", channel_id, connection_id)

# Put channel metadata
self._table.put_item(
Expand All @@ -77,13 +82,14 @@ def _create_channel(self, event, connection_id, expiry):
self._post_to_connection(
event,
connection_id,
json.dumps({"channelId": channel_id}),
json.dumps({"channelid": channel_id}),
)

return {"statusCode": 200}

def _join_channel(self, channel_id, connection_id, expiry):
"""Join an existing channel atomically."""
logger.info("Joining channel=%s connection=%s", channel_id, connection_id)
try:
self._table.update_item(
Key={"PK": f"CHANNEL#{channel_id}"},
Expand Down Expand Up @@ -140,6 +146,7 @@ def _handle_disconnect(self, connection_id):

def _handle_message(self, event, connection_id):
"""Handle incoming message — relay to other connections."""
logger.info("Message from connection=%s", connection_id)
# Look up channel for this connection
result = self._table.get_item(Key={"PK": f"CONN#{connection_id}"})
if "Item" not in result:
Expand Down Expand Up @@ -201,6 +208,7 @@ def _post_to_connection(self, event, connection_id, data):
Data=data.encode("utf-8") if isinstance(data, str) else data,
)
except client.exceptions.GoneException:
logger.warning("Connection %s is gone, cleaning up", connection_id)
self._handle_disconnect(connection_id)

def _get_apigw_client(self, event):
Expand Down
2 changes: 1 addition & 1 deletion lambda/tests/services/test_channel_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_create_channel(
{},
{
"ConnectionId": "conn-1",
"Data": json.dumps({"channelId": FIXED_UUID}).encode("utf-8"),
"Data": json.dumps({"channelid": FIXED_UUID}).encode("utf-8"),
},
)

Expand Down
Loading