Private
Public Access
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>
57 lines
2.5 KiB
Python
57 lines
2.5 KiB
Python
import uuid
|
|
|
|
from redis.asyncio import Redis
|
|
|
|
from app.ws.expiring_presence import ExpiringPresenceSet
|
|
|
|
|
|
class GlobalPresence:
|
|
"""Cross-instance "does this user have the app open at all right now,"
|
|
independent of which (if any) room they currently have open -- backing
|
|
the online/offline presence dot shown wherever a user's avatar renders.
|
|
|
|
Each connection has its own expiring entry (see ExpiringPresenceSet), so
|
|
a user with several tabs/devices stays online until the last one goes,
|
|
and a connection that dies without cleaning up ages out instead of
|
|
leaving them online forever (#77). The key is versioned (`v2`) because
|
|
the old refcount hash lives at `presence:global` and its leaked counts
|
|
must not be read as live entries.
|
|
"""
|
|
|
|
KEY = "presence:v2:global"
|
|
|
|
def __init__(self, redis: Redis, store: ExpiringPresenceSet | None = None) -> None:
|
|
self._store = store or ExpiringPresenceSet(redis)
|
|
|
|
async def connect(self, user_id: uuid.UUID, conn_id: str) -> bool:
|
|
"""Returns True iff this made the user newly online -- a genuine
|
|
offline->online transition worth telling anyone about."""
|
|
return await self._store.add(self.KEY, user_id, conn_id)
|
|
|
|
async def refresh(self, user_id: uuid.UUID, conn_id: str) -> bool:
|
|
"""Heartbeat. Same as connect() on purpose: a connection whose entry
|
|
had already expired (a long stall) comes back online here, and the
|
|
caller needs to announce that exactly like a fresh connect."""
|
|
return await self._store.add(self.KEY, user_id, conn_id)
|
|
|
|
async def disconnect(self, user_id: uuid.UUID, conn_id: str) -> bool:
|
|
"""Returns True iff that was the user's last live connection -- a
|
|
genuine online->offline transition."""
|
|
return await self._store.remove(self.KEY, user_id, conn_id)
|
|
|
|
async def sweep(self) -> set[uuid.UUID]:
|
|
"""Drops expired entries and returns the users that went offline as a
|
|
result -- the transitions no disconnect ever reported."""
|
|
return await self._store.prune(self.KEY)
|
|
|
|
async def is_online(self, user_id: uuid.UUID) -> bool:
|
|
return await self._store.is_live(self.KEY, user_id)
|
|
|
|
async def online_user_ids(self, user_ids: list[uuid.UUID]) -> set[uuid.UUID]:
|
|
if not user_ids:
|
|
return set()
|
|
return set(user_ids) & await self._store.live_user_ids(self.KEY)
|
|
|
|
async def all_online_user_ids(self) -> set[uuid.UUID]:
|
|
return await self._store.live_user_ids(self.KEY)
|