feat(media): live channels across relay instances; viewers hear when a channel goes live
With several relays sharing pins, a viewer and the publisher could land on
different instances and never meet. LiveRelayOptions.mesh: each relay
claims the channels whose publisher it holds (LivePublisherLocator,
Postgres table shade_live_publishers, heartbeat 10 s). A relay whose
viewers want a channel published elsewhere subscribes to that relay once,
with a viewer's own read token, and fans out locally, verifying every
frame against the pin (a peer or a poisoned locator row cannot forge
one). Re-following replays a backlog; frames are passed on once (seq).
Viewers follow a publisher that moves. A token that fails the in-memory
pin is checked against the shared pin before refusal, so a publisher
back with a new read token is honoured on every instance.
SHADE_MEDIA_RELAY_URL / `media serve --advertise`.
Relays now send {"type":"state","live"} when a channel goes live or quiet
after a viewer joined; handled by the TS client (browser) and Android's
LiveWatcher.
Fixed on the way:
- The relay never saw its publisher leave: Hono gives each event its own
WSContext and the close handler compared identity. Channels stayed
"live" with nobody publishing; a replaced publisher was never closed.
- FileMediaStore on an unusable directory raised an unhandled rejection
at start; now each call fails with the reason.
Verified: 5 mesh tests (follow, late join, publisher moving A→B with
dedupe, forging peer refused, new read token) + state test, each fix
mutation-checked; Postgres locator + two media services over Postgres;
CLI relays with --advertise behind a round-robin HTTP+WS balancer:
publisher on A, Chromium viewer on B, "live", 208 lines. Suites: media
93, storage-postgres 20, server 55; Kotlin 92 (1 skipped), 0 failures.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -90,9 +90,10 @@ With `SHADE_MEDIA_OWNERS` empty, the relay serves what it has and accepts nothin
|
||||
```
|
||||
SHADE_MEDIA_S3_URL=https://<key>:<secret>@s3.eu-north-1.amazonaws.com/<bucket>[/<prefix>]
|
||||
SHADE_MEDIA_PG_URL=postgres://… # falls back to SHADE_PREKEY_PG_URL
|
||||
SHADE_MEDIA_RELAY_URL=http://10.0.0.5:3000 # this instance, as the others reach it (live channels)
|
||||
```
|
||||
|
||||
(`shade media serve --s3 … --pg …` does the same outside the container.) Sealed
|
||||
(`shade media serve --s3 … --pg … --advertise URL` does the same outside the container.) Sealed
|
||||
bytes go to the bucket, records and live-channel pins to Postgres, and any number of
|
||||
instances behind one hostname serve one library. Any S3 API works (AWS, R2, MinIO,
|
||||
SeaweedFS, Garage, B2); credentials left out of the URL come from
|
||||
@@ -110,8 +111,21 @@ off and the rest of the server runs.
|
||||
(a crash mid-upload) are reclaimed after 24 hours, a minute after start and every
|
||||
6 hours. Every instance may sweep at once.
|
||||
- **Live channels** are pinned in Postgres, so a channel cannot be pinned to two keys
|
||||
by two instances. Frames still fan out in the memory of the instance the publisher
|
||||
is connected to: route `/v1/live/*` to one instance (or sticky by channel id).
|
||||
by two instances. Each instance records which channels' publishers are connected to
|
||||
it (`shade_live_publishers`, refreshed every 10 s). An instance whose viewers want a
|
||||
channel published elsewhere subscribes to that instance once, with a viewer's own
|
||||
read token, and fans the frames out locally. It verifies every frame against the pin
|
||||
as if the publisher were its own, so a peer, or a poisoned locator row, cannot forge
|
||||
one. Following an instance again replays its backlog; frames already passed on (by
|
||||
`seq`) are not passed on twice. If the publisher moves (a deploy), viewers on every
|
||||
instance follow it within a poll (2 s). What is sent while they look for it reaches
|
||||
them the way a late joiner gets it, from a keyframe. A token that does not match the
|
||||
pin held in memory is checked against the shared pin before it is refused, so a
|
||||
publisher that comes back with a new read token is honoured everywhere. Without
|
||||
`SHADE_MEDIA_RELAY_URL` an instance cannot be followed, and `/v1/live/*` must go to
|
||||
one instance.
|
||||
- Viewers now hear when a channel goes live or quiet after they joined: a
|
||||
`{"type":"state","live":…}` message, in the browser, the TS client and Android.
|
||||
- The bucket holds only sealed SMF1 bytes. No key, name or plaintext is in it or in
|
||||
Postgres.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user