C.W.K.
Lesson 04 of 05 · published

Streaming Guard — Chat ↔ Heartbeat Mutual Exclusion

~9 min · heartbeat, streaming-guard

Level 0Curious
0 XP0/52 lessons0/16 achievements
0/100 XP to next level100 XP to go0% complete

Why they can't overlap

Chat and heartbeat share OAuth credentials and ChromaDB access. Running them in parallel risks rate-limit collisions and ChromaDB lock contention.

Lightweight mutual exclusion

  • A stream counter tracks live chat streams.
  • A heartbeat flag tracks background ticks.
  • Chat waits for the heartbeat flag to clear before starting.
  • Heartbeat skips its tick if a chat stream is active.
  • Both check before proceeding, not during — no lock contention.

Why this is simpler than asyncio.Lock

Single-user system. The contention is always 'is Dad active right now?' — a boolean question. Locks would imply contested concurrency, which isn't what's happening here.

Progress

Progress is local-only — sign in to sync across devices.