Files
ds-chat/backend/app/ws/presence.py
T
ksmithandClaude Sonnet 5.5 dda85a2b78 Expire presence so users can't get stuck online (#77)
Presence was a per-user refcount in Redis, decremented only when a
connection's cleanup ran. Anything that ended a connection without
cleanup (a crash, an unclean restart, a laptop asleep with the page
frozen) leaked its count forever, so those users never showed offline.
Production had 19 counts for 2 live connections.

Presence is now one expiring entry per connection (90s TTL) for global,
per-room and focus presence, in new presence:v2:* keys. Clients renew
theirs by sending any frame, which the existing 30s ping/pong already
does. The server closes a human connection that sends nothing for 90s
(code 4408), which catches a frozen page whose browser still answers
transport pings. Bot connections can't be expected to answer pings, so
the server renews theirs itself while the handler is alive.

An expiry has no disconnect event to announce it, so a sweeper on each
worker (one per 15s overall, via a Redis lock) prunes expired entries
and broadcasts the offline transition. The old presence:* hashes are
deleted at startup.

Tests cover the expiry store, the sweeper, and the websocket cases.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 19:21:39 -06:00

42 lines
1.8 KiB
Python

import uuid
from redis.asyncio import Redis
from app.ws.expiring_presence import ExpiringPresenceSet
class Presence:
"""Cross-instance "who's connected to this room," one expiring set per
room (see ExpiringPresenceSet).
This is what the offline-push check reads, so a leaked entry used to mean
a user who'd gone away was still treated as "watching the room" and never
got a push notification (#77). Entries now expire unless the connection
keeps renewing them, so that can't outlive a dead connection by more than
the TTL. Keys are versioned (`v2`) so the old refcount hashes
(`presence:{room_id}`), with whatever they leaked, are never read.
"""
def __init__(self, redis: Redis, store: ExpiringPresenceSet | None = None) -> None:
self._store = store or ExpiringPresenceSet(redis)
def _key(self, room_id: uuid.UUID) -> str:
return f"presence:v2:room:{room_id}"
async def join(self, room_id: uuid.UUID, user_id: uuid.UUID, conn_id: str) -> None:
key = self._key(room_id)
# Per-room offline transitions aren't broadcast anywhere, so unlike
# the global key there's nothing to lose by pruning here -- it just
# keeps a busy room's set from accumulating expired entries.
await self._store.prune(key)
await self._store.add(key, user_id, conn_id)
async def refresh(self, room_id: uuid.UUID, user_id: uuid.UUID, conn_id: str) -> None:
await self._store.add(self._key(room_id), user_id, conn_id)
async def leave(self, room_id: uuid.UUID, user_id: uuid.UUID, conn_id: str) -> None:
await self._store.remove(self._key(room_id), user_id, conn_id)
async def connected_user_ids(self, room_id: uuid.UUID) -> set[uuid.UUID]:
return await self._store.live_user_ids(self._key(room_id))