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:
@@ -1,11 +1,13 @@
|
||||
import postgres from 'postgres';
|
||||
import { ObjectMediaStore, S3Bucket } from '@shade/media/server';
|
||||
import type { LivePinStore, MediaStore } from '@shade/media/server';
|
||||
import { PostgresLivePinStore, PostgresMediaIndex } from './postgres-media-store.js';
|
||||
import type { LivePinStore, LivePublisherLocator, MediaStore } from '@shade/media/server';
|
||||
import { PostgresLivePinStore, PostgresLivePublisherLocator, PostgresMediaIndex } from './postgres-media-store.js';
|
||||
|
||||
export interface SharedMediaBackend {
|
||||
store: MediaStore;
|
||||
pins: LivePinStore;
|
||||
/** Where each live channel's publisher is, for relays that follow one another. */
|
||||
locator: LivePublisherLocator;
|
||||
/** Where the bytes go, for logs — no credentials. */
|
||||
describe: string;
|
||||
/** Reclaim unreferenced objects and dead uploads; safe on every instance at once. */
|
||||
@@ -26,6 +28,7 @@ export async function createSharedMediaBackend(opts: { s3Url: string; pgUrl: str
|
||||
try {
|
||||
const index = await PostgresMediaIndex.fromClient(sql);
|
||||
const pins = await PostgresLivePinStore.fromClient(sql);
|
||||
const locator = await PostgresLivePublisherLocator.fromClient(sql);
|
||||
const store = new ObjectMediaStore(bucket, index, { prefix });
|
||||
// Fail at start, not at the first upload: a bucket that is not there, or
|
||||
// credentials it refuses, show up here.
|
||||
@@ -33,6 +36,7 @@ export async function createSharedMediaBackend(opts: { s3Url: string; pgUrl: str
|
||||
return {
|
||||
store,
|
||||
pins,
|
||||
locator,
|
||||
describe: `s3 ${new URL(opts.s3Url).host}/${bucket.bucket}/${prefix}`,
|
||||
sweep: () => store.sweep(),
|
||||
close: () => sql.end(),
|
||||
|
||||
Reference in New Issue
Block a user