diff --git a/bench/fanout/session/src/main.rs b/bench/fanout/session/src/main.rs index 88d07fea4f4..6c841192bd7 100644 --- a/bench/fanout/session/src/main.rs +++ b/bench/fanout/session/src/main.rs @@ -87,7 +87,8 @@ fn main() { .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .expect("the room is online"); let subscriber = remote.track("chat").unwrap().subscribe(None).await.expect("subscribe"); audience.push((sessions, local, subscriber)); } diff --git a/c/moq/MIGRATE.md b/c/moq/MIGRATE.md index 79455bdaaa3..8c738b9bda8 100644 --- a/c/moq/MIGRATE.md +++ b/c/moq/MIGRATE.md @@ -20,6 +20,14 @@ Audio and video subscribe setup now reports `MOQ_ERROR_UNSUPPORTED_CONFIG` when The hand-written crate never published as moq-c: a libmoq 0.6 user applies these entries before moving to the generated API. +### Announcements are a listing ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`moq_announce_kind` is `MOQ_ANNOUNCE_KIND_PRESENT` (0) or `MOQ_ANNOUNCE_KIND_ABSENT` (1), and `moq_announce_event` loses `predecessor` and `predecessor_len`, changing the C ABI. +Another instance taking a path over keeps it present, and a route's cost changing is not reported. +Map `_START` to `_PRESENT` and `_END` to `_ABSENT`, and drop `_UPDATE` handling. Where you requested the path again on `_RESTART` or `_TAKEOVER`, call `moq_origin_follow`, whose callback delivers a broadcast handle for each new instance. + +`moq_origin_follow` takes a `moq_follow_callback`, `(void *user_data, moq_follow_event event, int32_t code)`, instead of a `moq_status_callback`. `MOQ_FOLLOW_EVENT_ONLINE` carries each broadcast handle (where a positive code did), `MOQ_FOLLOW_EVENT_OFFLINE` reports the path going offline (or, for a follow pinned to an instance, another instance taking it over) with code `0`, and `MOQ_FOLLOW_EVENT_END` is the terminal callback, with `0` or a negative error that `moq_error()` explains (where a code `<= 0` was). Release `user_data` on `MOQ_FOLLOW_EVENT_END`. + ### Authored tracks default to a zero max age ([#7729](https://github.com/moq-dev/moq/pull/7729)) A NULL or zeroed `moq_track_info` now advertises a zero max age, serving only the live edge, instead of no publisher limit. `max_age_present` is gone and `max_age_us` always applies; set the new `max_age_unlimited`, which ignores `max_age_us`, where you relied on `max_age_present` false meaning no limit. Zero-initialize the struct (`moq_track_info info = {0};`) or assign every member. In the generated API, an unset `moq_track_info.max_age` is likewise zero, and `max_age_unlimited` sets no limit. diff --git a/cpp/moq/MIGRATE.md b/cpp/moq/MIGRATE.md index b68cbd6c346..b3a688e0dca 100644 --- a/cpp/moq/MIGRATE.md +++ b/cpp/moq/MIGRATE.md @@ -2,6 +2,10 @@ ## Unreleased +### `Following::available()` reports the path going offline ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`moq::Following::available()` resolves to a null `std::shared_ptr` once the path goes offline (or, for a follow pinned to an instance, once another instance takes it over), then the next broadcast when something serves the path again. The first call is never null. Check for null before using a later result, and treat it like the previous instance ending. + ### Separate datagram tracks Replace stream-track datagram methods with `BroadcastProducer::create_datagrams(name, info)` and `BroadcastConsumer::datagrams(name)`. The generated `DatagramProducer` exposes `append(timestamp, payload)` and `insert(Datagram)`; `DatagramConsumer::recv()` returns an optional datagram through a future. `DatagramInfo` has only priority and optional timescale. Demand is aggregate subscriber `priority()` / `priority_changed(last)`; on-demand datagram requests come from `BroadcastRequests::datagrams()` / `DatagramRequests::requested()`. Stream tracks of the same name are independent. diff --git a/cpp/moq/test/probe.cpp b/cpp/moq/test/probe.cpp index 2d7bbc1fb1c..ffea5252058 100644 --- a/cpp/moq/test/probe.cpp +++ b/cpp/moq/test/probe.cpp @@ -341,7 +341,9 @@ int main() // Subscriber: resolve the announced broadcast and subscribe to its track. auto announced = ok(session->consume()->follow("probe", std::nullopt), "follow"); + // The first available() is never offline. auto remote = ok(announced->available().get(), "available"); + CHECK(remote); auto consumer = ok(remote->subscribe_track("data", std::nullopt).get(), "subscribe_track"); // Cancel a read that has nothing to deliver yet. diff --git a/cpp/obs/src/moq-source.cpp b/cpp/obs/src/moq-source.cpp index 3669a55ab47..e656fa88d5e 100644 --- a/cpp/obs/src/moq-source.cpp +++ b/cpp/obs/src/moq-source.cpp @@ -448,7 +448,8 @@ static void moq_source_on_connect(struct moq_source *ctx, const std::shared_ptr< moq_source_next_broadcast(ctx, conn); } -// Waits for the next publisher instance: the first one announced, then each that takes over. +// Waits for the next publisher instance: the first one announced, then each that takes over +// or returns after the broadcast went offline. static void moq_source_next_broadcast(struct moq_source *ctx, const std::shared_ptr &conn) { conn->available_call = conn->announced->available().then( @@ -466,6 +467,14 @@ static void moq_source_on_broadcast(struct moq_source *ctx, const std::shared_pt return; } + // The offline instance's tracks end on their own once its route dies, holding the last + // frame, so only wait for whatever serves the path next. + if (!*result) { + LOG_INFO("Broadcast went offline: %s", conn->broadcast.c_str()); + moq_source_next_broadcast(ctx, conn); + return; + } + // Another instance took over, or the broadcast came back. The previous one's active tracks // keep playing until the new catalog's renditions commit, or until they end once its // route dies, holding the last frame. diff --git a/cpp/obs/test/moq-output-test.cpp b/cpp/obs/test/moq-output-test.cpp index 4c8ae88281a..da6b44d8fa0 100644 --- a/cpp/obs/test/moq-output-test.cpp +++ b/cpp/obs/test/moq-output-test.cpp @@ -408,10 +408,10 @@ struct MoQOutputStatsTest { auto event = TestOk(next.get(), "announcement"); if (!event) return std::nullopt; - const auto *start = std::get_if(&event->get_variant()); - if (!start) + const auto *present = std::get_if(&event->get_variant()); + if (!present) return std::nullopt; - return start->announce.route.instance; + return present->announce.route.instance; } static std::shared_ptr Session(MoQOutput &output) diff --git a/dart/moq/MIGRATE.md b/dart/moq/MIGRATE.md index bde762ab762..f2edcc86509 100644 --- a/dart/moq/MIGRATE.md +++ b/dart/moq/MIGRATE.md @@ -4,6 +4,28 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `Following.available()` returns null once the path goes offline + +`await following.available()` returns `BroadcastConsumer?`. +After the first call, it returns null once the followed path goes offline, or, for a `follow(path, instance: ...)` pinned to an instance, once another instance takes it over; the next call returns whatever serves the path next. The first call never returns null. +Drop what you read from the previous broadcast either way. A caller that only waits for the first broadcast can take it with `!`. + +### Announcements are a listing + +`announced(...).updates()` yields `AnnounceEventPresent` when a path becomes present and `AnnounceEventAbsent` when it goes, each carrying an `Announce`. +`AnnounceEventStart` is `AnnounceEventPresent` and `AnnounceEventEnd` is `AnnounceEventAbsent`. +`AnnounceEventUpdate`, `AnnounceEventRestart`, and `AnnounceEventTakeover` are gone: a path whose route or publisher instance changes stays present and is not reported again. +Where you requested the path again on a restart, hold `follow(path)` instead; each `available()` returns the next instance's broadcast. + +```dart +final following = moq.follow('live/camera'); +while (true) { + final broadcast = await following.available(); + if (broadcast == null) continue; // offline until the next instance + play(broadcast); +} +``` + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `Video` and `Audio` gain `warmup`, the catalog's decoder warmup; it defaults to `null`, so existing constructors still compile. `TrackConsumer` and the audio and video consumers read it from the rendition and start that much earlier. diff --git a/dart/moq/README.md b/dart/moq/README.md index cff7d70ce40..c6ccb9272be 100644 --- a/dart/moq/README.md +++ b/dart/moq/README.md @@ -10,16 +10,15 @@ final announced = connection.announced( options: const AnnounceOptions(prefix: 'live/', filter: '*/camera'), ); await for (final event in announced.updates()) { - // A restart is a new publisher run at the same path: request it again. A takeover - // continues what an earlier route served, like a start. + // The listing reports each path once while it stays present, even as its + // publisher restarts; `connection.follow(path)` hands out each new run, or + // null once it goes offline. switch (event) { - case AnnounceEventStart(:final announce) || - AnnounceEventRestart(:final announce) || - AnnounceEventTakeover(:final announce): + case AnnounceEventPresent(:final announce): // Prefix stays origin-relative; captures reports wildcard matches. print(announce.prefix); print(announce.captures); - default: + case AnnounceEventAbsent(): } } ``` diff --git a/dart/moq/lib/src/aliases.dart b/dart/moq/lib/src/aliases.dart index 81149d34860..478853db830 100644 --- a/dart/moq/lib/src/aliases.dart +++ b/dart/moq/lib/src/aliases.dart @@ -73,27 +73,18 @@ typedef AnnounceRoute = MoqAnnounceRoute; /// A route over a prefix: its origin-relative path, required wildcard captures (empty without wildcards), and route metadata. typedef Announce = MoqAnnounce; -/// What an [AnnounceConsumer] yields: [AnnounceEventStart], -/// [AnnounceEventUpdate], [AnnounceEventEnd], [AnnounceEventRestart], or -/// [AnnounceEventTakeover]. +/// What an [AnnounceConsumer] yields: [AnnounceEventPresent] or +/// [AnnounceEventAbsent]. +/// +/// Another publisher instance taking a path over keeps it present: hold a +/// [Following] to see the broadcast change. typedef AnnounceEvent = MoqAnnounceEvent; -/// A route now covers the prefix; the stream had none there. -typedef AnnounceEventStart = StartMoqAnnounceEvent; - -/// The route covering the prefix changed hops or cost. -typedef AnnounceEventUpdate = UpdateMoqAnnounceEvent; - -/// No route covers the prefix any more; carries its last route. -typedef AnnounceEventEnd = EndMoqAnnounceEvent; - -/// Another publisher instance now serves the prefix: drop what was resolved -/// under it and request afresh. -typedef AnnounceEventRestart = RestartMoqAnnounceEvent; +/// A route now covers the path, which was absent. +typedef AnnounceEventPresent = PresentMoqAnnounceEvent; -/// A route now covers the prefix and continues what the route at `predecessor` -/// served beneath it: keep what was resolved through that one. -typedef AnnounceEventTakeover = TakeoverMoqAnnounceEvent; +/// No route covers the path any more; carries its last route. +typedef AnnounceEventAbsent = AbsentMoqAnnounceEvent; /// The write side of a broadcast: publish tracks into it. typedef BroadcastProducer = MoqBroadcastProducer; diff --git a/dart/moq/lib/src/client.dart b/dart/moq/lib/src/client.dart index 0ab5c3c40af..18e7964173a 100644 --- a/dart/moq/lib/src/client.dart +++ b/dart/moq/lib/src/client.dart @@ -221,17 +221,20 @@ final class Moq { Publication createBroadcast(String path) => session.publish().createBroadcast(path: path); - /// Discover routes matching [options]; prefixes stay relative to the origin. + /// List the paths matching [options] as each becomes present or absent; + /// prefixes stay relative to the origin. /// /// Listen to `announced(...).updates()` for a [Stream] of [AnnounceEvent] - /// that releases the cursor when the subscription ends. + /// that releases the cursor when the subscription ends. A new publisher + /// instance keeps its path present, so use [follow] to see it. AnnounceConsumer announced({ AnnounceOptions options = const AnnounceOptions(), }) => session.consume().announced(config: options._ffi); /// Follow the broadcast at exactly [path]: each `available()` resolves the - /// next instance serving it, such as a restarted publisher. Pass an - /// [instance] to follow only that stream. + /// next instance serving it, such as a restarted publisher, or null once the + /// path goes offline. Pass an [instance] to follow only that stream; another + /// one taking over also resolves null. Following follow(String path, {String? instance}) => session.consume().follow(path: path, instance: instance); diff --git a/dart/moq/test/moq_test.dart b/dart/moq/test/moq_test.dart index 14749e44102..ffcc9daf1c8 100644 --- a/dart/moq/test/moq_test.dart +++ b/dart/moq/test/moq_test.dart @@ -279,7 +279,7 @@ void main() { viewer.dispose(); final announcement = client.announced().updates().firstWhere( - (event) => event is AnnounceEventStart, + (event) => event is AnnounceEventPresent, ); final publication = relay.createBroadcast(path: 'live'); final track = publication.broadcast().publishTrack( @@ -288,7 +288,7 @@ void main() { ); publication.announce(route: AnnounceRoute()); final announced = - (await announcement.timeout(timeout) as AnnounceEventStart).announce; + (await announcement.timeout(timeout) as AnnounceEventPresent).announce; expect(announced.prefix, 'live'); final requested = await client.request(announced.prefix).timeout(timeout); @@ -368,14 +368,14 @@ void main() { final serverSession = await accepted; final announcement = client.announced().updates().firstWhere( - (event) => event is AnnounceEventStart, + (event) => event is AnnounceEventPresent, ); final publication = server.createBroadcast('live'); final broadcast = publication.broadcast(); final track = broadcast.publishTrack(name: 'events', info: null); publication.announce(route: AnnounceRoute()); final announced = - (await announcement.timeout(timeout) as AnnounceEventStart).announce; + (await announcement.timeout(timeout) as AnnounceEventPresent).announce; expect(announced.prefix, 'live'); await client.close(); @@ -538,27 +538,27 @@ void main() { publication.announce(route: AnnounceRoute()); final announced = consumer.announced(config: MoqAnnounceConfig()); final first = await nextRoute(announced); - expect(first, isA()); - expect((first as AnnounceEventStart).announce.prefix, 'live'); + expect(first, isA()); + expect((first as AnnounceEventPresent).announce.prefix, 'live'); publication.unannounce(); final retracted = await nextRoute(announced); - expect(retracted, isA()); - expect((retracted as AnnounceEventEnd).announce.prefix, 'live'); + expect(retracted, isA()); + expect((retracted as AnnounceEventAbsent).announce.prefix, 'live'); await expectLater( consumer.request(path: 'live').timeout(timeout), throwsA(anything), ); publication.announce(route: AnnounceRoute()); - expect(await nextRoute(announced), isA()); + expect(await nextRoute(announced), isA()); await consumer.request(path: 'live').timeout(timeout); // Disposing the publication keeps the route; closing the broadcast ends it. publication.dispose(); await consumer.request(path: 'live').timeout(timeout); broadcast.close(); - expect(await nextRoute(announced), isA()); + expect(await nextRoute(announced), isA()); announced.cancel(); announced.dispose(); }); @@ -580,27 +580,57 @@ void main() { replacement.broadcast().close(); }); - test('a new instance restarts announce consumers', () async { + test('a new instance restarts followers but stays listed', () async { final origin = MoqOriginProducer(config: MoqOriginConfig()); - final announced = origin.consume().announced(config: MoqAnnounceConfig()); + final consumer = origin.consume(); + final announced = consumer.announced(config: MoqAnnounceConfig()); + final following = consumer.follow(path: 'instance'); final publication = origin.createBroadcast(path: 'instance'); publication.announce(route: AnnounceRoute()); - final start = await nextRoute(announced) as AnnounceEventStart; - expect(start.announce.route.instance, isNotEmpty); + final present = await nextRoute(announced) as AnnounceEventPresent; + expect(present.announce.route.instance, isNotEmpty); + (await following.available().timeout(timeout))!.dispose(); publication.announce(route: AnnounceRoute(instance: 'camera', epoch: 1)); - final restart = await nextRoute(announced) as AnnounceEventRestart; - expect(restart.announce.route.instance, 'camera'); + (await following.available().timeout(timeout))!.dispose(); + + // The takeover was not listed, so the next event is the path going absent. + publication.unannounce(); + final absent = await nextRoute(announced) as AnnounceEventAbsent; + expect(absent.announce.route.instance, 'camera'); expect( () => publication.announce(route: AnnounceRoute(instance: '')), throwsA(isA()), ); + following.cancel(); + following.dispose(); announced.cancel(); announced.dispose(); }); + test( + 'a follower reports the path going offline and the next broadcast', + () async { + final origin = MoqOriginProducer(config: MoqOriginConfig()); + final following = origin.consume().follow(path: 'offline'); + final publication = origin.createBroadcast(path: 'offline'); + + publication.announce(route: AnnounceRoute()); + (await following.available().timeout(timeout))!.dispose(); + + publication.unannounce(); + expect(await following.available().timeout(timeout), isNull); + + publication.announce(route: AnnounceRoute()); + (await following.available().timeout(timeout))!.dispose(); + + following.cancel(); + following.dispose(); + }, + ); + test('announced pattern reports captures', () async { final origin = MoqOriginProducer(config: MoqOriginConfig()); final announced = origin.consume().announced( @@ -609,7 +639,7 @@ void main() { final publication = origin.createBroadcast(path: 'room/alice/chat'); publication.announce(route: AnnounceRoute()); - final update = await nextRoute(announced) as AnnounceEventStart; + final update = await nextRoute(announced) as AnnounceEventPresent; expect(update.announce.prefix, 'room/alice/chat'); expect(update.announce.captures, ['alice']); }); diff --git a/dart/moq_ffi/lib/src/moq.dart b/dart/moq_ffi/lib/src/moq.dart index ef6628b69ac..0d4c40af414 100644 --- a/dart/moq_ffi/lib/src/moq.dart +++ b/dart/moq_ffi/lib/src/moq.dart @@ -8398,31 +8398,13 @@ class FfiConverterMoqAnnounceEvent { final subview = Uint8List.view(buf.buffer, buf.offsetInBytes + 4); switch (index) { case 1: - final lifted = StartMoqAnnounceEvent.read(subview); + final lifted = PresentMoqAnnounceEvent.read(subview); return LiftRetVal( lifted.value, lifted.bytesRead - subview.offsetInBytes + 4, ); case 2: - final lifted = UpdateMoqAnnounceEvent.read(subview); - return LiftRetVal( - lifted.value, - lifted.bytesRead - subview.offsetInBytes + 4, - ); - case 3: - final lifted = EndMoqAnnounceEvent.read(subview); - return LiftRetVal( - lifted.value, - lifted.bytesRead - subview.offsetInBytes + 4, - ); - case 4: - final lifted = RestartMoqAnnounceEvent.read(subview); - return LiftRetVal( - lifted.value, - lifted.bytesRead - subview.offsetInBytes + 4, - ); - case 5: - final lifted = TakeoverMoqAnnounceEvent.read(subview); + final lifted = AbsentMoqAnnounceEvent.read(subview); return LiftRetVal( lifted.value, lifted.bytesRead - subview.offsetInBytes + 4, @@ -8448,18 +8430,18 @@ class FfiConverterMoqAnnounceEvent { } } -class StartMoqAnnounceEvent extends MoqAnnounceEvent { +class PresentMoqAnnounceEvent extends MoqAnnounceEvent { final MoqAnnounce announce; - StartMoqAnnounceEvent(MoqAnnounce this.announce); - StartMoqAnnounceEvent._(MoqAnnounce this.announce); - static LiftRetVal read(Uint8List buf) { + PresentMoqAnnounceEvent(MoqAnnounce this.announce); + PresentMoqAnnounceEvent._(MoqAnnounce this.announce); + static LiftRetVal read(Uint8List buf) { int new_offset = buf.offsetInBytes; final announce_lifted = FfiConverterMoqAnnounce.read( Uint8List.view(buf.buffer, new_offset), ); final announce = announce_lifted.value; new_offset += announce_lifted.bytesRead; - return LiftRetVal(StartMoqAnnounceEvent._(announce), new_offset); + return LiftRetVal(PresentMoqAnnounceEvent._(announce), new_offset); } @override @@ -8486,18 +8468,18 @@ class StartMoqAnnounceEvent extends MoqAnnounceEvent { } } -class UpdateMoqAnnounceEvent extends MoqAnnounceEvent { +class AbsentMoqAnnounceEvent extends MoqAnnounceEvent { final MoqAnnounce announce; - UpdateMoqAnnounceEvent(MoqAnnounce this.announce); - UpdateMoqAnnounceEvent._(MoqAnnounce this.announce); - static LiftRetVal read(Uint8List buf) { + AbsentMoqAnnounceEvent(MoqAnnounce this.announce); + AbsentMoqAnnounceEvent._(MoqAnnounce this.announce); + static LiftRetVal read(Uint8List buf) { int new_offset = buf.offsetInBytes; final announce_lifted = FfiConverterMoqAnnounce.read( Uint8List.view(buf.buffer, new_offset), ); final announce = announce_lifted.value; new_offset += announce_lifted.bytesRead; - return LiftRetVal(UpdateMoqAnnounceEvent._(announce), new_offset); + return LiftRetVal(AbsentMoqAnnounceEvent._(announce), new_offset); } @override @@ -8524,141 +8506,6 @@ class UpdateMoqAnnounceEvent extends MoqAnnounceEvent { } } -class EndMoqAnnounceEvent extends MoqAnnounceEvent { - final MoqAnnounce announce; - EndMoqAnnounceEvent(MoqAnnounce this.announce); - EndMoqAnnounceEvent._(MoqAnnounce this.announce); - static LiftRetVal read(Uint8List buf) { - int new_offset = buf.offsetInBytes; - final announce_lifted = FfiConverterMoqAnnounce.read( - Uint8List.view(buf.buffer, new_offset), - ); - final announce = announce_lifted.value; - new_offset += announce_lifted.bytesRead; - return LiftRetVal(EndMoqAnnounceEvent._(announce), new_offset); - } - - @override - RustBuffer lower() { - final buf = Uint8List(allocationSize()); - write(buf); - return toRustBuffer(buf); - } - - @override - int allocationSize() { - return FfiConverterMoqAnnounce.allocationSize(announce) + 4; - } - - @override - int write(Uint8List buf) { - buf.buffer.asByteData(buf.offsetInBytes).setInt32(0, 3); - int new_offset = buf.offsetInBytes + 4; - new_offset += FfiConverterMoqAnnounce.write( - announce, - Uint8List.view(buf.buffer, new_offset), - ); - return new_offset; - } -} - -class RestartMoqAnnounceEvent extends MoqAnnounceEvent { - final MoqAnnounce announce; - RestartMoqAnnounceEvent(MoqAnnounce this.announce); - RestartMoqAnnounceEvent._(MoqAnnounce this.announce); - static LiftRetVal read(Uint8List buf) { - int new_offset = buf.offsetInBytes; - final announce_lifted = FfiConverterMoqAnnounce.read( - Uint8List.view(buf.buffer, new_offset), - ); - final announce = announce_lifted.value; - new_offset += announce_lifted.bytesRead; - return LiftRetVal(RestartMoqAnnounceEvent._(announce), new_offset); - } - - @override - RustBuffer lower() { - final buf = Uint8List(allocationSize()); - write(buf); - return toRustBuffer(buf); - } - - @override - int allocationSize() { - return FfiConverterMoqAnnounce.allocationSize(announce) + 4; - } - - @override - int write(Uint8List buf) { - buf.buffer.asByteData(buf.offsetInBytes).setInt32(0, 4); - int new_offset = buf.offsetInBytes + 4; - new_offset += FfiConverterMoqAnnounce.write( - announce, - Uint8List.view(buf.buffer, new_offset), - ); - return new_offset; - } -} - -class TakeoverMoqAnnounceEvent extends MoqAnnounceEvent { - final MoqAnnounce announce; - final String predecessor; - TakeoverMoqAnnounceEvent({ - required MoqAnnounce this.announce, - required String this.predecessor, - }); - TakeoverMoqAnnounceEvent._( - MoqAnnounce this.announce, - String this.predecessor, - ); - static LiftRetVal read(Uint8List buf) { - int new_offset = buf.offsetInBytes; - final announce_lifted = FfiConverterMoqAnnounce.read( - Uint8List.view(buf.buffer, new_offset), - ); - final announce = announce_lifted.value; - new_offset += announce_lifted.bytesRead; - final predecessor_lifted = FfiConverterString.read( - Uint8List.view(buf.buffer, new_offset), - ); - final predecessor = predecessor_lifted.value; - new_offset += predecessor_lifted.bytesRead; - return LiftRetVal( - TakeoverMoqAnnounceEvent._(announce, predecessor), - new_offset, - ); - } - - @override - RustBuffer lower() { - final buf = Uint8List(allocationSize()); - write(buf); - return toRustBuffer(buf); - } - - @override - int allocationSize() { - return FfiConverterMoqAnnounce.allocationSize(announce) + - FfiConverterString.allocationSize(predecessor) + - 4; - } - - @override - int write(Uint8List buf) { - buf.buffer.asByteData(buf.offsetInBytes).setInt32(0, 5); - int new_offset = buf.offsetInBytes + 4; - new_offset += FfiConverterMoqAnnounce.write( - announce, - Uint8List.view(buf.buffer, new_offset), - ); - new_offset += FfiConverterString.write( - predecessor, - Uint8List.view(buf.buffer, new_offset), - ); - return new_offset; - } -} - enum MoqTransport { quic, webSocket, tcp, unix, webTransport } class FfiConverterMoqTransport { @@ -13585,7 +13432,7 @@ class FfiConverterMoqBroadcastRequest { } abstract class MoqFollowingInterface { - UniffiRustFuture available(); + UniffiRustFuture available(); void cancel(); } @@ -13626,16 +13473,15 @@ class MoqFollowing implements MoqFollowingInterface { } } - UniffiRustFuture available() { + UniffiRustFuture available() { return uniffiRustCallAsync( () => uniffi_moq_ffi_fn_method_moqfollowing_available(uniffiClonePointer()), - ffi_moq_ffi_rust_future_poll_u64, - ffi_moq_ffi_rust_future_complete_u64, - ffi_moq_ffi_rust_future_cancel_u64, - ffi_moq_ffi_rust_future_free_u64, - (ptr) => - FfiConverterMoqBroadcastConsumer.lift(Pointer.fromAddress(ptr)), + ffi_moq_ffi_rust_future_poll_rust_buffer, + ffi_moq_ffi_rust_future_complete_rust_buffer, + ffi_moq_ffi_rust_future_cancel_rust_buffer, + ffi_moq_ffi_rust_future_free_rust_buffer, + FfiConverterOptionalMoqBroadcastConsumer.lift, moqExceptionErrorHandler, ); } @@ -17060,6 +16906,58 @@ class FfiConverterOptionalMoqBandwidthConsumer { } } +class FfiConverterOptionalMoqBroadcastConsumer { + static MoqBroadcastConsumer? lift(RustBuffer buf) { + return FfiConverterOptionalMoqBroadcastConsumer.read( + buf.asUint8List(), + ).value; + } + + static LiftRetVal read(Uint8List buf) { + if (ByteData.view(buf.buffer, buf.offsetInBytes).getInt8(0) == 0) { + return LiftRetVal(null, 1); + } + final result = FfiConverterMoqBroadcastConsumer.read( + Uint8List.view(buf.buffer, buf.offsetInBytes + 1), + ); + return LiftRetVal( + result.value, + result.bytesRead + 1, + ); + } + + static int allocationSize([MoqBroadcastConsumer? value]) { + if (value == null) { + return 1; + } + return FfiConverterMoqBroadcastConsumer.allocationSize(value) + 1; + } + + static RustBuffer lower(MoqBroadcastConsumer? value) { + if (value == null) { + return toRustBuffer(Uint8List.fromList([0])); + } + final buf = Uint8List( + FfiConverterOptionalMoqBroadcastConsumer.allocationSize(value), + ); + FfiConverterOptionalMoqBroadcastConsumer.write(value, buf); + return toRustBuffer(buf); + } + + static int write(MoqBroadcastConsumer? value, Uint8List buf) { + if (value == null) { + buf[0] = 0; + return 1; + } + buf[0] = 1; + return FfiConverterMoqBroadcastConsumer.write( + value, + Uint8List.view(buf.buffer, buf.offsetInBytes + 1), + ) + + 1; + } +} + class FfiConverterOptionalMoqCatalog { static MoqCatalog? lift(RustBuffer buf) { return FfiConverterOptionalMoqCatalog.read(buf.asUint8List()).value; @@ -23769,7 +23667,7 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqbroadcastrequest_serve() != 16802) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqfollowing_available() != 39867) { + if (uniffi_moq_ffi_checksum_method_moqfollowing_available() != 26337) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqfollowing_cancel() != 37387) { @@ -23778,7 +23676,7 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqoriginconsumer_announced() != 35661) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqoriginconsumer_follow() != 34533) { + if (uniffi_moq_ffi_checksum_method_moqoriginconsumer_follow() != 55889) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqoriginconsumer_request() != 40910) { diff --git a/dart/moq_ffi/test/future_test.dart b/dart/moq_ffi/test/future_test.dart index f671129d350..92d8d311bdb 100644 --- a/dart/moq_ffi/test/future_test.dart +++ b/dart/moq_ffi/test/future_test.dart @@ -140,7 +140,7 @@ void main() { final publication = origin.createBroadcast(path: 'still-listening'); try { publication.announce(route: MoqAnnounceRoute()); - expect(await next, isA()); + expect(await next, isA()); } finally { publication.broadcast().close(); } diff --git a/demo/carla/README.md b/demo/carla/README.md index 823d544bf9f..b43465d6701 100644 --- a/demo/carla/README.md +++ b/demo/carla/README.md @@ -106,7 +106,7 @@ The worker's gate (`sim/moq_carla_sim/drive.py`) owns every truck's controls: - **One driver at a time.** The first driver to send a command holds the lease until its broadcast ends or its 1 s watchdog expires; the player heartbeats every 250 ms. The lease is bound to the driver's path and the - route instance it announced with, so a driver that reconnects at the same + publisher instance serving it, so a driver that reconnects at the same path is a new claimant rather than a continuation. - **E-stop latches** until an explicit reset, from any viewer: the player latches and resets it through a short-lived safety broadcast without taking diff --git a/demo/carla/sim/moq_carla_sim/drive.py b/demo/carla/sim/moq_carla_sim/drive.py index af7f4373d00..fde62c302a9 100644 --- a/demo/carla/sim/moq_carla_sim/drive.py +++ b/demo/carla/sim/moq_carla_sim/drive.py @@ -94,10 +94,10 @@ class Command: `drive`, `estop`, `reset_estop`, and `respawn` arrive on the wire; `driver_left` and `watchdog_expired` are synthesized locally. - `driver_id` is the driver's path segment, which `status` echoes. `instance` is - the route instance its broadcast was announced under: the same id announced - under another instance is another run of the driver, so it is a different - driver for the lease. + `driver_id` is the driver's path segment, which `status` echoes. `instance` + names the run that sent it, one per publisher instance serving the path: the + same id under another instance is another run of the driver, so it is a + different driver for the lease. """ kind: CommandKind @@ -114,8 +114,8 @@ class Drive: The first driver to send a command owns the truck until it disconnects or its command lease expires. Other drivers cannot silently steal control. - The lease binds to the driver's broadcast path and the instance it was - announced under, so a driver whose broadcast ends or restarts loses it. + The lease binds to the driver's broadcast path and the run serving it, so a + driver whose broadcast ends or restarts loses it. It is still cooperative, not enforced: the holder's id is echoed on the public `status` track, so anyone allowed to publish there can announce at that path under another instance, which ends the holder's broadcast and frees diff --git a/demo/carla/sim/moq_carla_sim/publish.py b/demo/carla/sim/moq_carla_sim/publish.py index bdc496325f7..34f334ce683 100644 --- a/demo/carla/sim/moq_carla_sim/publish.py +++ b/demo/carla/sim/moq_carla_sim/publish.py @@ -10,6 +10,7 @@ from __future__ import annotations import asyncio +import itertools import logging import math from dataclasses import dataclass @@ -301,71 +302,94 @@ async def watch_drivers( ) -> None: """Discover driver broadcasts at `{prefix}/{driverId}` and fan their commands in. - Runs until cancelled. Each run of a driver, its path plus the route instance it - announced under, gets its own task, so one slow or stuck driver cannot hold - up the others. A broadcast that ends, or restarts under another instance, ends - its task, and with it the lease of that run; a restart starts a new task. + Runs until cancelled. Each driver path the listing reports present gets its own + task, so one slow or stuck driver cannot hold up the others. That task follows + the path across the driver's runs, one publisher instance each; a run that + another takes over, or whose path goes absent, ends, and with it its lease. """ - drivers: dict[str, tuple[str, asyncio.Task[None]]] = {} - - def forget(driver_id: str, task: asyncio.Task[None]) -> None: - # Only this run's entry: a restart has already put its successor there. - current = drivers.get(driver_id) - if current is not None and current[1] is task: - del drivers[driver_id] + drivers: dict[str, asyncio.Task[None]] = {} try: async with client.announced(prefix, filter="*") as announced: async for event in announced: - # Every variant carries `announce`; uniffi's stubs type only the enum. - announce = event.announce # pyright: ignore[reportAttributeAccessIssue] - path = announce.prefix + # Both variants carry `announce`; uniffi's stubs type only the enum. + path = event.announce.prefix # pyright: ignore[reportAttributeAccessIssue] driver_id = path.removeprefix(prefix + "/") # The filter admits one segment below the prefix; anything else # is a route that only overlaps it, not a driver's broadcast. if driver_id == path or not driver_id or "/" in driver_id: continue - instance = announce.route.instance - current = drivers.get(driver_id) - if isinstance(event, moq.AnnounceEventEnd): - if current is not None: - del drivers[driver_id] - current[1].cancel() + if isinstance(event, moq.AnnounceEventAbsent): + task = drivers.pop(driver_id, None) + if task is not None: + task.cancel() continue - if current is not None: - # Same run: the route only changed hops or cost. - if current[0] == instance and not isinstance(event, moq.AnnounceEventRestart): - continue - current[1].cancel() - task = asyncio.create_task( - _pump_driver(driver_id, instance, client, path, commands), + drivers[driver_id] = asyncio.create_task( + _follow_driver(driver_id, client, path, commands), name=f"driver:{driver_id}", ) - drivers[driver_id] = (instance, task) - task.add_done_callback(lambda task, driver_id=driver_id: forget(driver_id, task)) finally: - for _, task in drivers.values(): + for task in drivers.values(): task.cancel() -async def _pump_driver( +# Names each driver run for the lease. The bindings hand out a run's broadcast +# without its route instance, and a process-wide count keeps a run's late +# `driver_left` from releasing a later run of the same driver. +_runs = itertools.count(1) + + +async def _follow_driver( driver_id: str, - instance: str, client: moq.Client, path: str, commands: asyncio.Queue[Command], +) -> None: + """Pump each run of one driver in turn, ending a run once another takes over or it goes offline.""" + run: asyncio.Task[None] | None = None + try: + async with client.follow(path) as following: + while True: + try: + broadcast = await following.available() + # A refusal leaves the follow waiting for another run; only shutdown ends it. + except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] + if moq.is_shutdown(err): + raise + log.info("driver %s refused: %s", driver_id, err) + continue + if run is not None: + run.cancel() + run = None + if broadcast is None: + continue + instance = str(next(_runs)) + run = asyncio.create_task( + _pump_driver(driver_id, instance, broadcast, commands), + name=f"driver:{driver_id}:{instance}", + ) + # uniffi's stubs declare the error a plain class; at runtime it is an Exception. + except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] + log.info("driver %s follow ended: %s", driver_id, err) + finally: + if run is not None: + run.cancel() + + +async def _pump_driver( + driver_id: str, + instance: str, + broadcast: moq.BroadcastConsumer, + commands: asyncio.Queue[Command], ) -> None: """Feed one driver run's `control` track into the command queue. Always emits `driver_left` on the way out, however it leaves, so the gate releases a lease even when the track ends abnormally. """ - log.info("driver %s connected (instance %s)", driver_id, instance) + log.info("driver %s connected (run %s)", driver_id, instance) try: - # Pinned to this run, so a newer one that took over before its own - # announcement cancels this task is never read under this run's lease. - broadcast = await client.request(path, instance=instance) - control = moq.json.SnapshotConsumer(await broadcast.subscribe_track("control")) + control = moq.json.SnapshotConsumer(broadcast, "control") async for message in control: command = parse_command(driver_id, message.value, instance) if command is not None: @@ -374,7 +398,7 @@ async def _pump_driver( except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] log.info("driver %s track ended: %s", driver_id, err) finally: - log.info("driver %s disconnected (instance %s)", driver_id, instance) + log.info("driver %s disconnected (run %s)", driver_id, instance) await commands.put(Command(kind="driver_left", driver_id=driver_id, instance=instance)) diff --git a/demo/carla/sim/tests/conftest.py b/demo/carla/sim/tests/conftest.py index 64c01c0c189..1aa31b9689e 100644 --- a/demo/carla/sim/tests/conftest.py +++ b/demo/carla/sim/tests/conftest.py @@ -17,19 +17,11 @@ def __init__(self, announce: SimpleNamespace) -> None: self.announce = announce -class AnnounceEventStart(AnnounceEvent): +class AnnounceEventPresent(AnnounceEvent): pass -class AnnounceEventUpdate(AnnounceEvent): - pass - - -class AnnounceEventEnd(AnnounceEvent): - pass - - -class AnnounceEventRestart(AnnounceEvent): +class AnnounceEventAbsent(AnnounceEvent): pass @@ -48,10 +40,11 @@ def update(self, value: object, timestamp: object) -> None: class SnapshotConsumer: - """Wraps parsed fake values in timed records, like the runtime consumer.""" + """Reads the named fake track of a broadcast, wrapping its parsed values in timed records, + like the runtime consumer.""" - def __init__(self, track: object) -> None: - self._track = track + def __init__(self, broadcast: object, name: str, *, priority: int = 0) -> None: + self._track = broadcast.tracks[name] async def __aiter__(self): async for value in self._track: @@ -60,10 +53,10 @@ async def __aiter__(self): moq = ModuleType("moq") moq.Error = MoqError -moq.AnnounceEventStart = AnnounceEventStart -moq.AnnounceEventUpdate = AnnounceEventUpdate -moq.AnnounceEventEnd = AnnounceEventEnd -moq.AnnounceEventRestart = AnnounceEventRestart +# The stub has no shutdown errors: every `MoqError` it raises is a failure. +moq.is_shutdown = lambda err: False +moq.AnnounceEventPresent = AnnounceEventPresent +moq.AnnounceEventAbsent = AnnounceEventAbsent moq.Timestamp = lambda value, scale: SimpleNamespace(value=value, scale=scale) moq.TrackInfo = lambda **kwargs: SimpleNamespace(**kwargs) diff --git a/demo/carla/sim/tests/test_publish.py b/demo/carla/sim/tests/test_publish.py index 2ed3994dedd..c4736d765ab 100644 --- a/demo/carla/sim/tests/test_publish.py +++ b/demo/carla/sim/tests/test_publish.py @@ -176,14 +176,30 @@ def closed(_payload: object, _timestamp: object) -> None: publisher.publish({}) -class Announcements: - """A scripted announcement stream. `send` returns once the watcher has - handled the event and come back for the next one.""" +class Scripted: + """A scripted async source. `send` returns once the reader has handled the + item and come back for the next one.""" def __init__(self) -> None: - self._events: asyncio.Queue = asyncio.Queue() + self._items: asyncio.Queue = asyncio.Queue() self._idle = asyncio.Event() + async def next(self): + if self._items.empty(): + self._idle.set() + item = await self._items.get() + self._idle.clear() + return item + + async def send(self, item) -> None: + self._idle.clear() + self._items.put_nowait(item) + await self._idle.wait() + + +class Announcements(Scripted): + """The scripted listing of driver paths.""" + async def __aenter__(self): return self @@ -194,43 +210,66 @@ def __aiter__(self): return self async def __anext__(self): - if self._events.empty(): - self._idle.set() - event = await self._events.get() - self._idle.clear() - return event + return await self.next() - async def send(self, kind: type, path: str, instance: str) -> None: - self._idle.clear() - self._events.put_nowait(kind(SimpleNamespace(prefix=path, route=SimpleNamespace(instance=instance)))) - await self._idle.wait() + async def present(self, path: str) -> None: + await self.send(moq.AnnounceEventPresent(SimpleNamespace(prefix=path))) + + async def absent(self, path: str) -> None: + await self.send(moq.AnnounceEventAbsent(SimpleNamespace(prefix=path))) + + +class Following(Scripted): + """A scripted follow of one path: each `run` hands out another instance, `offline` a `None`, + and `refuse` an error.""" + + async def __aenter__(self): + return self + + async def __aexit__(self, *_exc) -> None: + pass + + async def available(self): + item = await self.next() + if isinstance(item, BaseException): + raise item + return item + + async def refuse(self) -> None: + """Fail the next `available()`, as a handler refusing the path does.""" + await self.send(moq.Error("refused")) + + async def run(self, sequence: int) -> None: + """Hand out an instance that serves one drive command numbered `sequence`, then stays open.""" + + async def control(): + yield {"type": "drive", "sequence": sequence, "throttle": 0.5} + await asyncio.Event().wait() + + await self.send(SimpleNamespace(tracks={"control": control()})) + + async def offline(self) -> None: + await self.send(None) class Drivers: - """A relay holding driver broadcasts: each request serves one drive command - numbered by how many requests came before it, then stays open.""" + """A relay holding driver broadcasts.""" def __init__(self) -> None: self.announcements = Announcements() - self.requested: list[tuple[str, str | None]] = [] + self.followed: list[str] = [] + self._followings: dict[str, Following] = {} def announced(self, prefix: str, *, filter: str): assert (prefix, filter) == ("viewer/carla/truck1", "*") return self.announcements - async def request(self, path: str, *, instance: str | None = None): - self.requested.append((path, instance)) - sequence = len(self.requested) + def following(self, path: str) -> Following: + return self._followings.setdefault(path, Following()) - async def control(): - yield {"type": "drive", "sequence": sequence, "throttle": 0.5} - await asyncio.Event().wait() - - async def subscribe_track(name: str): - assert name == "control" - return control() - - return SimpleNamespace(subscribe_track=subscribe_track) + def follow(self, path: str) -> Following: + self.followed.append(path) + return self.following(path) async def watching(scenario) -> None: @@ -252,63 +291,82 @@ async def next_command(commands: asyncio.Queue, gate: Drive): return command -def test_a_driver_run_keeps_the_lease_across_route_updates() -> None: - """An update is the same run re-priced: no second subscription, no release.""" +DRIVER = "viewer/carla/truck1/abc" + +def test_a_driver_whose_broadcast_goes_absent_loses_the_lease() -> None: async def scenario(relay: Drivers, commands: asyncio.Queue, gate: Drive) -> None: - await relay.announcements.send(moq.AnnounceEventStart, "viewer/carla/truck1/abc", "run-1") + await relay.announcements.present(DRIVER) + await relay.following(DRIVER).run(1) command = await next_command(commands, gate) - assert (command.kind, command.driver_id, command.instance) == ("drive", "abc", "run-1") + assert (command.kind, command.driver_id) == ("drive", "abc") assert gate.driver == "abc" - await relay.announcements.send(moq.AnnounceEventUpdate, "viewer/carla/truck1/abc", "run-1") - assert commands.empty() - assert gate.driver == "abc" - assert relay.requested == [("viewer/carla/truck1/abc", "run-1")] + await relay.announcements.absent(DRIVER) + left = await next_command(commands, gate) + assert (left.kind, left.instance) == ("driver_left", command.instance) + assert gate.driver is None asyncio.run(watching(scenario)) -def test_a_driver_whose_broadcast_ends_loses_the_lease() -> None: +def test_a_driver_whose_run_goes_offline_loses_the_lease_until_it_returns() -> None: async def scenario(relay: Drivers, commands: asyncio.Queue, gate: Drive) -> None: - await relay.announcements.send(moq.AnnounceEventStart, "viewer/carla/truck1/abc", "run-1") - await next_command(commands, gate) - assert gate.driver == "abc" + await relay.announcements.present(DRIVER) + await relay.following(DRIVER).run(1) + first = await next_command(commands, gate) - await relay.announcements.send(moq.AnnounceEventEnd, "viewer/carla/truck1/abc", "run-1") + await relay.following(DRIVER).offline() left = await next_command(commands, gate) - assert (left.kind, left.instance) == ("driver_left", "run-1") + assert (left.kind, left.instance) == ("driver_left", first.instance) assert gate.driver is None + await relay.following(DRIVER).run(2) + second = await next_command(commands, gate) + assert second.kind == "drive" and second.instance != first.instance + assert gate.driver == "abc" + asyncio.run(watching(scenario)) -@pytest.mark.parametrize("kind", ["restart", "update"]) -def test_a_driver_that_restarts_under_a_new_instance_is_a_new_driver(kind: str) -> None: - """The old run's lease ends with it, even though the path and id are the same. +def test_a_refused_driver_is_still_followed() -> None: + """A refusal leaves the follow waiting, so the driver's next run still takes the lease.""" + + async def scenario(relay: Drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present(DRIVER) + await relay.following(DRIVER).refuse() + await relay.following(DRIVER).run(1) + command = await next_command(commands, gate) + assert (command.kind, command.driver_id) == ("drive", "abc") + assert relay.followed == [DRIVER] + + asyncio.run(watching(scenario)) + - A new instance is a restart whether the relay says so or reports it as an update. - """ - event = moq.AnnounceEventRestart if kind == "restart" else moq.AnnounceEventUpdate +def test_a_driver_that_restarts_under_a_new_instance_is_a_new_driver() -> None: + """The old run's lease ends with it, even though the path and id are the same.""" async def scenario(relay: Drivers, commands: asyncio.Queue, gate: Drive) -> None: - await relay.announcements.send(moq.AnnounceEventStart, "viewer/carla/truck1/abc", "run-1") - await next_command(commands, gate) + await relay.announcements.present(DRIVER) + await relay.following(DRIVER).run(1) + first = await next_command(commands, gate) - await relay.announcements.send(event, "viewer/carla/truck1/abc", "run-2") + await relay.following(DRIVER).run(2) received = [await next_command(commands, gate) for _ in range(2)] - assert {(command.kind, command.instance) for command in received} == { - ("driver_left", "run-1"), - ("drive", "run-2"), + assert {(command.kind, command.instance == first.instance) for command in received} == { + ("driver_left", True), + ("drive", False), } - assert relay.requested == [("viewer/carla/truck1/abc", "run-1"), ("viewer/carla/truck1/abc", "run-2")] + # The path stays present across the restart, so one follow sees both runs. + assert relay.followed == [DRIVER] # Whichever order those two arrived in, the new run holds the lease # by its next command, and the old run can no longer release it. - assert not gate.holds(Command(kind="drive", driver_id="abc", instance="run-1")) - gate.apply(Command(kind="drive", driver_id="abc", instance="run-2", sequence=3, state=DriveState())) - assert gate.holds(Command(kind="drive", driver_id="abc", instance="run-2")) - assert not gate.apply(Command(kind="driver_left", driver_id="abc", instance="run-1")) + second = next(command.instance for command in received if command.kind == "drive") + assert not gate.holds(Command(kind="drive", driver_id="abc", instance=first.instance)) + gate.apply(Command(kind="drive", driver_id="abc", instance=second, sequence=3, state=DriveState())) + assert gate.holds(Command(kind="drive", driver_id="abc", instance=second)) + assert not gate.apply(Command(kind="driver_left", driver_id="abc", instance=first.instance)) assert gate.driver == "abc" asyncio.run(watching(scenario)) @@ -316,9 +374,9 @@ async def scenario(relay: Drivers, commands: asyncio.Queue, gate: Drive) -> None def test_only_direct_children_of_the_prefix_are_drivers() -> None: async def scenario(relay: Drivers, commands: asyncio.Queue, gate: Drive) -> None: - await relay.announcements.send(moq.AnnounceEventStart, "viewer/carla", None) - await relay.announcements.send(moq.AnnounceEventStart, "viewer/carla/truck1/abc/deeper", None) - await relay.announcements.send(moq.AnnounceEventStart, "viewer/carla/truck10", None) - assert relay.requested == [] + await relay.announcements.present("viewer/carla") + await relay.announcements.present("viewer/carla/truck1/abc/deeper") + await relay.announcements.present("viewer/carla/truck10") + assert relay.followed == [] asyncio.run(watching(scenario)) diff --git a/demo/carla/web/src/player.ts b/demo/carla/web/src/player.ts index 9dda7025d71..010dfdec7da 100644 --- a/demo/carla/web/src/player.ts +++ b/demo/carla/web/src/player.ts @@ -123,14 +123,14 @@ export function mount(root: ShadowRoot, connection: Moq.Connection): () => void // A broad route can cover the scope without naming one camera. if (!parseCamera(entry.prefix)) continue; - // A restart is a new simulator instance on the same path; the watch - // broadcast follows it, so only an end removes the tile. - if (entry.kind !== "end" && !tiles.has(entry.prefix)) { + // A restarted simulator keeps its path present, and the watch broadcast + // follows it, so only an absence removes the tile. + if (entry.kind !== "absent" && !tiles.has(entry.prefix)) { const truck = new Truck({ broadcastId: entry.prefix, connection, driving, focused, hovered }); tiles.set(entry.prefix, new TruckTile(truck)); vehicles.set(tiles.size); refreshFocusLayout(); - } else if (entry.kind === "end") { + } else if (entry.kind === "absent") { const tile = tiles.get(entry.prefix); tile?.dispose(); tiles.delete(entry.prefix); diff --git a/demo/web/src/index.ts b/demo/web/src/index.ts index e7342608394..c5128d3a9e9 100644 --- a/demo/web/src/index.ts +++ b/demo/web/src/index.ts @@ -199,7 +199,7 @@ discovery.run((effect) => { // Only catalog-backed broadcasts are watchable streams; this skips the relay's // `.stats` broadcast (see the stats dashboard demo for that one). if (!path.endsWith(".hang") && !path.endsWith(".msf")) continue; - if (entry.kind === "end") live.delete(path); + if (entry.kind === "absent") live.delete(path); else live.add(path); broadcasts.set([...live].sort()); } diff --git a/demo/web/src/stats.ts b/demo/web/src/stats.ts index 3b689cd2c32..17c9ad5eb7e 100644 --- a/demo/web/src/stats.ts +++ b/demo/web/src/stats.ts @@ -6,7 +6,7 @@ * auto-discover all of those nodes (announcements under `.stats/node`), so this * works for a single relay and for a cluster alike, then aggregate each node and * let you drill into one. A restarted relay announces a new instance at the same - * path, which arrives as an end then a start, so its history starts over. + * path, which each node's follower hands out afresh, so its history starts over. * * Per node we read `group.json`, the whole node's group at the relay's default * `depth = 0`: `{ epoch, tiers: { : { paths, labels } } }`, a JSON snapshot @@ -161,13 +161,7 @@ discovery.run((effect) => { const node = Net.Path.stripPrefix(prefix, path); if (!node) continue; - // The same node re-priced or failed over: its counters carry on. - if (entry.kind === "update") continue; - if (entry.kind === "start" && subs.has(node)) continue; - - // An end, or a restart: another instance at the path counts from zero, so neither - // series may splice across it. - if (entry.kind !== "start") { + if (entry.kind === "absent") { subs.get(node)?.close(); subs.delete(node); nodeStats.mutate((s) => { @@ -175,8 +169,9 @@ discovery.run((effect) => { }); history.delete(node); clusterMembership = ""; + continue; } - if (entry.kind === "end") continue; + if (subs.has(node)) continue; const ne = new Signals.Effect(); subs.set(node, ne); @@ -186,30 +181,36 @@ discovery.run((effect) => { }); function subscribeNode(effect: Signals.Effect, origin: Net.Origin.Reader, path: Net.Path.Valid, node: string) { - nodeStats.mutate((s) => { - s[node] = { - egress: {}, - ingress: {}, - sessions: {}, - }; - }); - - // The path was just announced, so the request resolves from the table immediately. - const request = origin.request(path); - effect.cleanup(() => request.close()); - const consumer = request.active.peek(); - if (!consumer) return; - - const group = new Json.Snapshot.Consumer({ track: consumer.track("group.json") }); - effect.cleanup(() => group.close()); - effect.spawn(async () => { - for (;;) { - const state = await effect.race(group.latest()); - if (state === undefined) break; - nodeStats.mutate((s) => { - if (s[node]) s[node] = nodeStatsOf(state.value ?? {}); - }); - } + // Follow the node across instances: a restarted one counts from zero, so its series starts + // afresh rather than splicing onto the old one's. + const following = origin.follow(path); + effect.cleanup(() => following.close()); + effect.run((instance) => { + const consumer = instance.get(following.active); + nodeStats.mutate((s) => { + s[node] = { + egress: {}, + ingress: {}, + sessions: {}, + }; + }); + history.delete(node); + // The aggregate spans every node, so a restarted node's counters must not splice into + // its history either. + clusterMembership = ""; + if (!consumer) return; + + const group = new Json.Snapshot.Consumer({ track: consumer.track("group.json") }); + instance.cleanup(() => group.close()); + instance.spawn(async () => { + for (;;) { + const state = await instance.race(group.latest()); + if (state === undefined) break; + nodeStats.mutate((s) => { + if (s[node]) s[node] = nodeStatsOf(state.value ?? {}); + }); + } + }); }); } diff --git a/doc/concept/identity.md b/doc/concept/identity.md index 024877746f5..681c50d54c4 100644 --- a/doc/concept/identity.md +++ b/doc/concept/identity.md @@ -119,14 +119,22 @@ content into it: subscriptions already open stay on the old stream until its last route goes, then end with an error. New requests resolve the winner on a fresh broadcast. Choose between two kinds of reader. -**Follow announcements** when the reader should outlive any one stream, as a -player does. Start on each broadcast, drop all decoder and buffer state on a -restart, and wait on an end. Rust uses `consumer.follow(path, None)` (or -`moq_mux::follow::Instances`), JavaScript `origin.follow(path)`, and the -moq-ffi bindings `follow(path)` with `available()`. `@moq/watch`, `moq play`, -and `moqsrc` follow this way, and `moq export ts --stitch` can splice a -replacement as a marked program switch. A request failing is never the signal -to restart; the announcement is. Nor is a track's error kind: a track withdrawn +**Follow the path** when the reader should outlive any one stream, as a +player does. A follower hands out a new broadcast each time another instance +takes the path; an epoch change or another route of the same instance hands +out nothing. Start on each broadcast, drop all decoder and buffer state when the +next one arrives, and wait while the path is offline. Rust uses +`consumer.follow(path, None)`, whose `available()` returns `None` once the path +goes offline; JavaScript `origin.follow(path)`, whose `active` is undefined +between instances; the moq-ffi bindings `follow(path)`, whose `available()` +returns null once the path goes offline; and C `moq_origin_follow`, whose +callback reports `MOQ_FOLLOW_EVENT_OFFLINE`. +An announcement listing (`announced()`) only reports a path becoming present or +absent: an instance change keeps it present, so a reader that needs one holds a +follower. `@moq/watch`, `moq play`, and `moqsrc` follow this way, and +`moq export ts --stitch` can splice a replacement as a marked program switch. A +request failing is never the signal to restart; the follower handing out a new +broadcast is. Nor is a track's error kind: a track withdrawn unfinished ends `Unroutable` over moq-lite but `Internal` over moq-transport, whose wire has no code for it. @@ -194,19 +202,19 @@ A lazy job's lifecycle, as a viewer of `transcode/alice` sees it through a relay 2. **Takeover.** Publishing the answer through the claim proves it was job A, so job A's route takes over the claim: what the viewer already resolved through the claim beneath `transcode/alice` keeps playing, now as instance - A. A Rust follower sees a takeover rather than a restart (JavaScript still - reports a start or restart). A different worker, or a job restarted under - the same claim, never inherits it. Only moq-lite 07 carries the takeover + A. A Rust follower is handed nothing new (a JavaScript follower still moves + to a new broadcast). A different worker, or a job restarted under the same + claim, never inherits it. Only moq-lite 07 carries the takeover across relays. Older wires, and paths where a relay cannot vouch for it, send an ordinary announcement instead, so the first subscription through - the claim is reset there: a follower sees a restart and requests again, and - a holder's track ends with an error, and it requests again. Either way the + the claim is reset there: a follower is handed the new broadcast, and a + holder's track ends with an error, and it requests again. Either way the second request reaches job A. 3. **Steady state.** Requests resolve instance A by its exact route. Routes carrying instance A through other relays fail over without a seam. 4. **Job restart.** The worker closes job A's publication. The path falls back to the claim, the next request asks the worker again, and job B publishes - under a fresh instance. Followers see each change as a restart; relays drop + under a fresh instance. Followers are handed each new broadcast; relays drop job A's cached groups, so a viewer returning within the cache window never gets them. Other paths under the claim, such as `transcode/bob`, are untouched. diff --git a/doc/lib/c/index.md b/doc/lib/c/index.md index f59d7597dde..d86d4e7af12 100644 --- a/doc/lib/c/index.md +++ b/doc/lib/c/index.md @@ -82,8 +82,10 @@ if (moq_publication_announce((uint32_t)publication, &route) < 0) A route carries only a cost and the stream's instance; relays add the hops. Each publication mints a fresh instance and keeps it across re-announcing, so a restarted -publisher is a new stream viewers restart onto (`MOQ_ANNOUNCE_KIND_RESTART`) instead of a -stalled broadcast. Set `instance` only for a publisher that continues its group sequences +publisher is a new stream that `moq_origin_follow` hands viewers instead of a stalled +broadcast; the path stays `MOQ_ANNOUNCE_KIND_PRESENT` throughout. The follow callback +reports each instance as `MOQ_FOLLOW_EVENT_ONLINE` with its handle, +`MOQ_FOLLOW_EVENT_OFFLINE` when the path goes offline, and one `MOQ_FOLLOW_EVENT_END`. Set `instance` only for a publisher that continues its group sequences across restarts, and raise `epoch` on each: readers stitch every route of one instance together, so restarting the sequences under an old instance splices unrelated groups into their tracks. diff --git a/doc/lib/cpp/index.md b/doc/lib/cpp/index.md index 7db92026a2c..56f5d8c7fe3 100644 --- a/doc/lib/cpp/index.md +++ b/doc/lib/cpp/index.md @@ -94,7 +94,7 @@ void ok(moq::expected result) { auto client = ok(moq::Client::init({})); // a moq::ClientConfig; {} takes every default auto session = ok(client->connect("https://relay.example.com").get()); auto announced = ok(session->consume()->follow("my-stream.hang", std::nullopt)); -auto broadcast = ok(announced->available().get()); +auto broadcast = ok(announced->available().get()); // the first call is never null auto catalogs = ok(moq::MediaCatalogConsumer::subscribe(broadcast).get()); if (auto catalog = ok(catalogs->next().get())) { // std::nullopt once the catalog ends for (const auto &[id, video] : catalog->video) { @@ -111,8 +111,9 @@ The config is generated, so nothing names this package for you: set after any product tokens of your own, and moq-ffi appends its own. Call `available()` again to wait for the next publisher instance at that path, such as a -restarted publisher, and drop what was read from the previous one then: its tracks end -when its session does. +restarted publisher. It returns null once the path goes offline, then the next +broadcast when something serves it again. Drop what was read from the previous instance +on either: its tracks end when its session does. `MediaTrackConsumer` reads `MediaRead` records in decode order: `kFrame`, `kGap`, or `kEnd`, like the audio and video decoders. Match the variant before taking its frame. diff --git a/doc/lib/dart/index.md b/doc/lib/dart/index.md index e6139f97676..23552e5633c 100644 --- a/doc/lib/dart/index.md +++ b/doc/lib/dart/index.md @@ -60,20 +60,19 @@ moq ) .updates() .listen((event) { - // A restart is a new publisher run at the same path: request it again. A takeover - // continues what an earlier route served, like a start. + // A path stays present across publisher restarts: hold moq.follow(path) to be handed + // each new run, or null once it goes offline. switch (event) { - case AnnounceEventStart(:final announce) || - AnnounceEventRestart(:final announce) || - AnnounceEventTakeover(:final announce): + case AnnounceEventPresent(:final announce): print(announce.prefix); // Captures are a list of wildcard matches, empty without wildcards. print(announce.captures); - default: + case AnnounceEventAbsent(): } }); -// Wait for a publisher at one path; call available() again for the next one. -final broadcast = await moq.follow('live/camera').available(); +// Wait for a publisher at one path. The first call never returns null; call available() +// again for the next one, or null once the path goes offline. +final broadcast = (await moq.follow('live/camera').available())!; final catalog = await media.CatalogConsumer.subscribe(broadcast: broadcast); print(await catalog.next()); ``` @@ -108,7 +107,7 @@ encoder.finish(); // Decode a subscribed rendition. Dispose each frame promptly. import 'package:moq/video.dart' as video; -final remote = await moq.follow('live/camera').available(); +final remote = (await moq.follow('live/camera').available())!; final renditions = await media.CatalogConsumer.subscribe(broadcast: remote); final decoder = await video.Consumer.subscribe( broadcast: remote, diff --git a/doc/lib/go/index.md b/doc/lib/go/index.md index 76ae948235e..b595ded6eb4 100644 --- a/doc/lib/go/index.md +++ b/doc/lib/go/index.md @@ -66,17 +66,13 @@ for event, err := range announced.All(ctx) { if moq.IsShutdown(err) { break } log.Fatal(err) } - var ann moq.Announce - switch event := event.(type) { - case moq.AnnounceEventStart: - ann = event.Announce - case moq.AnnounceEventRestart: // a new publisher run at the same path: request it again - ann = event.Announce - case moq.AnnounceEventTakeover: // continues what an earlier route served, like a start - ann = event.Announce - default: - continue // AnnounceEventUpdate or AnnounceEventEnd + // A path stays present across publisher restarts: hold client.Follow(path, nil) to be + // handed each new run, with Available returning nil while the path is offline. + present, ok := event.(moq.AnnounceEventPresent) + if !ok { + continue // AnnounceEventAbsent } + ann := present.Announce // Prefix stays origin-relative; Captures is a list, empty without wildcards. fmt.Printf("captures: %v\n", ann.Captures) broadcast, err := client.Request(ctx, ann.Prefix, nil) diff --git a/doc/lib/kt/index.md b/doc/lib/kt/index.md index 6f611203685..b2c524ba5ba 100644 --- a/doc/lib/kt/index.md +++ b/doc/lib/kt/index.md @@ -54,13 +54,11 @@ import dev.moq.media.* // Subscribe. The Flow is live, so run it in its own coroutine. Moq.connect("https://relay.example.com", ClientConfig(tls = ClientTls(roots = listOf("ca.pem")))).use { moq -> moq.announced(AnnounceConfig(prefix = "live/", filter = "*/camera")).updates().collect { event -> - // A restart is a new publisher run at the same path: request it again. A takeover - // continues what an earlier route served, like a start. + // A path stays present across publisher restarts: hold moq.follow(path) to be handed + // each new run, or null once it goes offline. val announce = when (event) { - is AnnounceEventStart -> event.announce - is AnnounceEventRestart -> event.announce - is AnnounceEventTakeover -> event.announce - else -> return@collect // Update or End + is AnnounceEventPresent -> event.announce + is AnnounceEventAbsent -> return@collect } // Prefixes stay origin-relative; captures is a list of wildcard matches, empty without wildcards. println(announce.captures) diff --git a/doc/lib/py/index.md b/doc/lib/py/index.md index 1064174de64..3d2fd2640a7 100644 --- a/doc/lib/py/index.md +++ b/doc/lib/py/index.md @@ -47,10 +47,10 @@ async def main(): async with moq.Client("https://cdn.moq.dev/anon") as client: # The filter is relative to the literal prefix; prefixes stay origin-relative. async for event in client.announced("live/", filter="*/camera"): - # A restart is a new publisher run at the same path: request it again. A - # takeover continues what an earlier route served, like a start. - if not isinstance(event, (moq.AnnounceEventStart, moq.AnnounceEventRestart, moq.AnnounceEventTakeover)): - continue # AnnounceEventUpdate or AnnounceEventEnd + # A path stays present across publisher restarts: hold client.follow(path) to + # be handed each new run, or None once it goes offline. + if not isinstance(event, moq.AnnounceEventPresent): + continue # AnnounceEventAbsent announcement = event.announce print(announcement.captures) # list of wildcard matches, empty without wildcards broadcast = await client.request(announcement.prefix) diff --git a/doc/lib/rs/index.md b/doc/lib/rs/index.md index 3a74e86a022..cbf2be17fdd 100644 --- a/doc/lib/rs/index.md +++ b/doc/lib/rs/index.md @@ -69,8 +69,9 @@ let session = client.with_origin(origin.clone()).connect(url); let consumer = origin.consume(); let mut announced = consumer.announced(); while let Some(event) = announced.next().await { - // Skip updates and retractions. - let moq_net::announce::Event::Start(update) = event else { continue }; + // Skip retractions. A path stays present across publisher restarts: hold + // `consumer.follow(path, None)` to be handed each new run. + let moq_net::announce::Event::Present(update) = event else { continue }; let broadcast = consumer.request(&update.prefix, update.route.instance).await?; let catalog = broadcast .track(hang::Catalog::DEFAULT_NAME)? diff --git a/doc/lib/rs/moq-net.md b/doc/lib/rs/moq-net.md index b54f83999ed..e96543e9395 100644 --- a/doc/lib/rs/moq-net.md +++ b/doc/lib/rs/moq-net.md @@ -18,7 +18,7 @@ What you use it for, beyond what the concept page already describes: - **An origin outlives the session.** Creating a broadcast returns an origin publication that controls its announcement and exposes the broadcast to fill. Standalone and Prepared fill producers cannot announce; only publishing the Prepared answer admits its publication. A reconnect announces existing publications again, and closing the session does not delete them. - **Requests can pin an instance.** `consumer.request(path, Some(instance))` resolves only through a route announcing that [publisher instance](/concept/moq-lite#publisher-instances); `None` takes whichever route wins. The resolved consumer names its instance in `info().instance`, pinned or not. A broadcast announced with `Route::default()` serves a random instance it keeps across re-announcing; set one with `Route::with_instance(instance, epoch)` only for a publisher that continues its group sequences across restarts and raises the epoch each time. - **Rank several CDNs.** Dial each with its own subscribe handle, `origin.clone().with_preference(origin::Preference::new(rank))` (higher wins; 0 is neutral and a negative rank demotes), all on one origin. Requests use the preferred link that has a route and fail over to the next; see [multiple CDNs](/concept/moq-lite#multiple-cdns). -- **Request one instance, or follow them all.** `consumer.request(path, None)` resolves the instance serving a path now, and fails `Unroutable` only once discovery has synced with no route for it. `consumer.follow(path, None)` hands out each publisher instance serving a path from `available()`: the first once announced, then each one that takes over or returns after an end, like JS `follow(path)`. A broadcast already resolved stays on the old instance until dropped or its route goes. See [identity and recovery](/concept/identity) to choose between following and holding one instance. +- **Request one instance, or follow them all.** `consumer.request(path, None)` resolves the instance serving a path now, and fails `Unroutable` only once discovery has synced with no route for it. `consumer.follow(path, None)` hands out each publisher instance serving a path from `available()`: the first once announced, then each one that takes over, `None` once the path goes offline, and whatever serves it next, like JS `follow(path)`. `consumer.announced()` lists paths only as present or absent. A broadcast already resolved stays on the old instance until dropped or its route goes. See [identity and recovery](/concept/identity) to choose between following and holding one instance. - **Discovery follows what you watch.** A client session asks the relay for announcements only under the prefixes its subscribe origin's readers watch, with `announced()`, `request`, or `follow`, stops asking about a prefix 2s after nothing at or beneath it is watched, and asks again on reconnect. A broadcast resolved through a discovered route keeps its path watched until dropped. `request` owns discovery of its path, so no separate watch is needed. A server session, or a client fed into an origin handle marked `with_link(origin::Link::Peer)` or `Link::Upstream`, asks for everything it may, so a relay discovers everything. An HTTP gateway (the moq-hls `Server`, the moq-rtc WHEP router) watches its whole scope while it lives, so the paths its callers name never open a request each. moq-lite-03/04 sessions, whose announcements mark no initial set, still ask for everything. - **Dial and accept sockets.** `socket::Endpoint::new(&origin, prefix, id)` announces a hidden endpoint at `/.socket/`, used only to route dials. A session speaks one of two protocols, each with its own dial and accept. `accept_tcp()` and `accept_track()` yield an `Incoming` dial to `accept` or `reject` (dropping it rejects). `dial_tcp(peer)` and an accepted `tcp` dial yield a `socket::TcpStream`, a `group::Producer` and `group::Consumer` pair carrying one reliable, ordered byte pipe each way (finish is FIN, abort is RST, and `ready` paces the writer to the reader); `dial_track(peer, info)` and an accepted `track` dial yield a `socket::TrackStream`, this side's `track::Producer` and a `track::Subscriber` of the peer's half, subscribed reliably and in order until the application updates it, one message per group. A dial resolves once the peer accepts, and an accept checks that the claimed dialer really dialed this endpoint, waiting for its endpoint while the dial stays open. Every hop needs moq-lite-07 or moq-transport: while the origin has an older moq-lite client session whose publish half doesn't opt into hidden broadcasts (`with_hidden(true)`), dials and accepts fail with `Unsupported`. A dial to a peer behind such a hop waits, as for a peer not online yet. Session tracks are never announced or listed. See [Sockets](/concept/socket). - **One track per name.** Concurrent subscriptions share one request and producer. The application owns sequence numbers: the broadcast keeps nothing of an ended track, so a producer that replaces it starts at 0. A name always means the same content, so the replacement continues past its predecessor's groups with `create_group` at an explicit sequence. Readers of an aborted track resume onto the replacement when they read through an origin request, whose route always names an instance; otherwise their subscription ends with the error. diff --git a/doc/lib/swift/index.md b/doc/lib/swift/index.md index 35895c71e49..4cf0183aee2 100644 --- a/doc/lib/swift/index.md +++ b/doc/lib/swift/index.md @@ -55,13 +55,9 @@ let client = try Client() let session = try await client.connect(to: "https://relay.example.com") for try await event in try session.consume.announced(prefix: "live/", filter: "*/camera") { - // A restart is a new publisher run at the same path: request it again. A takeover - // continues what an earlier route served, like a start. - let announcement: Announce - switch event { - case .start(let started), .restart(let started), .takeover(let started, _): announcement = started - default: continue // .update or .end - } + // A path stays present across publisher restarts: hold follow(path:) to be handed each + // new run, or nil once it goes offline. + guard case .present(let announcement) = event else { continue } // Prefixes stay origin-relative; captures is a list of wildcard matches, empty without wildcards. print(announcement.captures) let broadcast = try await session.consume.request(path: announcement.prefix) diff --git a/doc/setup/redundancy.md b/doc/setup/redundancy.md index 6357ec7d704..b632057c71a 100644 --- a/doc/setup/redundancy.md +++ b/doc/setup/redundancy.md @@ -57,10 +57,10 @@ again as a new broadcast. This is not a fixed primary/standby priority unless the routes' costs make it one, and a restarted publisher takes a fresh instance, so it may rank either way. -The switch is announced as a `Restart`. Players that follow announcements -request the path again and begin the replacement broadcast. Existing +The path stays announced across the switch, and a follower hands out the +replacement broadcast. Players that follow the path begin it. Existing subscriptions stay on the old instance while it remains available, unless the -consumer chooses to follow the restart. +consumer chooses to follow the switch. For MPEG-TS output, opt into following replacements: diff --git a/go/wrapper/MIGRATE.md b/go/wrapper/MIGRATE.md index bea18463d46..d11bc22dc22 100644 --- a/go/wrapper/MIGRATE.md +++ b/go/wrapper/MIGRATE.md @@ -4,6 +4,37 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `Following.Available` reports the path going offline + +After the first call, `Available` returns `(nil, nil)` once the followed path goes offline, or, for a `Follow` pinned to an instance, once another instance takes it over. +The next call returns whatever serves the path next. The first call is never nil. +Check for nil before using the broadcast, and drop what was read from the previous instance either way. + +### Announcements are a listing + +`AnnounceConsumer` yields only `AnnounceEventPresent`, when a route now covers a path that was absent, and `AnnounceEventAbsent`, when none does any more. +Rename `AnnounceEventStart` to `AnnounceEventPresent` and `AnnounceEventEnd` to `AnnounceEventAbsent`. +`AnnounceEventUpdate`, `AnnounceEventRestart`, and `AnnounceEventTakeover` with its `Predecessor` are gone: a route changing cost or hops, a restarted publisher, and a takeover all keep the path present and report nothing. +Instead of requesting the path again on a restart, hold `Follow` and read each new publisher instance from `Available`, dropping what was read from the previous one. + +```go +following, err := client.Follow(path, nil) +if err != nil { + return err +} +defer following.Cancel() +for { + broadcast, err := following.Available(ctx) + if err != nil { + return err + } + if broadcast == nil { + continue // offline until the next instance + } + play(broadcast) +} +``` + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `ffi.MoqVideo` and `ffi.MoqAudio` (`media.Video` and `media.Audio`) gain `Warmup`, the catalog's decoder warmup, nil when absent. `media.NewTrackConsumer` and the audio and video `Subscribe` read it from the rendition and start that much earlier; a negative one is refused with an error. Only unkeyed struct literals change. diff --git a/go/wrapper/README.md b/go/wrapper/README.md index c459b043a99..1550a52120f 100644 --- a/go/wrapper/README.md +++ b/go/wrapper/README.md @@ -51,17 +51,20 @@ for event, err := range announced.All(ctx) { log.Fatal(err) } switch event := event.(type) { - case moq.AnnounceEventStart: + case moq.AnnounceEventPresent: // Prefix stays origin-relative; Captures reports wildcard matches. - fmt.Println("got broadcast", event.Announce.Prefix) - case moq.AnnounceEventRestart: // a new publisher run at the same path - fmt.Println("restarted broadcast", event.Announce.Prefix) - case moq.AnnounceEventTakeover: // continues what an earlier route served - fmt.Println("took over broadcast", event.Announce.Prefix) + fmt.Println("broadcast online", event.Announce.Prefix) + case moq.AnnounceEventAbsent: + fmt.Println("broadcast offline", event.Announce.Prefix) } } ``` +The listing reports only whether each path is present. A restarted publisher or another +instance taking the path over keeps it present: to read the broadcast across them, hold +`client.Follow(path, nil)` and read each new instance from `Available`, which returns nil +while the path is offline. + ## Common APIs `Dial` takes a `ClientConfig` whose names and nesting mirror moq-ffi's diff --git a/go/wrapper/client.go b/go/wrapper/client.go index 87f37134d71..3982bca6ad4 100644 --- a/go/wrapper/client.go +++ b/go/wrapper/client.go @@ -307,7 +307,7 @@ func (c *Client) CreateBroadcast(path string) (*Publication, error) { return c.publisher.CreateBroadcast(path) } -// Announced streams routes announced by the remote under a pattern scope. +// Announced lists the paths the remote announces under a pattern scope, as each becomes present or absent. func (c *Client) Announced(options AnnounceOptions) (*AnnounceConsumer, error) { return c.consumer.Announced(options) } diff --git a/go/wrapper/example_test.go b/go/wrapper/example_test.go index da49aff1e08..aaba35f5a70 100644 --- a/go/wrapper/example_test.go +++ b/go/wrapper/example_test.go @@ -33,12 +33,48 @@ func ExampleClient_Announced() { } log.Fatal(err) } - if event, ok := event.(moq.AnnounceEventStart); ok { + if event, ok := event.(moq.AnnounceEventPresent); ok { fmt.Println("broadcast:", event.Announce.Prefix) } } } +// Read a broadcast across publisher restarts, noticing when it goes offline. +func ExampleClient_Follow() { + ctx := context.Background() + + client, err := moq.Dial(ctx, "https://relay.example.com", moq.ClientConfig{}) + if err != nil { + log.Fatal(err) + } + defer client.Close() + + following, err := client.Follow("demos/bbb", nil) + if err != nil { + log.Fatal(err) + } + defer following.Cancel() + + for { + broadcast, err := following.Available(ctx) + if err != nil { + if moq.IsShutdown(err) { + break + } + log.Fatal(err) + } + if broadcast == nil { + fmt.Println("offline") + continue + } + catalog, err := moqmedia.CatalogSnapshot(ctx, broadcast) + if err != nil { + log.Fatal(err) + } + fmt.Printf("online: %+v\n", catalog) + } +} + // Publish a media track to a relay. func ExampleClient_CreateBroadcast() { ctx := context.Background() diff --git a/go/wrapper/moq_test.go b/go/wrapper/moq_test.go index e321d68113b..3f64bdcdc39 100644 --- a/go/wrapper/moq_test.go +++ b/go/wrapper/moq_test.go @@ -1593,7 +1593,7 @@ func TestBroadcastIsReachableOnlyWhileAnnounced(t *testing.T) { t.Fatal(err) } event := nextRoute(t, ctx, announced) - if retracted, ok := event.(moq.AnnounceEventEnd); !ok || retracted.Announce.Prefix != "live" { + if retracted, ok := event.(moq.AnnounceEventAbsent); !ok || retracted.Announce.Prefix != "live" { t.Fatalf("unannounce: event=%+v", event) } if _, err := consumer.Request(ctx, "live", nil); err == nil { @@ -1716,18 +1716,24 @@ func TestAnnouncedExactFilterCapturesEmpty(t *testing.T) { } } -// A broadcast mints its own instance and keeps it across re-announcing; another -// instance is another stream and restarts announce consumers. -func TestAnnouncedInstanceRestarts(t *testing.T) { +// A broadcast mints its own instance and keeps it across re-announcing. Another +// instance keeps the path present in the listing and reaches only followers. +func TestInstanceChangeReachesFollowers(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() origin := newOrigin(t) - announced, err := origin.Consume().Announced(moq.AnnounceOptions{}) + consumer := origin.Consume() + announced, err := consumer.Announced(moq.AnnounceOptions{}) if err != nil { t.Fatal(err) } defer announced.Cancel() + following, err := consumer.Follow("instance", nil) + if err != nil { + t.Fatal(err) + } + defer following.Cancel() publication, err := origin.CreateBroadcast("instance") if err != nil { @@ -1738,24 +1744,30 @@ func TestAnnouncedInstanceRestarts(t *testing.T) { } minted := nextAnnounced(t, ctx, announced).Route.Instance if minted == "" { - t.Fatal("start instance is empty, want a minted one") + t.Fatal("present instance is empty, want a minted one") + } + if _, err := following.Available(ctx); err != nil { + t.Fatal(err) } if err := publication.Announce(moq.AnnounceRoute{Cost: 1}); err != nil { t.Fatal(err) } - update, ok := nextRoute(t, ctx, announced).(moq.AnnounceEventUpdate) - if !ok || update.Announce.Route.Instance != minted { - t.Fatalf("update = %+v, want instance %s", update, minted) - } - stable := "cam" if err := publication.Announce(moq.AnnounceRoute{Instance: &stable, Epoch: 1}); err != nil { t.Fatal(err) } - restart, ok := nextRoute(t, ctx, announced).(moq.AnnounceEventRestart) - if !ok || restart.Announce.Route.Instance != stable { - t.Fatalf("restart = %+v, want instance %s", restart, stable) + if _, err := following.Available(ctx); err != nil { + t.Fatalf("follow after the instance change: %v", err) + } + + // The listing's next event is the absence, so neither re-announce reached it. + if err := publication.Unannounce(); err != nil { + t.Fatal(err) + } + event := nextRoute(t, ctx, announced) + if absent, ok := event.(moq.AnnounceEventAbsent); !ok || absent.Announce.Route.Instance != stable { + t.Fatalf("event = %+v, want absent with instance %s", event, stable) } empty := "" @@ -1764,6 +1776,77 @@ func TestAnnouncedInstanceRestarts(t *testing.T) { } } +// A follower reports the path going offline as nil, then the next broadcast once it returns. +func TestFollowReportsOffline(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) + defer cancel() + + origin := newOrigin(t) + following, err := origin.Consume().Follow("offline", nil) + if err != nil { + t.Fatal(err) + } + defer following.Cancel() + + publication, err := origin.CreateBroadcast("offline") + if err != nil { + t.Fatal(err) + } + if err := publication.Announce(moq.AnnounceRoute{}); err != nil { + t.Fatal(err) + } + if broadcast, err := following.Available(ctx); err != nil || broadcast == nil { + t.Fatalf("first Available = %v, %v; want a broadcast", broadcast, err) + } + + if err := publication.Unannounce(); err != nil { + t.Fatal(err) + } + if broadcast, err := following.Available(ctx); err != nil || broadcast != nil { + t.Fatalf("Available after unannounce = %v, %v; want nil", broadcast, err) + } + + if err := publication.Announce(moq.AnnounceRoute{}); err != nil { + t.Fatal(err) + } + if broadcast, err := following.Available(ctx); err != nil || broadcast == nil { + t.Fatalf("Available after re-announce = %v, %v; want a broadcast", broadcast, err) + } +} + +// A follower pinned to an instance reports another instance taking over as nil. +func TestPinnedFollowReportsTakeoverOffline(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) + defer cancel() + + origin := newOrigin(t) + pinned := "cam" + following, err := origin.Consume().Follow("pinned", &pinned) + if err != nil { + t.Fatal(err) + } + defer following.Cancel() + + publication, err := origin.CreateBroadcast("pinned") + if err != nil { + t.Fatal(err) + } + if err := publication.Announce(moq.AnnounceRoute{Instance: &pinned}); err != nil { + t.Fatal(err) + } + if broadcast, err := following.Available(ctx); err != nil || broadcast == nil { + t.Fatalf("first Available = %v, %v; want a broadcast", broadcast, err) + } + + other := "other" + if err := publication.Announce(moq.AnnounceRoute{Instance: &other, Epoch: 1}); err != nil { + t.Fatal(err) + } + if broadcast, err := following.Available(ctx); err != nil || broadcast != nil { + t.Fatalf("Available after takeover = %v, %v; want nil", broadcast, err) + } +} + // nextRoute returns the next announce event. func nextRoute(t *testing.T, ctx context.Context, announced *moq.AnnounceConsumer) moq.AnnounceEvent { t.Helper() @@ -1778,12 +1861,12 @@ func nextRoute(t *testing.T, ctx context.Context, announced *moq.AnnounceConsume return event } -// nextAnnounced returns the next newly announced route. +// nextAnnounced returns the route of the next path to become present. func nextAnnounced(t *testing.T, ctx context.Context, announced *moq.AnnounceConsumer) moq.Announce { t.Helper() event := nextRoute(t, ctx, announced) - announcedEvent, ok := event.(moq.AnnounceEventStart) + announcedEvent, ok := event.(moq.AnnounceEventPresent) if !ok { t.Fatalf("expected an announcement, got %+v", event) } diff --git a/go/wrapper/origin.go b/go/wrapper/origin.go index 0123431e09d..0abeae0c06f 100644 --- a/go/wrapper/origin.go +++ b/go/wrapper/origin.go @@ -193,7 +193,8 @@ type AnnounceOptions struct { Hidden bool } -// Announced streams routes under a literal prefix matching an optional pattern filter. +// Announced lists the paths under a literal prefix matching an optional pattern filter, as each +// becomes present or absent. To see another publisher instance take a path over, use Follow. func (o *OriginConsumer) Announced(options AnnounceOptions) (*AnnounceConsumer, error) { inner, err := o.inner.Announced(ffi.MoqAnnounceConfig{ Prefix: options.Prefix, @@ -206,9 +207,9 @@ func (o *OriginConsumer) Announced(options AnnounceOptions) (*AnnounceConsumer, return &AnnounceConsumer{inner: inner}, nil } -// Follow follows an exact path, resolving each stream that serves it. A non-nil -// instance follows only that stream: another one taking over is never handed out, -// and the stream is handed out again if it returns after an end. +// Follow follows an exact path, resolving each stream that serves it and reporting +// when it goes offline. A non-nil instance follows only that stream: another one +// taking over reads as offline, and the stream is handed out again if it returns. func (o *OriginConsumer) Follow(path string, instance *string) (*Following, error) { inner, err := o.inner.Follow(path, instance) if err != nil { @@ -232,12 +233,12 @@ func (o *OriginConsumer) Request(ctx context.Context, path string, instance *str return &BroadcastConsumer{inner: inner}, nil } -// AnnounceConsumer is a stream of announce events. +// AnnounceConsumer is a listing of paths, each present or absent. type AnnounceConsumer struct { inner *ffi.MoqAnnounceConsumer } -// Next returns the next announce event, or (nil, nil) when the stream ends. +// Next returns the next path becoming present or absent, or (nil, nil) when the stream ends. func (a *AnnounceConsumer) Next(ctx context.Context) (AnnounceEvent, error) { res, err := bridge.Call(ctx, a.inner.Cancel, a.inner.Next) if err != nil || res == nil { @@ -246,12 +247,12 @@ func (a *AnnounceConsumer) Next(ctx context.Context) (AnnounceEvent, error) { return *res, nil } -// All ranges over announce events until the stream ends or the loop breaks. +// All ranges over the listing's events until the stream ends or the loop breaks. func (a *AnnounceConsumer) All(ctx context.Context) iter.Seq2[AnnounceEvent, error] { return bridge.Seq(ctx, a.Next) } -// Cancel stops the announcement stream. +// Cancel stops the listing. func (a *AnnounceConsumer) Cancel() { a.inner.Cancel() } @@ -261,15 +262,22 @@ type Following struct { inner *ffi.MoqFollowing } -// Available blocks until the next publisher instance serves the broadcast and -// returns its consumer. The first call resolves the instance serving the path once -// one is announced. Each later call waits for another to take over (a restarted -// publisher, or a replacement session) or for the broadcast to return after it went -// offline. Drop what was read from the previous instance then: its tracks end when -// its session does. +// Available blocks until the publisher instance serving the broadcast changes and +// returns its consumer, or (nil, nil) once the path goes offline. The first call +// resolves the instance serving the path once one is announced, and is never nil. +// Each later call returns another instance taking over (a restarted publisher, or a +// replacement session), nil once the path goes offline (or, when following a pinned +// instance, once another instance takes it over), then whatever serves it next. Drop +// what was read from the previous instance then: its tracks end when its route does. func (a *Following) Available(ctx context.Context) (*BroadcastConsumer, error) { - inner, err := bridge.CallHandle(ctx, a.inner.Cancel, a.inner.Available) - if err != nil { + inner, err := bridge.CallHandle(ctx, a.inner.Cancel, func(ctx context.Context) (*ffi.MoqBroadcastConsumer, error) { + res, err := a.inner.Available(ctx) + if err != nil || res == nil { + return nil, err + } + return *res, nil + }) + if err != nil || inner == nil { return nil, err } return &BroadcastConsumer{inner: inner}, nil diff --git a/go/wrapper/reconnect_test.go b/go/wrapper/reconnect_test.go index eca76e3b1fa..398fe4f8a66 100644 --- a/go/wrapper/reconnect_test.go +++ b/go/wrapper/reconnect_test.go @@ -109,7 +109,7 @@ func waitConnects(t *testing.T, ctx context.Context, session *moq.Session, want } } -// awaitAnnouncement drains announcements until an active one names path. +// awaitAnnouncement drains announcements until path is present. func awaitAnnouncement(t *testing.T, ctx context.Context, announced *moq.AnnounceConsumer, path string) { t.Helper() @@ -121,7 +121,7 @@ func awaitAnnouncement(t *testing.T, ctx context.Context, announced *moq.Announc if event == nil { t.Fatalf("announcement stream ended before %q", path) } - if ann, ok := event.(moq.AnnounceEventStart); ok && ann.Announce.Prefix == path { + if ann, ok := event.(moq.AnnounceEventPresent); ok && ann.Announce.Prefix == path { return } } diff --git a/go/wrapper/types.go b/go/wrapper/types.go index 1ee5b6beb73..5f1b7e08c5f 100644 --- a/go/wrapper/types.go +++ b/go/wrapper/types.go @@ -46,21 +46,13 @@ type ( // resolve a path with [OriginConsumer.Request]. Captures is a required slice, // empty when the filter has no wildcards. Announce = ffi.MoqAnnounce - // AnnounceEvent is what an AnnounceConsumer yields: AnnounceEventStart, - // AnnounceEventUpdate, AnnounceEventEnd, AnnounceEventRestart, or AnnounceEventTakeover. + // AnnounceEvent is what an AnnounceConsumer yields: AnnounceEventPresent or AnnounceEventAbsent. + // Another publisher instance taking a path over keeps it present; see it with [OriginConsumer.Follow]. AnnounceEvent = ffi.MoqAnnounceEvent - // AnnounceEventStart reports a route now covering a prefix that had none. - AnnounceEventStart = ffi.MoqAnnounceEventStart - // AnnounceEventUpdate reports the route covering a prefix changing hops or cost. - AnnounceEventUpdate = ffi.MoqAnnounceEventUpdate - // AnnounceEventEnd reports that no route covers a prefix any more, carrying its last route. - AnnounceEventEnd = ffi.MoqAnnounceEventEnd - // AnnounceEventRestart reports another publisher instance now serving a prefix: - // drop what was resolved under it and request afresh. - AnnounceEventRestart = ffi.MoqAnnounceEventRestart - // AnnounceEventTakeover reports a route now covering a prefix that continues what the - // route at Predecessor served beneath it: keep what was resolved through that one. - AnnounceEventTakeover = ffi.MoqAnnounceEventTakeover + // AnnounceEventPresent reports a route now covering a path that was absent. + AnnounceEventPresent = ffi.MoqAnnounceEventPresent + // AnnounceEventAbsent reports that no route covers a path any more, carrying its last route. + AnnounceEventAbsent = ffi.MoqAnnounceEventAbsent // Range is a span of a track to deliver: from Start up to End, or onward when End is nil. // A relative start is the first frame of its group, a relative end includes its whole // group, and an absolute end is exclusive. diff --git a/js/moq-boy/src/element.tsx b/js/moq-boy/src/element.tsx index c66f79d79ca..bf672a128d5 100644 --- a/js/moq-boy/src/element.tsx +++ b/js/moq-boy/src/element.tsx @@ -183,7 +183,7 @@ export default class MoqBoy extends HTMLElement { if (!capture?.isLiteral) continue; const id = capture.text; - if (entry.kind !== "end" && !this.#sessions.has(id)) { + if (entry.kind !== "absent" && !this.#sessions.has(id)) { const config: GameConfig = { sessionId: id, connection: this.connection, @@ -195,7 +195,7 @@ export default class MoqBoy extends HTMLElement { const game = new Game(config); this.#sessions.set(id, game); this.games.set(new Map(this.#sessions)); - } else if (entry.kind === "end") { + } else if (entry.kind === "absent") { const game = this.#sessions.get(id); if (game) { game.close(); diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index 1e1f0cb769a..7116046186f 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -4,6 +4,11 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Announcements are a listing ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`Announce.Event` is `{ kind: "present" | "absent" } & Announce`. Another instance taking a path over keeps it present, and a route's cost or hops changing is not reported, so `start`, `update`, `restart` and `end` are gone. +Map `"start"` to `"present"` and `"end"` to `"absent"`, and drop `"update"` branches. Where you requested the path again on `"restart"`, hold `origin.follow(path)`: its `active` moves to each new instance's broadcast. + ### Opens wait their turn for stream credit ([#7894](https://github.com/moq-dev/moq/pull/7894)) `NoStreamCredit` is removed: a request no longer fails when the peer grants no more streams. Every open waits for credit, and returning credit goes to the open with the highest `sendOrder`. A request's 10s deadline runs only while none of its streams waits to open. diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index 113d31542ac..dba751e5a23 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -21,7 +21,7 @@ export namespace Announce { get synced(): Signals.Getter; } export type Event = { - kind: "start" | "update" | "restart" | "end"; + kind: "present" | "absent"; } & Announce.Announce; export interface Options { covering?: boolean; diff --git a/js/net/bench/broadcasts.ts b/js/net/bench/broadcasts.ts index befb87f20f6..eb38662b229 100644 --- a/js/net/bench/broadcasts.ts +++ b/js/net/bench/broadcasts.ts @@ -45,7 +45,7 @@ for (const scope of [ let notifications = 0; const streams = Array.from({ length: observerCount }, (_, index) => scope === "full-name" || scope === "overlap-only" - ? origin.consume().announced(Path.Pattern.parse("**/*.chat")) + ? origin.consume().routes(Path.Pattern.parse("**/*.chat")) : (scope === "producer-scopes" ? origin.scope( Path.empty(), @@ -57,7 +57,7 @@ for (const scope of [ new Path.Patterns([Path.Pattern.parse(`**/tag-${index}`)]), ) : origin - ).announced( + ).routes( scope === "distinct-scopes" ? Path.Pattern.parse(`room/**/tag-${index}`) : undefined, // These scopes only overlap `room/`, so they measure the covering view. { covering: scope !== "unscoped" }, diff --git a/js/net/bench/forward.ts b/js/net/bench/forward.ts index c0aaa6f030f..42edb11c682 100644 --- a/js/net/bench/forward.ts +++ b/js/net/bench/forward.ts @@ -42,7 +42,7 @@ class FakeSession { }); } - announced(): Announce.Consumer { + routes(): Announce.Routes { const stream = new Announce.Producer(); this.streams.push(stream); // This peer has no initial routes; later announcements are live updates. diff --git a/js/net/bench/refresh.ts b/js/net/bench/refresh.ts index 815fa3e7274..a4ecbbad214 100644 --- a/js/net/bench/refresh.ts +++ b/js/net/bench/refresh.ts @@ -30,7 +30,7 @@ for (const retained of [1, 64, 1024]) ]); try { const announcements = destination.announced(); - while ((await announcements.next())?.kind !== "start") {} + while ((await announcements.next())?.kind !== "present") {} const request = destination.request(Path.from("room")); while (!request.active.peek()) await request.active.changed(); const remote = request.active.peek(); diff --git a/js/net/examples/discovery.ts b/js/net/examples/discovery.ts index b361e02e7ab..eb070025ff1 100644 --- a/js/net/examples/discovery.ts +++ b/js/net/examples/discovery.ts @@ -11,7 +11,7 @@ async function main() { // Discover broadcasts announced by the server for await (const announcement of announced) { - if (announcement.kind === "end" || requests.has(announcement.prefix)) continue; + if (announcement.kind === "absent" || requests.has(announcement.prefix)) continue; console.log("New stream available:", announcement.prefix); // One request per path follows its publisher restarts until closed. diff --git a/js/net/src/announced.test-util.ts b/js/net/src/announced.test-util.ts index daee64e6ad2..6f2b2732d87 100644 --- a/js/net/src/announced.test-util.ts +++ b/js/net/src/announced.test-util.ts @@ -1,12 +1,15 @@ import { Signal } from "@moq/signals"; -import type { Options } from "./announced"; +import type { Options, Routes } from "./announced"; import type { Route } from "./hop"; -import type { Table } from "./origin"; import type * as Path from "./path"; /** A closeable announcement snapshot for tests and benchmarks. @internal */ -export async function announcedView(origin: Pick, scope?: Path.Pattern, options?: Options) { - const stream = origin.announced(scope, options); +export async function announcedView( + origin: { routes(scope?: Path.Pattern, options?: Options): Routes }, + scope?: Path.Pattern, + options?: Options, +) { + const stream = origin.routes(scope, options); const value = new Signal>(new Map()); const done = (async () => { for await (const event of stream) diff --git a/js/net/src/announced.test.ts b/js/net/src/announced.test.ts index f9da2f5b217..eb2ce44fe0e 100644 --- a/js/net/src/announced.test.ts +++ b/js/net/src/announced.test.ts @@ -28,6 +28,20 @@ test("next streams every appended event in order", async () => { expect(await consumer.next()).toEqual({ prefix: p("a"), captures: undefined, kind: "end", route }); }); +test("a listing reports only a path becoming present or absent", async () => { + const producer = new Announce.Producer(); + const listing = Announce.list(producer.consume()); + + const route = Route.default; + producer.append({ prefix: p("a"), captures: undefined, kind: "start", route }); + producer.append({ prefix: p("a"), captures: undefined, kind: "update", route }); + producer.append({ prefix: p("a"), captures: undefined, kind: "restart", route }); + producer.append({ prefix: p("a"), captures: undefined, kind: "end", route }); + + expect((await listing.next())?.kind).toBe("present"); + expect((await listing.next())?.kind).toBe("absent"); +}); + test("the consumer is an async iterable of the same events", async () => { const producer = new Announce.Producer(); const consumer = producer.consume(); @@ -161,7 +175,7 @@ test("the local route wins over a blind request on a no-discovery origin", async }); const tick = () => yieldNow(); -const start = (prefix: string): Announce.Event => ({ +const start = (prefix: string): Announce.RouteEvent => ({ prefix: p(prefix), captures: undefined, kind: "start", @@ -217,7 +231,7 @@ test("behind takes sync back, and closing hands it back for good", () => { test("an origin with no session delivering syncs on its own replay", async () => { const origin = new OriginProducer(); const room = publish(origin, p("room")); - const announced = origin.consume().announced(); + const announced = origin.consume().routes(); expect(await announced.next()).toMatchObject({ prefix: p("room"), kind: "start" }); void announced.next(); await tick(); @@ -232,8 +246,8 @@ test("an origin stream waits on every overlapping initial set in flight", async const room = wire.snapshot(p("room")); const other = wire.snapshot(p("other")); - const scoped = origin.consume().announced(Path.Pattern.subtree(p("room"))); - const all = origin.consume().announced(); + const scoped = origin.consume().routes(Path.Pattern.subtree(p("room"))); + const all = origin.consume().routes(); await tick(); expect(scoped.synced.peek()).toBe(false); expect(all.synced.peek()).toBe(false); @@ -268,7 +282,7 @@ test("an origin stream waits on every overlapping initial set in flight", async test("a snapshot elsewhere never holds a scoped stream", async () => { const origin = new OriginProducer(); const other = wireOf(origin).snapshot(p("other")); - const scoped = origin.consume().announced(Path.Pattern.subtree(p("room"))); + const scoped = origin.consume().routes(Path.Pattern.subtree(p("room"))); await tick(); expect(scoped.synced.peek()).toBe(true); other(); @@ -278,8 +292,8 @@ test("a snapshot elsewhere never holds a scoped stream", async () => { test("a snapshot under a hidden segment holds only a reader that opts in", async () => { const origin = new OriginProducer(); const stats = wireOf(origin).snapshot(p(".stats")); - const all = origin.consume().announced(); - const hidden = origin.consume().announced(Path.Pattern.all(), { hidden: true }); + const all = origin.consume().routes(); + const hidden = origin.consume().routes(Path.Pattern.all(), { hidden: true }); await tick(); expect(all.synced.peek()).toBe(true); expect(hidden.synced.peek()).toBe(false); diff --git a/js/net/src/announced.ts b/js/net/src/announced.ts index 64dca8800b3..20db6927bfc 100644 --- a/js/net/src/announced.ts +++ b/js/net/src/announced.ts @@ -30,16 +30,26 @@ export interface Announce { } /** - * What an announcement stream yields. + * What an announcement listing yields: a path becoming present or absent. * - * `start`: a route now covers the prefix. `update`: the route covering it changed - * hops or cost, in place. `restart`: another instance now serves the prefix, so request it - * afresh; what was already resolved stays on the old one until dropped or its route goes. - * `end`: no route covers it any more. + * `present`: a route now covers the path, which was absent. `absent`: no route covers it any + * more, carrying its last advertised route. Another instance taking a path over keeps it + * present: follow it with the origin's `follow(path)` to see the broadcast change. * * @public */ -export type Event = { kind: "start" | "update" | "restart" | "end" } & Announce; +export type Event = { kind: "present" | "absent" } & Announce; + +/** + * Every change to a route, for code that forwards or routes them. + * + * `start`: a route now covers the prefix. `update`: the route covering it changed hops or cost, + * in place. `restart`: another instance now serves the prefix. `end`: no route covers it any + * more. + * + * @internal + */ +export type RouteEvent = { kind: "start" | "update" | "restart" | "end" } & Announce; /** * Options for an announcement stream. @@ -67,7 +77,7 @@ export interface Options { /** Reactive backing state shared by announcement producers and consumers. */ class AnnounceState { - queue = new Signal([]); + queue = new Signal([]); closed = new Once(); synced = new Signal(false); // The queued events still to read before the stream counts as synced, or undefined @@ -100,9 +110,9 @@ function closeState(state: AnnounceState, abort?: Error) { } /** - * The write side of an announcement stream. + * The write side of a route stream. * - * @public + * @internal */ export class Producer { #state = new AnnounceState(); @@ -116,20 +126,20 @@ export class Producer { } /** A read handle for this announcement stream. */ - consume(): Consumer { - return makeConsumer(this.#state); + consume(): Routes { + return makeRoutes(this.#state); } /** * Whether any consumer still holds the stream: false before the first {@link consume} and - * once the last one {@link Consumer.close}s, which is how a reader cancels it. + * once the last one {@link Routes.close}s, which is how a reader cancels it. */ get wanted(): Getter { return this.#state.wanted; } /** Writes an event to the queue. */ - append(event: Event) { + append(event: RouteEvent) { if (this.#state.closed.peek() !== undefined) throw new Error("announcements are closed"); this.#state.queue.mutate((queue) => { queue.push(event); @@ -167,18 +177,16 @@ export class Producer { } } -// Constructs a Consumer from within this module without exposing a public constructor +// Constructs a Routes from within this module without exposing a public constructor // that would leak the unexported AnnounceState. Assigned in the class's static block. -let makeConsumer: (state: AnnounceState) => Consumer; +let makeRoutes: (state: AnnounceState) => Routes; /** - * The read side of an announcement stream. - * - * Created internally: obtain one from an origin or the connection's `announced(scope)`. + * The read side of a route stream: every change, for code that forwards or routes them. * - * @public + * @internal */ -export class Consumer { +export class Routes { #state: AnnounceState; // Set once this handle closes, ending its own reads and guarding the consumer count. #released = new Signal(false); @@ -195,7 +203,7 @@ export class Consumer { } static { - makeConsumer = (state) => new Consumer(state); + makeRoutes = (state) => new Routes(state); } /** @@ -212,7 +220,7 @@ export class Consumer { } /** The events as they arrive, until the stream closes. */ - async *[Symbol.asyncIterator](): AsyncGenerator { + async *[Symbol.asyncIterator](): AsyncGenerator { for (;;) { const event = await this.next(); if (!event) return; @@ -221,7 +229,7 @@ export class Consumer { } /** Returns the next event, or undefined once the stream or this handle closes. */ - async next(): Promise { + async next(): Promise { const state = this.#state; state.reading = false; settle(state); @@ -253,3 +261,81 @@ export class Consumer { if (--this.#state.consumers === 0) this.#state.wanted.set(false); } } + +// Constructs a listing over a route stream without a public constructor. Declared before the +// class, whose static block assigns it. +let makeConsumer: (routes: Routes) => Consumer; + +/** + * The paths announced under a scope, each reported as it becomes present or absent. + * + * Created internally: obtain one from an origin or the connection's `announced(scope)`. + * + * @public + */ +export class Consumer { + #routes: Routes; + + private constructor(routes: Routes) { + this.#routes = routes; + } + + static { + makeConsumer = (routes) => new Consumer(routes); + } + + /** Settles once the stream closes: `null` on a clean close, or the abort {@link Error}. */ + get closed(): GetPromise { + return this.#routes.closed; + } + + /** + * Whether the listing has delivered its source's initial announcements and the reader has + * taken them, so a path not present by now is absent rather than not yet heard of. + * + * Flips once the reader asks for the event after the last of them, and is true once + * closed. Through a session, the initial set is the peer's view when it answered: exact + * on versions that count it, best-effort on the rest. Reading an origin, it is every + * attached session's initial set, so a reconnect takes it back until the new one lands. + */ + get synced(): Getter { + return this.#routes.synced; + } + + /** The events as they arrive, until the listing closes. */ + async *[Symbol.asyncIterator](): AsyncGenerator { + for (;;) { + const event = await this.next(); + if (!event) return; + yield event; + } + } + + /** Returns the next path that became present or absent, or undefined once the listing or this handle closes. */ + async next(): Promise { + for (;;) { + const event = await this.#routes.next(); + if (!event) return undefined; + const { kind, ...announce } = event; + if (kind === "start") return { kind: "present", ...announce }; + if (kind === "end") return { kind: "absent", ...announce }; + } + } + + /** + * Release this handle, like dropping a Rust handle: the source sees the listing as no + * longer wanted. Idempotent. + */ + close() { + this.#routes.close(); + } +} + +/** + * The listing over a route stream: only paths becoming present or absent. + * + * @internal + */ +export function list(routes: Routes): Consumer { + return makeConsumer(routes); +} diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 517dbe8ea8a..0ca98b10dc6 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -369,7 +369,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { await waitFor(client.auth.grant, (g) => g !== undefined); origin.createBroadcast(Path.from("a/x")).announce(); - const announced = server.announced(); + const announced = server.routes(); const first = await nextRoute(announced); expect(first?.prefix).toBe(Path.from("a/x")); expect(first?.kind).toBe("start"); diff --git a/js/net/src/connection/close.test.ts b/js/net/src/connection/close.test.ts index fc4dbb53761..2111a17e097 100644 --- a/js/net/src/connection/close.test.ts +++ b/js/net/src/connection/close.test.ts @@ -65,7 +65,7 @@ for (const protocol of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18]) { connect({ url, transport: pair.client, consume: destination }), accept({ url, transport: pair.server, publish: source.consume() }), ]); - const announced = destination.announced(); + const announced = destination.routes(); let installed: Installed | undefined; try { expect((await announced.next())?.kind).toBe("start"); diff --git a/js/net/src/connection/established.ts b/js/net/src/connection/established.ts index c4920e6d4ce..9e55e067e25 100644 --- a/js/net/src/connection/established.ts +++ b/js/net/src/connection/established.ts @@ -1,5 +1,6 @@ import type { GetPromise, Getter } from "@moq/signals"; import type * as announce from "../announce.ts"; +import type { Routes } from "../announced.ts"; import type * as Auth from "../auth.ts"; import type * as Path from "../path.ts"; import type { Drain } from "./goaway.ts"; @@ -59,6 +60,9 @@ export interface Established { */ announced(scope?: Path.Pattern, options?: announce.Options): announce.Consumer; + /** Every route change matching `scope`, for code that forwards routes. @internal */ + routes(scope?: Path.Pattern, options?: announce.Options): Routes; + /** * Snapshot the transport's counters, querying it fresh on each call. * diff --git a/js/net/src/connection/forward.test.ts b/js/net/src/connection/forward.test.ts index e53f77a50cd..79a9df2030a 100644 --- a/js/net/src/connection/forward.test.ts +++ b/js/net/src/connection/forward.test.ts @@ -79,7 +79,7 @@ class FakeSession { }); } - announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { + routes(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Routes { const stream = new Announce.Producer(); this.requests.push({ scope: scope.text, hidden: options?.hidden, covering: options?.covering, stream }); if (this.syncedAtOnce) stream.synced(); @@ -137,7 +137,7 @@ function start(stream: Announce.Producer, prefix: string, route: Route = Route.d } /** Every event an origin announcement stream yields, as `kind prefix`. */ -function record(stream: Announce.Consumer): string[] { +function record(stream: Announce.Routes): string[] { const events: string[] = []; void (async () => { for await (const event of stream) events.push(`${event.kind} ${event.prefix}`); @@ -172,7 +172,7 @@ test("a watch opens its request, and the request closes once the linger expires" const session = new FakeSession(); forwardAnnounced(session.session, origin); - const watch = origin.announced(Path.Pattern.subtree(Path.from("room"))); + const watch = origin.routes(Path.Pattern.subtree(Path.from("room"))); await settle(); expect(session.requests.map(({ scope, hidden }) => ({ scope, hidden }))).toEqual([ { scope: "room/**", hidden: true }, @@ -183,7 +183,7 @@ test("a watch opens its request, and the request closes once the linger expires" await advance(LINGER - 1); expect(session.open()).toEqual(["room/**"]); - const again = origin.announced(Path.Pattern.subtree(Path.from("room"))); + const again = origin.routes(Path.Pattern.subtree(Path.from("room"))); await advance(LINGER); expect(session.open()).toEqual(["room/**"]); expect(session.requests.length).toBe(1); @@ -203,15 +203,15 @@ test("a watch arriving as the linger expires opens a fresh request", async () => const session = new FakeSession(); forwardAnnounced(session.session, origin); - const watch = origin.announced(Path.Pattern.subtree(Path.from("room"))); + const watch = origin.routes(Path.Pattern.subtree(Path.from("room"))); await settle(); watch.close(); await settle(); // The expiry closes the old request, and the next watch lands before its stream winds down. - let again: Announce.Consumer | undefined; + let again: Announce.Routes | undefined; session.onClose = () => { - again ??= origin.announced(Path.Pattern.subtree(Path.from("room"))); + again ??= origin.routes(Path.Pattern.subtree(Path.from("room"))); }; await advance(LINGER); expect(again).toBeDefined(); @@ -285,7 +285,7 @@ test("a pattern watches its literal head", async () => { const session = new FakeSession(); forwardAnnounced(session.session, origin); - const chat = origin.announced(Path.Pattern.parse("room/*/chat")); + const chat = origin.routes(Path.Pattern.parse("room/*/chat")); await settle(); expect(session.open()).toEqual(["room/**"]); @@ -299,8 +299,8 @@ test("a narrower watch rides an open broader request and keeps it open", async ( const session = new FakeSession(); forwardAnnounced(session.session, origin); - const broad = origin.announced(Path.Pattern.subtree(Path.from("a"))); - const narrow = origin.announced(Path.Pattern.subtree(Path.from("a/b"))); + const broad = origin.routes(Path.Pattern.subtree(Path.from("a"))); + const narrow = origin.routes(Path.Pattern.subtree(Path.from("a/b"))); const events = record(narrow); await settle(); expect(session.open()).toEqual(["a/**"]); @@ -333,14 +333,14 @@ test("a narrower request stays open until the broader one covering it goes", asy const session = new FakeSession(); forwardAnnounced(session.session, origin); - const narrow = origin.announced(Path.Pattern.subtree(Path.from("a/b"))); + const narrow = origin.routes(Path.Pattern.subtree(Path.from("a/b"))); await settle(); start(session.stream("a/b/**"), "a/b/x"); await settle(); // A broader watch opens its own request, which has yet to answer when the narrower watch // leaves. The narrower request stays open, so the route never retracts. - const broad = origin.announced(Path.Pattern.subtree(Path.from("a"))); + const broad = origin.routes(Path.Pattern.subtree(Path.from("a"))); const events = record(broad); await settle(); narrow.close(); @@ -365,8 +365,8 @@ test("an exclusive session closes every request beneath a broader one before it const session = new FakeSession({ exclusive: true }); forwardAnnounced(session.session, origin); - const narrow = origin.announced(Path.Pattern.subtree(Path.from("a/b"))); - const lingering = origin.announced(Path.Pattern.subtree(Path.from("a/c"))); + const narrow = origin.routes(Path.Pattern.subtree(Path.from("a/b"))); + const lingering = origin.routes(Path.Pattern.subtree(Path.from("a/c"))); await settle(); start(session.stream("a/b/**"), "a/b/x"); lingering.close(); @@ -375,7 +375,7 @@ test("an exclusive session closes every request beneath a broader one before it // Watched or lingering, each narrower request closes for the broader one, on purpose: the // session stays discovering, and the route returns once the broader request reports it. - const broad = origin.announced(Path.Pattern.subtree(Path.from("a"))); + const broad = origin.routes(Path.Pattern.subtree(Path.from("a"))); const events = record(broad); await settle(); expect(session.open()).toEqual(["a/**"]); @@ -409,11 +409,11 @@ for (const [name, instances] of [ const session = new FakeSession(); forwardAnnounced(session.session, origin); - const narrow = origin.announced(Path.Pattern.subtree(Path.from("a/b"))); + const narrow = origin.routes(Path.Pattern.subtree(Path.from("a/b"))); await settle(); start(session.stream("a/b/**"), "a/b/x", { ...Route.default, instance: instances[0] }); await settle(); - const broad = origin.announced(Path.Pattern.subtree(Path.from("a"))); + const broad = origin.routes(Path.Pattern.subtree(Path.from("a"))); const events = record(broad); await settle(); start(session.stream("a/**"), "a/b/x", { ...Route.default, instance: instances[1] }); @@ -438,8 +438,8 @@ test("a reconnect re-opens every live watch and skips a lingering one", async () const first = new FakeSession(); forwardAnnounced(first.session, origin); - const kept = origin.announced(Path.Pattern.subtree(Path.from("kept"))); - const dropped = origin.announced(Path.Pattern.subtree(Path.from("dropped"))); + const kept = origin.routes(Path.Pattern.subtree(Path.from("kept"))); + const dropped = origin.routes(Path.Pattern.subtree(Path.from("dropped"))); await settle(); expect(first.open()).toEqual(["kept/**", "dropped/**"]); @@ -483,7 +483,7 @@ test("a refused announce request downgrades the session once, and later watches expect(plain.active.peek()).toBeDefined(); expect(session.consumes(path)).toBe(1); - const later = origin.announced(Path.Pattern.subtree(Path.from("other"))); + const later = origin.routes(Path.Pattern.subtree(Path.from("other"))); await settle(); expect(session.requests.length).toBe(1); @@ -536,7 +536,7 @@ test("a session dying does not downgrade discovery for the next one", async () = const origin = new OriginProducer(); const first = new FakeSession(); forwardAnnounced(first.session, origin); - const watch = origin.announced(); + const watch = origin.routes(); await settle(); expect(origin.discovery.peek()).toBe(true); @@ -563,7 +563,7 @@ test("a request replaced across one coalesced wakeup still gets answered", async forwardAnnounced(session.session, origin); // Refusing discovery downgrades the session, so plain requests take its blind answers. - const listing = origin.announced(Path.Pattern.subtree(path)); + const listing = origin.routes(Path.Pattern.subtree(path)); await settle(); session.stream("wanted/**").close(new Error("namespace rejected")); await settle(); @@ -600,15 +600,15 @@ test("a scoped origin narrows a root watch to its disjoint literal heads", async const session = new FakeSession(); forwardAnnounced(session.session, scoped); - const all = origin.announced(); + const all = origin.routes(); await settle(); expect(session.open()).toEqual(["other/**", "room/**"]); all.close(); // A watch inside a head rides the head's open request, and one outside the scope asks for // nothing. - let inside = origin.announced(Path.Pattern.subtree(Path.from("room/alice/chat"))); - const outside = origin.announced(Path.Pattern.subtree(Path.from("elsewhere"))); + let inside = origin.routes(Path.Pattern.subtree(Path.from("room/alice/chat"))); + const outside = origin.routes(Path.Pattern.subtree(Path.from("elsewhere"))); await advance(LINGER); expect(session.open()).toEqual(["room/**"]); @@ -616,7 +616,7 @@ test("a scoped origin narrows a root watch to its disjoint literal heads", async inside.close(); await advance(LINGER); expect(session.open()).toEqual([]); - inside = origin.announced(Path.Pattern.subtree(Path.from("room/alice/chat"))); + inside = origin.routes(Path.Pattern.subtree(Path.from("room/alice/chat"))); await settle(); expect(session.open()).toEqual(["room/alice/chat/**"]); @@ -632,7 +632,7 @@ test("a scoped session filters discovered routes and requests under its root", a const session = new FakeSession(); forwardAnnounced(session.session, scoped); - const all = origin.announced(); + const all = origin.routes(); const events = record(all); await settle(); // Session-relative: the scope's root is the session's. @@ -665,8 +665,8 @@ test("one failed request leaves the other routes live until the session ends", a const session = new FakeSession(); forwardAnnounced(session.session, origin); - const a = origin.announced(Path.Pattern.subtree(Path.from("a"))); - const b = origin.announced(Path.Pattern.subtree(Path.from("b"))); + const a = origin.routes(Path.Pattern.subtree(Path.from("a"))); + const b = origin.routes(Path.Pattern.subtree(Path.from("b"))); await settle(); start(session.stream("a/**"), "a"); start(session.stream("b/**"), "b"); @@ -694,7 +694,7 @@ test("an origin watcher syncs with the session's initial set, not before", async const session = new FakeSession(); forwardAnnounced(session.session, origin); - const watch = origin.announced(Path.Pattern.subtree(Path.from("room"))); + const watch = origin.routes(Path.Pattern.subtree(Path.from("room"))); const events = record(watch); await settle(); expect(watch.synced.peek()).toBe(false); @@ -720,7 +720,7 @@ test("a session ending before its initial set lands stops holding the watcher", const session = new FakeSession(); forwardAnnounced(session.session, origin); - const watch = origin.announced(); + const watch = origin.routes(); record(watch); await settle(); expect(watch.synced.peek()).toBe(false); @@ -739,7 +739,7 @@ test("a request synced as it opens releases the watcher", async () => { session.syncedAtOnce = true; forwardAnnounced(session.session, origin); - const watch = origin.announced(); + const watch = origin.routes(); record(watch); await settle(); expect(session.requests).toHaveLength(1); diff --git a/js/net/src/connection/forward.ts b/js/net/src/connection/forward.ts index b93b9f55bad..99ea9c338f7 100644 --- a/js/net/src/connection/forward.ts +++ b/js/net/src/connection/forward.ts @@ -24,7 +24,7 @@ const LINGER = Milli(2000); /** One announce request open on the session, and the routes it currently reports. */ interface Interest { - readonly announced: Announce.Consumer; + readonly announced: Announce.Routes; readonly reported: Set; /** The pending close, set while nothing at or beneath the prefix is watched. */ linger?: () => void; @@ -89,7 +89,7 @@ export function forwardAnnounced(conn: Established, origin: OriginProducer): voi void serveRequests(conn, origin); - const report = (interest: Interest, event: Announce.Event) => { + const report = (interest: Interest, event: Announce.RouteEvent) => { const { prefix, route } = event; if (event.kind === "end") { unreport(interest, prefix); @@ -173,7 +173,7 @@ export function forwardAnnounced(conn: Established, origin: OriginProducer): voi // Hidden and covering routes are mirrored too; each local reader opts in on its own. const interest: Interest = { - announced: conn.announced(Path.Pattern.subtree(prefix), { hidden: true, covering: true }), + announced: conn.routes(Path.Pattern.subtree(prefix), { hidden: true, covering: true }), reported: new Set(), closing: false, held: new Map(), diff --git a/js/net/src/connection/pool.test.ts b/js/net/src/connection/pool.test.ts index f849baf5145..d62e72772e2 100644 --- a/js/net/src/connection/pool.test.ts +++ b/js/net/src/connection/pool.test.ts @@ -264,14 +264,14 @@ test("a borrowed connection origin exposes live routes and dynamic serving", asy const prefix = Path.from("on-demand"); const route = table.requestsRoute(prefix); await advance(); - expect(announcement).toMatchObject({ kind: "start", prefix }); + expect(announcement).toMatchObject({ kind: "present", prefix }); let withdrawal: unknown; void live.next().then((event) => { withdrawal = event; }); route.close(); await advance(); - expect(withdrawal).toMatchObject({ kind: "end", prefix }); + expect(withdrawal).toMatchObject({ kind: "absent", prefix }); live.close(); } finally { handle.close(); @@ -605,7 +605,7 @@ test("a handle's announcements sync with the shared origin", async () => { stubTransports(); const handle = new Connection({ url }); - const watch = handle.announced(); + const watch = handle.routes(); expect(watch.synced.peek()).toBe(false); await advance(); diff --git a/js/net/src/connection/pool.ts b/js/net/src/connection/pool.ts index 795af807a51..4ca50528787 100644 --- a/js/net/src/connection/pool.ts +++ b/js/net/src/connection/pool.ts @@ -7,7 +7,7 @@ import { Milli, Nano } from "@moq/clock"; import { Effect, type GetPromise, type Getter, Once, Signal } from "@moq/signals"; import type * as Announce from "../announce.ts"; -import { Producer as AnnounceProducer } from "../announced.ts"; +import { Producer as AnnounceProducer, list, type Routes } from "../announced.ts"; import type * as Auth from "../auth.ts"; import type { Handle, Viewers } from "../bandwidth.ts"; import * as Origin from "../origin.ts"; @@ -361,6 +361,15 @@ export class Connection { * the old relay's origin, then the new one's arrivals stream in. */ announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { + return list(this.routes(scope, options)); + } + + /** + * Every route change matching `scope`, spanning reconnects like {@link announced}. + * + * @internal + */ + routes(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Routes { const producer = new AnnounceProducer(); const consumer = producer.consume(); @@ -377,7 +386,7 @@ export class Connection { return; } // A URL switch retracts what the old origin announced. - pumpAnnounced(effect, origin.announced(scope, options), producer); + pumpAnnounced(effect, origin.routes(scope, options), producer); }); closeUnwanted(producer); diff --git a/js/net/src/connection/reload.test.ts b/js/net/src/connection/reload.test.ts index 5d79d593eec..80e37e1e321 100644 --- a/js/net/src/connection/reload.test.ts +++ b/js/net/src/connection/reload.test.ts @@ -592,7 +592,7 @@ test("origins span reconnects: local re-announces, remote re-populates", async ( const pair = createMockTransportPair(Lite.ALPN_05); const saw = new OriginProducer(); // The server watches everything, so it asks the client for announcements. - const watching = saw.announced(); + const watching = saw.routes(); const serverOrigin = new OriginProducer(); void accept(pair.server, url, { publish: serverOrigin.consume(), consume: saw }).then((session) => { publish(serverOrigin, Path.from("remote")); @@ -612,7 +612,7 @@ test("origins span reconnects: local re-announces, remote re-populates", async ( }); const reader = subscribeOrigin.consume(); // A watch held across the reconnect: each session re-opens its announce request. - const watching = reader.announced(); + const watching = reader.routes(); try { // First session: the server's broadcast lands in the client origin, and the client's @@ -1055,7 +1055,7 @@ test("closing an announce consumer during upstream teardown does not append retr test("announced accepts a non-prefix scope before any session exists", () => { const connection = new Connection({ enabled: false }); try { - const announced = connection.announced(Path.Pattern.parse("room/*")); + const announced = connection.routes(Path.Pattern.parse("room/*")); announced.close(); } finally { connection.close(); @@ -1099,7 +1099,7 @@ test("a watcher syncs with the server's initial set, and again after a reconnect }; const reload = viaOrigin ? new Reload({ ...props, consume: clientOrigin }) : undefined; const connection = viaOrigin ? undefined : new Connection({ ...props, share: false }); - const watching = connection ? connection.announced() : clientOrigin.announced(); + const watching = connection ? connection.routes() : clientOrigin.routes(); const seen = new Set(); void (async () => { for await (const event of watching) { diff --git a/js/net/src/connection/reload.ts b/js/net/src/connection/reload.ts index db9ed20d714..8074b972720 100644 --- a/js/net/src/connection/reload.ts +++ b/js/net/src/connection/reload.ts @@ -874,7 +874,7 @@ export class Reload { * * @internal */ -export function pumpAnnounced(effect: Effect, upstream: Announce.Consumer, producer: Announce.Producer) { +export function pumpAnnounced(effect: Effect, upstream: Announce.Routes, producer: Announce.Producer) { // The last event per active route rides along for its retraction. const active = new Map(); let live = true; diff --git a/js/net/src/connection/scope.test.ts b/js/net/src/connection/scope.test.ts index c4d8e4fe784..c59a729acf1 100644 --- a/js/net/src/connection/scope.test.ts +++ b/js/net/src/connection/scope.test.ts @@ -31,7 +31,7 @@ for (const protocol of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18]) { connect({ url, transport: pair.client, consume }), accept({ url, transport: pair.server, publish: publish.consume() }), ]); - const announced = destination.announced(undefined, { hidden: true }); + const announced = destination.routes(undefined, { hidden: true }); try { const received: string[] = []; for (let index = 0; index < 3; index++) { diff --git a/js/net/src/goaway-requests.test.ts b/js/net/src/goaway-requests.test.ts index 8de6f9da638..bc2df5b5235 100644 --- a/js/net/src/goaway-requests.test.ts +++ b/js/net/src/goaway-requests.test.ts @@ -131,10 +131,10 @@ async function handover(kind: "lite" | "ietf"): Promise { accept({ transport: pair.server, url, publish: publish.consume() }), ]); - const announced = client.announced(); + const announced = client.routes(); // Watching the origin has the session discover routes into it. Request once the route // lands, so the request resolves through the table rather than blind. - const watching = consume.announced(); + const watching = consume.routes(); expect((await watching.next())?.kind).toBe("start"); const request = consume.request(Path.from("room")); let replacement: Awaited> | undefined; @@ -187,7 +187,7 @@ async function handover(kind: "lite" | "ietf"): Promise { group.close(); // A new announce-interest opens too, and sees the route at the drain cost. - const interest = client.announced(); + const interest = client.routes(); const drained = await interest.next(); expect(drained?.route.cost).toBe(DRAIN); interest.close(); @@ -195,7 +195,7 @@ async function handover(kind: "lite" | "ietf"): Promise { expect(pair.client.sendStreams.bidi.length).toBeGreaterThan(opened); const drainingOpened = pair.client.sendStreams.bidi.length; - const routes = consume.consume().announced(); + const routes = consume.consume().routes(); expect((await routes.next())?.kind).toBe("start"); const pair2 = createMockTransportPair(protocol); @@ -276,7 +276,7 @@ test("announcing and subscribing after GOAWAY still open streams", async () => { expect(opened).toBe(true); opened = false; - const announced = subscriber.announced(); + const announced = subscriber.routes(); await waitUntil(() => opened); announced.close(); } @@ -319,9 +319,9 @@ async function routeDeath(kind: "lite" | "ietf", after: boolean): Promise accept({ transport: pair.server, url, publish: publish.consume() }), ]); - const watching = consume.announced(); + const watching = consume.routes(); const request = consume.request(Path.from("room")); - let routes: ReturnType | undefined; + let routes: ReturnType | undefined; let replacement: Awaited> | undefined; let replacementServer: Awaited> | undefined; @@ -337,7 +337,7 @@ async function routeDeath(kind: "lite" | "ietf", after: boolean): Promise else await sendLiteGoaway(pair.server); await client.draining; - routes = consume.announced(); + routes = consume.routes(); expect((await routes.next())?.kind).toBe("start"); const pair2 = createMockTransportPair(protocol); [replacement, replacementServer] = await Promise.all([ diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index abd10cb73b5..e5aa4d97e5e 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -1,6 +1,6 @@ import { Nano, Second } from "@moq/clock"; import { type GetPromise, type Getter, Once, race, Signal } from "@moq/signals"; -import type * as announce from "../announced.ts"; +import * as announce from "../announced.ts"; import type * as Auth from "../auth.ts"; import { AuthSession } from "../auth_session.ts"; import type { Established } from "../connection/established.ts"; @@ -313,9 +313,14 @@ export class Connection implements Established { } } - /** Gets an announced reader for `scope`; see {@link Established.announced}. */ + /** The paths announced matching `scope`, each present or absent; see {@link Established.announced}. */ announced(scope?: Path.Pattern, options?: announce.Options): announce.Consumer { - return this.#subscriber.announced(scope, options); + return announce.list(this.#subscriber.routes(scope, options)); + } + + /** Every route change matching `scope`, for code that forwards routes. @internal */ + routes(scope?: Path.Pattern, options?: announce.Options): announce.Routes { + return this.#subscriber.routes(scope, options); } /** Accepts request streams and dispatches them. */ diff --git a/js/net/src/ietf/goaway.test.ts b/js/net/src/ietf/goaway.test.ts index c93c19eb45d..7ceb767ae72 100644 --- a/js/net/src/ietf/goaway.test.ts +++ b/js/net/src/ietf/goaway.test.ts @@ -209,7 +209,7 @@ for (const [version, alpn] of [ version, client: true, }); - const announced = connection.announced(); + const announced = connection.routes(); try { const request = await Stream.accept(pair.server, version); if (!request) throw new Error("no namespace subscription"); diff --git a/js/net/src/ietf/peer-violations.test.ts b/js/net/src/ietf/peer-violations.test.ts index e2e770d4498..27b4e41a37a 100644 --- a/js/net/src/ietf/peer-violations.test.ts +++ b/js/net/src/ietf/peer-violations.test.ts @@ -146,7 +146,7 @@ for (const [version, alpn] of versions) { }); const acquiring = spyOn(Slots.prototype, "tryAcquire"); try { - const announced = subscriber.announced(); + const announced = subscriber.routes(); await announced.closed; expect(acquiring).toHaveBeenCalledTimes(3); // a, b, c; neither DONE nor the repeat takes another slot. expect((await pair.client.closed).closeCode).toBe(SessionCode.TooManyRequests); @@ -169,7 +169,7 @@ for (const [version, alpn] of versions) { cluster: { self, peer }, limits: { announces: 1 }, }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const stream = await Stream.accept(pair.client, version); if (!stream) throw new Error("missing namespace stream"); const answer = async (stream: Stream) => { @@ -200,7 +200,7 @@ for (const [version, alpn] of versions) { } else { stream.abort(new StreamError(StreamCode.Cancel)); await announced.closed; - const next = subscriber.announced(); + const next = subscriber.routes(); const replacement = await Stream.accept(pair.client, version); if (!replacement) throw new Error("missing replacement namespace stream"); await answer(replacement); @@ -223,7 +223,7 @@ for (const [version, alpn] of versions) { quic: pair.server, limits: { announces: 1 }, }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); try { const inline = await Stream.accept(pair.client, version); if (!inline) throw new Error("missing namespace stream"); diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index 0de13688cbf..9e53c2ae68b 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -65,7 +65,7 @@ test("every peer is asked", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session }); - subscriber.announced(); + subscriber.routes(); expect(await nextStream(pair.client)).toBeDefined(); }); @@ -80,7 +80,7 @@ test("an unsolicited announcement lands", async () => { const session = new NativeSession(pair.server, version, true); const subscriber = new Subscriber({ session }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); // The question we asked, which this peer never answers. expect(await nextStream(pair.client)).toBeDefined(); @@ -128,7 +128,7 @@ test("an unsolicited announcement lands", async () => { test("an advertisement with no path is anonymous", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const subscriber = new Subscriber({ session: new NativeSession(pair.server, VERSION, true) }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); expect(await nextStream(pair.client)).toBeDefined(); const stream = await Stream.open(pair.server, { version: VERSION }); @@ -172,7 +172,7 @@ async function inlineNamespace( * before it on the same stream have been read. Announcing an already-announced path is * silent by design, so it cannot be waited on directly. */ -async function syncInline(stream: Stream, announced: announce.Consumer, cluster?: Cluster.Advert): Promise { +async function syncInline(stream: Stream, announced: announce.Routes, cluster?: Cluster.Advert): Promise { await inlineNamespace(stream, Path.from("sentinel"), cluster); expect(await announced.next()).toMatchObject({ prefix: Path.from("sentinel"), @@ -193,7 +193,7 @@ test("an announcement survives the first of its two sources ending", async () => const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); // Unsolicited first, then the same path inline on the subscription. @@ -225,8 +225,8 @@ test("announced lists literal matches unless covering asks for overlapping route const pair = createMockTransportPair(ALPN.DRAFT_18); const subscriber = new Subscriber({ session: new NativeSession(pair.server, VERSION, true) }); const scope = Path.Pattern.parse("**/*.chat"); - const listed = subscriber.announced(scope); - const covering = subscriber.announced(scope, { covering: true }); + const listed = subscriber.routes(scope); + const covering = subscriber.routes(scope, { covering: true }); // `room` could serve `room/lobby.chat`, but its own name is no match. for (const [requestId, path] of [ @@ -256,7 +256,7 @@ test("an announcement ends once its last source does", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); const request = await Stream.open(pair.server, { version: VERSION }); @@ -293,9 +293,9 @@ test("a subscription that dies releases what it advertised", async () => { const subscriber = new Subscriber({ session }); // Two feeds, so one outlives the stream that carries the advertisement. - const doomed = subscriber.announced(); + const doomed = subscriber.routes(); const streamA = await acceptSubscribeNamespace(pair.client); - const survivor = subscriber.announced(); + const survivor = subscriber.routes(); await acceptSubscribeNamespace(pair.client); await inlineNamespace(streamA, Path.from("orphan")); @@ -319,7 +319,7 @@ test("an entry buffered past a consumer close does not pin the path", async () = const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); // Both entries land in one chunk, so the second is buffered while the first is being @@ -334,7 +334,7 @@ test("an entry buffered past a consumer close does not pin the path", async () = // Whatever the loop managed to attach, the stream gave back on its way out. await sleep(Nano.fromMilli(Milli(50))); - const fresh = subscriber.announced(); + const fresh = subscriber.routes(); await acceptSubscribeNamespace(pair.client); const seeded = await Promise.race([fresh.next(), sleep(Nano.fromMilli(Milli(250))).then(() => "nothing" as const)]); expect(seeded).toBe("nothing"); @@ -354,7 +354,7 @@ test("an inline NAMESPACE that looped back through us is dropped", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); // Ours coming back, then someone else's. Only the second is news. @@ -372,7 +372,7 @@ test("an inline NAMESPACE that starts looping back is retracted", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); await inlineNamespace(subscription, Path.from("theirs"), { hops: [PEER], cost: 0n }); @@ -392,7 +392,7 @@ test("a repeated NAMESPACE reprices in place", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); await inlineNamespace(subscription, Path.from("theirs"), { hops: [PEER], cost: 4n }); @@ -416,7 +416,7 @@ test("a PUBLISH_NAMESPACE that looped back through us is refused", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); const request = await Stream.open(pair.server, { version: VERSION }); @@ -450,7 +450,7 @@ test("a NAMESPACE missing its hop path closes the session", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - subscriber.announced(); + subscriber.routes(); const subscription = await acceptSubscribeNamespace(pair.client); // The base form, which a negotiated session must never send. @@ -518,7 +518,7 @@ test("a PUBLISH_NAMESPACE update that starts looping back is detached", async () const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await acceptSubscribeNamespace(pair.client); // Published by 11, relayed by the peer. Every update keeps that publisher. @@ -574,7 +574,7 @@ test("a PUBLISH_NAMESPACE repricing is acknowledged in place", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await acceptSubscribeNamespace(pair.client); const request = await Stream.open(pair.server, { version: VERSION }); @@ -623,7 +623,7 @@ test("a PUBLISH_NAMESPACE update that changes the publisher applies in place", a const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await acceptSubscribeNamespace(pair.client); const request = await Stream.open(pair.server, { version: VERSION }); @@ -685,7 +685,7 @@ test("a repeated PUBLISH_NAMESPACE is a protocol violation", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, quic: pair.server, cluster: { self: SELF, peer: PEER } }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await acceptSubscribeNamespace(pair.client); const advert = new PublishNamespace({ @@ -797,7 +797,7 @@ test("a refused SUBSCRIBE or SUBSCRIBE_NAMESPACE ends its stream cleanly", async peer.writer.close(); }; - const announced = subscriber.announced(); + const announced = subscriber.routes(); const namespace = await nextStream(pair.client); if (!namespace) throw new Error("no SUBSCRIBE_NAMESPACE was sent"); expect(await namespace.reader.u53()).toBe(SubscribeNamespace.id); @@ -1393,8 +1393,8 @@ test("every object in a chunk reaches the reader before it wakes", async () => { test("local readers filter hidden unsolicited namespaces from a legacy peer", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const subscriber = new Subscriber({ session: new NativeSession(pair.server, VERSION, true) }); - const plain = subscriber.announced(); - const opted = subscriber.announced(undefined, { hidden: true }); + const plain = subscriber.routes(); + const opted = subscriber.routes(undefined, { hidden: true }); const handlers: Promise[] = []; const streams: Stream[] = []; for (const path of [".stats/node", "visible"]) { @@ -1516,7 +1516,7 @@ test("an object datagram is a datagram group", async () => { test("a more specific unnamed announcement replaces the consumed source", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const subscriber = new Subscriber({ session: new NativeSession(pair.server, VERSION, true) }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await acceptSubscribeNamespace(pair.client); const pool = Path.from("pool"); const job = Path.from("pool/job"); @@ -1589,17 +1589,17 @@ test("a request overlapping one on its way out waits for the peer to end it", as const session = new NativeSession(pair.server, version, true); const subscriber = new Subscriber({ session }); - const narrow = subscriber.announced(Path.Pattern.subtree(Path.from("a/b"))); + const narrow = subscriber.routes(Path.Pattern.subtree(Path.from("a/b"))); const stream = await acceptSubscribeNamespace(pair.client, version); await inlineNamespace(stream, Path.from("x"), undefined, version); expect(await narrow.next()).toMatchObject({ prefix: Path.from("a/b/x"), kind: "start" }); // A request still open is no blocker, and hears the narrower one's route go at once. - const watcher = subscriber.announced(Path.Pattern.subtree(Path.from("a/b"))); + const watcher = subscriber.routes(Path.Pattern.subtree(Path.from("a/b"))); const watching = await acceptSubscribeNamespace(pair.client, version); expect(await watcher.next()).toMatchObject({ prefix: Path.from("a/b/x"), kind: "start" }); narrow.close(); - const broad = subscriber.announced(Path.Pattern.subtree(Path.from("a"))); + const broad = subscriber.routes(Path.Pattern.subtree(Path.from("a"))); expect(await watcher.next()).toMatchObject({ prefix: Path.from("a/b/x"), kind: "end" }); // Draft 22 cancels with a reset, and the broader request waits on the peer's half. @@ -1663,7 +1663,7 @@ test("counted NAMESPACEs sync", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session, activeCount: true }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); const subscription = await answerCounted(pair.client, 2); await inlineNamespace(subscription, Path.from("a")); expect(await announced.next()).toMatchObject({ prefix: Path.from("a"), kind: "start" }); @@ -1688,7 +1688,7 @@ test("an uncounted answer syncs on the OK", async () => { const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await yieldTask(); expect(announced.synced.peek()).toBe(false); await acceptSubscribeNamespace(pair.client); @@ -1709,7 +1709,7 @@ test("an ACTIVE_COUNT that does not match the negotiation closes the session", a const warn = spyOn(console, "warn").mockImplementation(() => void 0); onTestFinished(() => warn.mockRestore()); - subscriber.announced(); + subscriber.routes(); await answerCounted(pair.client, count); await violationClosed(pair); } @@ -1724,7 +1724,7 @@ test("a namespace subscription closes the session once over a violation", async onTestFinished(() => warn.mockRestore()); const close = spyOn(pair.server, "close"); - subscriber.announced(); + subscriber.routes(); await answerCounted(pair.client, undefined); await violationClosed(pair); await yieldTask(); @@ -1740,7 +1740,7 @@ test.each(["done", "fin"] as const)("a counted set cut short by %s closes the se const warn = spyOn(console, "warn").mockImplementation(() => void 0); onTestFinished(() => warn.mockRestore()); - subscriber.announced(); + subscriber.routes(); const subscription = await answerCounted(pair.client, 2); await inlineNamespace(subscription, Path.from("a")); if (cut === "done") { diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 919ac16ff60..f2fde65d173 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -330,7 +330,7 @@ export class Subscriber { * The request goes out once every overlapping request already closed here is off the * wire, so a peer that refuses overlapping requests never sees this one beside them. */ - announced(scope: Path.Pattern = Path.Pattern.all(), options?: announce.Options): announce.Consumer { + routes(scope: Path.Pattern = Path.Pattern.all(), options?: announce.Options): announce.Routes { // The wire speaks announce interest by prefix. const prefix = scopeHead(scope); const filter: Asking = { diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index df0c5c764e9..d36509d4ed5 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -1,7 +1,7 @@ import { afterEach, expect, mock, onTestFinished, spyOn, test } from "bun:test"; import { Instant, Manual, Milli, Nano, now, sleep, yieldNow } from "@moq/clock"; import type { Getter } from "@moq/signals"; -import type * as Announce from "./announce.ts"; +import type * as Announce from "./announced.ts"; import { type Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; import { type AcceptProps, @@ -124,14 +124,14 @@ async function runPublishSubscribeFlow(protocol: string, version?: number) { })(); // Client discovers announced broadcast - const announced = client.announced(); + const announced = client.routes(); const entry = await announced.next(); if (!entry) throw new Error("expected entry"); expect(entry.prefix).toBe("test" as Path.Valid); expect(entry.kind).toBe("start"); // Scoped discovery only echoes the suffix on the wire, but presents the whole path. - const prefixed = client.announced(Path.Pattern.subtree(Path.from("root"))); + const prefixed = client.routes(Path.Pattern.subtree(Path.from("root"))); const prefixedEntry = await prefixed.next(); if (!prefixedEntry) throw new Error("expected prefixed entry"); expect(prefixedEntry.prefix).toBe("root/child" as Path.Valid); @@ -677,7 +677,7 @@ test("integration: lite draft-06 announce lifecycle", async () => { // Announced before the client asks, so it can ride the initial set. const first = publish(origin, Path.from("first")); - const announced = client.announced(); + const announced = client.routes(); let entry = await announced.next(); if (!entry) throw new Error("expected announce"); expect(entry.prefix).toBe("first" as Path.Valid); @@ -713,7 +713,7 @@ test("integration: lite draft-06 announce lifecycle", async () => { }); /** Collect announced prefixes until `until` arrives. */ -async function announcedUntil(announced: Announce.Consumer, until: string) { +async function announcedUntil(announced: Announce.Routes, until: string) { const seen: string[] = []; while (!seen.includes(until)) { const entry = await withTimeout(announced.next(), Nano.fromMilli(Milli(1000)), `waiting for ${until}`); @@ -742,17 +742,17 @@ for (const [protocol, carriesOptIn] of [ const hidden = publish(origin, Path.from(".x/y")); const visible = publish(origin, Path.from("visible")); - const plain = client.announced(); + const plain = client.routes(); expect(await announcedUntil(plain, "visible")).toEqual(["visible"]); // An IETF reader shares the session's table, so `visible` may land before the // opted-in request's own answer; wait on the hidden path itself where it is due. - const opted = client.announced(undefined, { hidden: true }); + const opted = client.routes(undefined, { hidden: true }); if (carriesOptIn) await announcedUntil(opted, ".x/y"); else expect(await announcedUntil(opted, "visible")).toEqual(["visible"]); if (carriesOptIn) { - const named = client.announced(Path.Pattern.parse(".x/**")); + const named = client.routes(Path.Pattern.parse(".x/**")); expect(await announcedUntil(named, ".x/y")).toEqual([".x/y"]); named.close(); } @@ -2296,7 +2296,7 @@ async function runOriginFlow(protocol: string, version?: number) { // The announcement lands in the client's origin. const reader = clientOrigin.consume(); - const announced = reader.announced(); + const announced = reader.routes(); expect(await announced.next()).toMatchObject({ prefix: Path.from("test"), kind: "start" }); // Consuming through the origin reaches the wire. @@ -2339,7 +2339,7 @@ test("origin: remote entries retract when the session dies, local ones survive", const reader = clientOrigin.consume(); // Watching is what has the session discover the peer's routes. - const watching = reader.announced(); + const watching = reader.routes(); await until(() => wireOf(reader).routes(Path.from("remote"))); client.abort(); @@ -2373,8 +2373,8 @@ test("origin: one origin on both directions consumes locally and never echoes", ]); // Both sides watch everything, so each session asks the other for announcements. - const watchingShared = shared.announced(); - const watchingServer = serverSees.announced(); + const watchingShared = shared.routes(); + const watchingServer = serverSees.routes(); // The server announces a broadcast; it lands in the shared origin as a remote entry. { @@ -2477,7 +2477,7 @@ test.each([Lite.ALPN_03, Lite.ALPN_05, Lite.ALPN_06, Lite.ALPN_07_WIP])( // Repeated watch cycles must each give their stream back, not pile up on the peer. for (let i = 1; i <= 3; i++) { - const announced = client.announced(Path.Pattern.subtree(Path.from("room"))); + const announced = client.routes(Path.Pattern.subtree(Path.from("room"))); expect((await announced.next())?.kind).toBe("start"); announced.close(); await until(() => ended === i); @@ -2507,7 +2507,7 @@ test.each([Lite.ALPN_05, Lite.ALPN_06, Lite.ALPN_07_WIP])( accept(pair.server, url, { publish: new OriginProducer().consume() }), ]); - const announced = client.announced(Path.Pattern.subtree(Path.from("room"))); + const announced = client.routes(Path.Pattern.subtree(Path.from("room"))); await until(() => started === 1); announced.close(); await until(() => ended === 1); @@ -2553,7 +2553,7 @@ for (const alpn of [Lite.ALPN_06, Lite.ALPN_07_WIP]) { // Browse, then play what the listing found. const reader = clientOrigin.consume(); - const listing = reader.announced(Path.Pattern.subtree(Path.from("room"))); + const listing = reader.routes(Path.Pattern.subtree(Path.from("room"))); expect((await listing.next())?.kind).toBe("start"); const request = reader[method](Path.from("room/cam")); await until(() => request.active.peek() !== undefined); @@ -2634,7 +2634,7 @@ test.each([Ietf.ALPN.DRAFT_18])( accept(pair.server, url, { publish: new OriginProducer().consume() }), ]); - const announced = client.announced(Path.Pattern.subtree(Path.from("room"))); + const announced = client.routes(Path.Pattern.subtree(Path.from("room"))); await until(() => started === 1); announced.close(); await until(() => ended === 1); @@ -2756,7 +2756,7 @@ test("origin: overlapping sessions carrying one path fail over", async () => { return { client, server, serverOrigin, broadcast, serving }; }; - const watching = clientOrigin.announced(); + const watching = clientOrigin.routes(); const first = await setup(); const second = await setup(); @@ -2849,7 +2849,7 @@ test("create then announce is discoverable on the wire", async () => { accept(pair.server, url, { publish: origin.consume() }), ]); - const announced = client.announced(); + const announced = client.routes(); const broadcastPublication = origin.createBroadcast(Path.from("later")); const broadcast = broadcastPublication.broadcast; broadcast.createTrack("chat", { timescale: Timescale.MILLI }); diff --git a/js/net/src/lite/compression.test.ts b/js/net/src/lite/compression.test.ts index d352c314648..13f556f0ce1 100644 --- a/js/net/src/lite/compression.test.ts +++ b/js/net/src/lite/compression.test.ts @@ -27,7 +27,7 @@ for (const downstream of [ALPN_06, ALPN_07_WIP]) { accept({ url, transport: pair.server, publish: publish.consume() }), ])), ); - const announcements = consume.announced(); + const announcements = consume.routes(); try { while ((await announcements.next())?.kind !== "start") {} } finally { diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index f3bf2b621d3..48690d2f88c 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -693,7 +693,7 @@ test("close waits for a request served while withdrawals are in flight", async ( connect({ url, transport: pair.client, consume: destination }), accept({ url, transport: pair.server, publish: source.consume() }), ]); - const announced = destination.announced(); + const announced = destination.routes(); let closed = false; void pair.server.closed.then(() => { closed = true; diff --git a/js/net/src/lite/connection.ts b/js/net/src/lite/connection.ts index a1ca4405692..46ac27753d6 100644 --- a/js/net/src/lite/connection.ts +++ b/js/net/src/lite/connection.ts @@ -1,6 +1,6 @@ import { Milli, Nano } from "@moq/clock"; import { Effect, type GetPromise, type Getter, Once, Signal } from "@moq/signals"; -import type * as announce from "../announced.ts"; +import * as announce from "../announced.ts"; import type * as Auth from "../auth.ts"; import { AuthSession } from "../auth_session.ts"; import type { Established } from "../connection/established.ts"; @@ -344,8 +344,14 @@ export class Connection implements Established { } } + /** The paths announced matching `scope`, each present or absent; see {@link Established.announced}. */ announced(scope?: Path.Pattern, options?: announce.Options): announce.Consumer { - return this.#subscriber.announced(scope, options); + return announce.list(this.#subscriber.routes(scope, options)); + } + + /** Every route change matching `scope`, for code that forwards routes. @internal */ + routes(scope?: Path.Pattern, options?: announce.Options): announce.Routes { + return this.#subscriber.routes(scope, options); } async #runSession() { diff --git a/js/net/src/lite/refresh.test.ts b/js/net/src/lite/refresh.test.ts index cac0a6741a8..90e2ad341a0 100644 --- a/js/net/src/lite/refresh.test.ts +++ b/js/net/src/lite/refresh.test.ts @@ -37,7 +37,7 @@ test.each([ALPN_06, ALPN_07_WIP])("refresh joins reach a browser publisher on %s accept({ url, transport: pair.server, publish: source.consume() }), ])), ); - const announcements = b.announced(); + const announcements = b.routes(); while ((await announcements.next())?.kind !== "start") {} const request = b.request(Path.from("room")); while (request.active.peek() === undefined) await request.active.changed(); diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 44e3437800e..cde4620246b 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -192,7 +192,7 @@ test("an announce consumer closed while its stream opens sends nothing on it", a } as unknown as WebTransport; const subscriber = new Subscriber(quic, Version.DRAFT_06, HopSchema.parse(1n)); - const announced = subscriber.announced(Path.Pattern.subtree(Path.from("room"))); + const announced = subscriber.routes(Path.Pattern.subtree(Path.from("room"))); announced.close(); // The slot frees up only after the consumer left. opening.resolve(); @@ -208,7 +208,7 @@ test("a local empty hop chain is not anonymous", () => { test("a max-length chain plus withheld responder is dropped", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const hops = Array.from({ length: MAX_HOPS }, (_, i) => HopSchema.parse(BigInt(i + 1))); @@ -235,7 +235,7 @@ test("a max-length chain plus withheld responder is dropped", async () => { test("an unidentified responder keeps hop 0 on a nonempty chain", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(UNKNOWN_HOP, 0).encode(w, Version.DRAFT_06)); @@ -258,7 +258,7 @@ test("an unidentified responder keeps hop 0 on a nonempty chain", async () => { test("a received hop list naming no publisher stays anonymous", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(UNKNOWN_HOP, 0).encode(w, Version.DRAFT_06)); @@ -275,7 +275,7 @@ test("a received hop list naming no publisher stays anonymous", async () => { test("a received chain starting with hop 0 is forwarded unchanged", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -296,7 +296,7 @@ test("a received chain starting with hop 0 is forwarded unchanged", async () => test("an update moves the route in place, even from another publisher", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const room = Path.from("room"); @@ -349,7 +349,7 @@ test("an update moves the route in place, even from another publisher", async () test("a lite-07 restart replaces the instance, and the next consume subscribes fresh", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_07); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const room = Path.from("room"); @@ -389,7 +389,7 @@ test("a lite-07 restart replaces the instance, and the next consume subscribes f test("a lite-07 takeover surfaces as a start beneath its predecessor, or a restart of it", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_07); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const pool = Path.from("pool"); const job = Path.from("pool/job"); @@ -441,7 +441,7 @@ test.each([ ["a predecessor not covering it", 0n, "other"], ] as const)("a lite-07 takeover of %s ends the stream", async (_, predecessor, suffix) => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_07); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_07)); @@ -478,7 +478,7 @@ test.each([ ["an end then a start", Version.DRAFT_06], ] as const)("%s of a prefix stops sharing what was consumed beneath it", async (_, version) => { const { subscriber, send, settle } = announceHarness(version); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const pool = Path.from("pool"); const job = Path.from("pool/job"); @@ -522,7 +522,7 @@ test.each([ test("an update that re-prices the same publisher emits the new route", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -554,7 +554,7 @@ test("an update that re-prices the same publisher emits the new route", async () test("a lite-05 duplicate announce follows the same update rule", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_05); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_05)); @@ -578,7 +578,7 @@ test("a lite-05 duplicate announce follows the same update rule", async () => { // that updates in place and keeps the shared broadcast, as does one that names a publisher. test("an update from an unidentified publisher updates in place", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const room = Path.from("room"); @@ -685,7 +685,7 @@ test("an announce skipped as a reflected loop still holds its path", async () => // The subscriber's own origin, so a chain naming it reflects back through us. const SELF = 1n; const { subscriber, send, abortReason, sessionEnded, settle } = announceHarness(Version.DRAFT_06, SELF); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -727,7 +727,7 @@ test("an announce skipped as a reflected loop still holds its path", async () => test("an update replaces an announce that was skipped as a reflected loop", async () => { const SELF = 1n; const { subscriber, send, settle } = announceHarness(Version.DRAFT_06, SELF); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -757,7 +757,7 @@ test("an update replaces an announce that was skipped as a reflected loop", asyn test("an update keeps the instance of an announce skipped as a reflected loop", async () => { const SELF = 1n; const { subscriber, send, settle } = announceHarness(Version.DRAFT_07, SELF); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_07)); @@ -786,7 +786,7 @@ test("an update keeps the instance of an announce skipped as a reflected loop", test("retiring an id whose announce was skipped ends nothing", async () => { const SELF = 1n; const { subscriber, send, settle } = announceHarness(Version.DRAFT_06, SELF); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -821,7 +821,7 @@ test("retiring an id whose announce was skipped ends nothing", async () => { test("a draft-02 initial announcement can still be retracted", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_02); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); // ANNOUNCE_INIT carries the initial set. These are advertisements like any other, so @@ -839,7 +839,7 @@ test("a draft-02 initial announcement can still be retracted", async () => { test("a duplicate start is reported even when its own route reflects", async () => { const SELF = 1n; const { subscriber, send, settle } = announceHarness(Version.DRAFT_06, SELF); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -873,7 +873,7 @@ test("a duplicate start is reported even when its own route reflects", async () test("a draft-02 initial set naming a path twice is refused", async () => { const { subscriber, send, abortReason, sessionEnded, settle } = announceHarness(Version.DRAFT_02); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); // One advertisement per path, and the initial set is advertisements. Two entries for @@ -897,7 +897,7 @@ test("a draft-02 initial set naming a path twice is refused", async () => { test("a violation on a very long path still ends the session", async () => { const SELF = 1n; const { subscriber, send, sessionEnded, settle } = announceHarness(Version.DRAFT_06, SELF); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_06)); @@ -924,7 +924,7 @@ test.each([Version.DRAFT_02, Version.DRAFT_06])( const { subscriber, send, sessionEnded, settle } = announceHarness(version); // Each half fits on the wire alone; the joined path is one byte over. const half = Path.from("x".repeat(Path.MAX_BYTES / 2)); - const announced = subscriber.announced(Path.Pattern.subtree(half)); + const announced = subscriber.routes(Path.Pattern.subtree(half)); await settle(); if (version === Version.DRAFT_02) { @@ -949,7 +949,7 @@ test("a draft-04 duplicate start is a violation, not an update", async () => { // update, is exempt. The Rust loop routes every other version through `start_announce`, // which rejects it. const { subscriber, send, sessionEnded, settle } = announceHarness(Version.DRAFT_04); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const active: AnnounceBroadcast = { status: "active", suffix: Path.from("room"), hops: [PUBLISHER_A] }; @@ -966,7 +966,7 @@ test("a draft-04 duplicate start is a violation, not an update", async () => { test("a draft-05 duplicate start is still an update", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_05); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, Version.DRAFT_05)); @@ -990,7 +990,7 @@ test("a draft-05 duplicate start is still an update", async () => { // Mirrors the Rust `an_unknown_announce_type_keeps_the_stream`. test("an unknown announce type between starts keeps the stream", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); const start = (suffix: string) => (w: Writer) => @@ -1810,7 +1810,7 @@ test.each([Version.DRAFT_05, Version.DRAFT_06])( async (version) => { const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await opened(0); const send = async (encode: (writer: Writer) => Promise) => { const writer = new Writer( @@ -1930,7 +1930,7 @@ test.each([ const warn = spyOn(console, "warn").mockImplementation(() => {}); const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const announced = subscriber.announced(); + const announced = subscriber.routes(); try { await opened(0); const pool = await unpinnedPool(version, streams, announced); @@ -1981,7 +1981,7 @@ test("a withdrawal while the subscribe opens closes the answered track", async ( const warn = spyOn(console, "warn").mockImplementation(() => {}); const { quic, streams, opened } = fakeSession([2]); const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const announced = subscriber.announced(); + const announced = subscriber.routes(); try { await opened(0); const pool = await unpinnedPool(version, streams, announced); @@ -2108,7 +2108,7 @@ test("consumes resolve the serving prefix and pin its instance", async () => { const version = Version.DRAFT_07; const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await opened(0); const peer = new Writer( new WritableStream({ @@ -2171,7 +2171,7 @@ test.each([Version.DRAFT_06, Version.DRAFT_07])( "a more specific start replaces an existing consume on %s", async (version) => { const { subscriber, send, settle } = announceHarness(version); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 0).encode(w, version)); const pool = Path.from("pool"); @@ -2210,7 +2210,7 @@ test.each([Version.DRAFT_05, Version.DRAFT_06])( async (version) => { const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await opened(0); const peer = new Writer( new WritableStream({ @@ -2827,7 +2827,7 @@ const announceStart = (suffix: string) => (w: Writer) => test("a counted initial set syncs once the reader is past its last announce", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 2).encode(w, Version.DRAFT_06)); @@ -2851,7 +2851,7 @@ test("a counted initial set syncs once the reader is past its last announce", as test("an empty initial set syncs on the answer", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); expect(announced.synced.peek()).toBe(false); @@ -2866,7 +2866,7 @@ test("an empty initial set syncs on the answer", async () => { test("a counted initial set cut short is a protocol violation", async () => { for (const cut of ["ended", "fin"] as const) { const { subscriber, send, finish, sessionEnded, settle } = announceHarness(Version.DRAFT_06); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceOk(PEER, 2).encode(w, Version.DRAFT_06)); @@ -2889,7 +2889,7 @@ test.each([Version.DRAFT_03, Version.DRAFT_04])( async (version) => { const yielding = spyOn(Budget.prototype, "poll").mockImplementation(() => yieldNow()); const { subscriber, settle } = announceHarness(version); - const announced = subscriber.announced(); + const announced = subscriber.routes(); try { expect(announced.synced.peek()).toBe(false); await settle(); @@ -2904,7 +2904,7 @@ test.each([Version.DRAFT_03, Version.DRAFT_04])( test("announce init is the initial set", async () => { const { subscriber, send, settle } = announceHarness(Version.DRAFT_02); - const announced = subscriber.announced(); + const announced = subscriber.routes(); await settle(); await send((w) => new AnnounceInit([Path.from("room")]).encode(w, Version.DRAFT_02)); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 6910c85e4c3..87515b03144 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -331,7 +331,7 @@ export class Subscriber { * Only routes whose literal name matches `scope` are reported, unless `options.covering` * also asks for those whose subtree overlaps it. */ - announced(scope: Path.Pattern = Path.Pattern.all(), options?: announce.Options): announce.Consumer { + routes(scope: Path.Pattern = Path.Pattern.all(), options?: announce.Options): announce.Routes { const announced = new announce.Producer(); // The wire speaks announce interest by prefix, and echoes suffixes beneath it. const consumer = announced.consume(); diff --git a/js/net/src/lite/tracks.test.ts b/js/net/src/lite/tracks.test.ts index c35c7780250..346abbbe2f7 100644 --- a/js/net/src/lite/tracks.test.ts +++ b/js/net/src/lite/tracks.test.ts @@ -503,7 +503,7 @@ test("an instance-pinned front lists its serving route's tracks", async () => { broadcastPublication.announce({ instance: Instance.parse("01900000-0000-7000-8000-000000000001") }); const reader = clientOrigin.consume(); - const announced = reader.announced(); + const announced = reader.routes(); expect(await announced.next()).toMatchObject({ prefix: Path.from("room"), kind: "start" }); const request = reader.request(Path.from("room")); await settle(); diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index 1b54098dc39..36ad412a0c5 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -119,7 +119,7 @@ test("a borrowed table exposes dynamic serving and a live scoped broadcast map", const origin = new Producer(); const table: Table = origin; const scope = Path.Pattern.parse("room/**"); - const live = await announcedView(table, scope); + const live = await announcedView(origin, scope); const changes: ReadonlyMap[] = []; const stop = live.subscribe((value) => changes.push(value)); await new Promise((resolve) => setImmediate(resolve)); @@ -369,7 +369,7 @@ test("announced streams the table under a scope with origin-relative paths", asy const a = publish(origin, Path.from("room/a")); - const announced = consumer.announced(Path.Pattern.subtree(Path.from("room"))); + const announced = consumer.routes(Path.Pattern.subtree(Path.from("room"))); // The initial state arrives first, named from the origin rather than the scope. expect(await announced.next()).toMatchObject({ prefix: Path.from("room/a"), kind: "start" }); @@ -407,10 +407,10 @@ test("hidden paths need an opt-in or a scope naming the dot segment", async () = Path.from(".stats/node"), ]); - const plain = consumer.announced(); + const plain = consumer.routes(); await new Promise((resolve) => setImmediate(resolve)); expect(await plain.next()).toMatchObject({ prefix: Path.from("room/catalog.pro") }); - const opted = consumer.announced(Path.Pattern.parse("room/**"), { hidden: true }); + const opted = consumer.routes(Path.Pattern.parse("room/**"), { hidden: true }); const seen = [(await opted.next())?.prefix, (await opted.next())?.prefix].sort(); await new Promise((resolve) => setImmediate(resolve)); expect(seen).toEqual([Path.from("room/.internal"), Path.from("room/catalog.pro")]); @@ -437,7 +437,7 @@ test("a remote entry resolves by path and retracts on dispose", async () => { handle?.close(); // Announced streams include remote entries. - const announced = consumer.announced(); + const announced = consumer.routes(); expect(await announced.next()).toMatchObject({ prefix: path, kind: "start", route: Route.default }); dispose(); @@ -458,7 +458,7 @@ test("announced keeps a broader covering route at its prefix", async () => { // The scope filters the route without changing the prefix it claims. const scope = Path.Pattern.subtree(Path.from("room/alice")); - const announced = consumer.announced(scope, { covering: true }); + const announced = consumer.routes(scope, { covering: true }); expect(await announced.next()).toMatchObject({ prefix: Path.from("room"), kind: "start" }); dispose(); @@ -489,7 +489,7 @@ test("a local publish shadows a remote entry", async () => { handle?.close(); // One path, one announcement, even though both tables route it. - const announced = consumer.announced(); + const announced = consumer.routes(); expect(await announced.next()).toMatchObject({ prefix: path, kind: "start", route: Route.default }); // Dropping the local publish falls back to the remote entry without a retraction. @@ -613,7 +613,7 @@ test("disposing the newest remote route promotes the fallback", async () => { const keepNewer = newer.consume(); const disposeNewer = serve(origin, path, provider(newer)); - const announced = consumer.announced(); + const announced = consumer.routes(); expect(await announced.next()).toMatchObject({ prefix: path, kind: "start" }); // The newer session dies: without an instance the promoted fallback is another source. @@ -687,7 +687,7 @@ test("requests never appear in announced or the table", async () => { expect(request.active.peek()).toBeDefined(); expect(wireOf(origin).routes(path)).toBe(false); - const announced = consumer.announced(); + const announced = consumer.routes(); publish(origin, Path.from("real")); expect(await announced.next()).toMatchObject({ prefix: Path.from("real"), kind: "start" }); @@ -702,7 +702,7 @@ test("a republish restarts the path", async () => { const path = Path.from("room"); publish(origin, path); - const announced = consumer.announced(); + const announced = consumer.routes(); expect(await announced.next()).toMatchObject({ prefix: path, kind: "start" }); // A new broadcast takes the path: consumers must let go of the replaced one. @@ -718,7 +718,7 @@ test("an identical reconnect restarts without an instance and is quiet with one" const origin = new Producer(); const consumer = origin.consume(); const path = Path.from("room"); - const announced = consumer.announced(); + const announced = consumer.routes(); const route = Route.normalize({ instance, hops: [PEER] }); const old = wireOf(origin).receive(path, route); @@ -1036,7 +1036,7 @@ test("createBroadcast is invisible to everyone until announce", async () => { const request = consumer.request(path); expect(request.active.peek()).toBeUndefined(); - const announced = consumer.announced(); + const announced = consumer.routes(); broadcastPublication.announce({ cost: 4n }); expect(wireOf(consumer).routes(path)).toBe(true); expect(wireOf(consumer).advertised.peek()?.has(path)).toBe(true); @@ -1093,7 +1093,7 @@ test("a request prefers a cheaper received route over an announced local broadca await settle(); const remote = request.active.peek(); expect(remote).toBeDefined(); - const announced = consumer.announced(); + const announced = consumer.routes(); expect(await announced.next()).toMatchObject({ prefix: path, route: { hops: [PEER] } }); // Re-priced below it, the local broadcast of the same instance wins at once: the request's @@ -1156,7 +1156,7 @@ test("a handle serves a request under live", async () => { const consumer = origin.consume(); const handle = origin.requestsRoute(Path.from("live")); - expect(await consumer.announced().next()).toMatchObject({ prefix: Path.from("live"), kind: "start" }); + expect(await consumer.routes().next()).toMatchObject({ prefix: Path.from("live"), kind: "start" }); const waiting = handle.requested().next(); const request = consumer.request(Path.from("live/cam")); @@ -1220,7 +1220,7 @@ test("a received route still serves a broadcast a session delivered", async () = test("a re-priced route is delivered as an update", async () => { const origin = new Producer(); const handle = origin.requestsRoute(Path.from("live")); - const announced = origin.consume().announced(); + const announced = origin.consume().routes(); expect(await announced.next()).toMatchObject({ prefix: Path.from("live"), kind: "start" }); handle.updateRoute({ cost: 5n }); expect(await announced.next()).toMatchObject({ prefix: Path.from("live"), kind: "update" }); @@ -1843,7 +1843,7 @@ test("announced filters by arbitrary patterns and reports captures", async () => const origin = new Producer(); const consumer = origin.consume(); const broadcast = publish(origin, Path.from("room/alice")); - const announced = consumer.announced(Path.Pattern.parse("room/*")); + const announced = consumer.routes(Path.Pattern.parse("room/*")); const update = await announced.next(); expect(update?.prefix).toBe(Path.from("room/alice")); @@ -1892,7 +1892,7 @@ test("announced filters before rooting and retains lifecycle, routing and watch const unlisten = wireOf(origin).interests((prefix, wanted) => interests.push(`${wanted ? "+" : "-"}${prefix}`)); const source = new BroadcastProducer(); const broad = origin.requestsRoute(Path.from("room")); - const stream = reader.announced(); + const stream = reader.routes(); const events = stream[Symbol.asyncIterator](); const covering = await announcedView(reader, undefined, { covering: true }); expect([...covering.peek().keys()]).toEqual([Path.empty()]); @@ -2071,7 +2071,7 @@ test("a rooted producer shares the table and enforces its pattern union", async expect([...(await announcedView(nested)).peek().keys()]).toEqual([Path.from("alice")]); await new Promise((resolve) => setImmediate(resolve)); expect(() => nested.createBroadcast(Path.from("alice/deep")).broadcast).toThrow("outside the origin scope"); - const announced = nested.announced(); + const announced = nested.routes(); await new Promise((resolve) => setImmediate(resolve)); expect(await announced.next()).toMatchObject({ kind: "start", prefix: Path.from("alice") }); announced.close(); @@ -2150,7 +2150,7 @@ test("closing a derived handle withdraws the sessions and answers attached throu const snapshot = wireOf(scoped).snapshot(Path.empty()); const interests: string[] = []; const unlisten = wireOf(scoped).interests((prefix, wanted) => interests.push(`${wanted ? "+" : "-"}${prefix}`)); - const announced = consumer.announced(); + const announced = consumer.routes(); const upstream = new BroadcastProducer(); const withdraw = wireOf(scoped).answer(path, upstream.consume()); expect(withdraw).toBeDefined(); @@ -2292,7 +2292,7 @@ test("a scoped reader picks the best route its scope can see at a prefix", async (await announcedView(origin, Path.Pattern.parse("room/video"), { covering: true })).peek().get(Path.empty()) ?.cost, ).toBe(5n); - const announced = reader.announced(undefined, { covering: true }); + const announced = reader.routes(undefined, { covering: true }); await new Promise((resolve) => setImmediate(resolve)); expect((await announced.next())?.route.cost).toBe(5n); announced.close(); @@ -2467,7 +2467,7 @@ test("the newest epoch of an instance is announced in place over a cheaper one", const origin = new Producer(); const consumer = origin.consume(); const path = Path.from("room/alice"); - const announced = consumer.announced(); + const announced = consumer.routes(); const instance = Instance.random(); const oldPublication = origin.createBroadcast(path); @@ -2682,7 +2682,7 @@ test.each([ if (named !== undefined) expect(from).toBe(named); const to = NEWER; const requests = dynamic.requested(); - const announced = origin.consume().announced(); + const announced = origin.consume().routes(); expect(await announced.next()).toMatchObject({ prefix, kind: "start" }); const held = origin.consume().request(path); diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index 6b6f1b5eb9c..ca062735965 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -1674,6 +1674,11 @@ export class Producer implements Table { return this.#reader.announced(scope, options); } + /** Every route change under `scope`; see {@link Consumer.routes}. @internal */ + routes(scope?: Path.Pattern, options?: Announce.Options): announce.Routes { + return this.#reader.routes(scope, options); + } + /** * Close the origin, every broadcast it still routes, and its announcement streams. Idempotent. * @@ -2255,8 +2260,9 @@ export class Consumer implements Reader { } /** - * The announced routes matching `scope`, as a live stream: every currently advertised - * route arrives first as `start`, then changes as they happen. + * The paths announced matching `scope`, each reported as it becomes present or absent: + * every currently advertised one arrives first as `present`. Another instance taking a + * path over keeps it present; {@link follow} it to see the broadcast change. * Any pattern is accepted. A local broadcast appears once it announces, exactly as a * peer sees it. Only routes whose literal name matches the scope are listed, unless * `options.covering` also asks for every dynamic or received route whose subtree @@ -2266,6 +2272,17 @@ export class Consumer implements Reader { * announcements there. */ announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { + return announce.list(this.routes(scope, options)); + } + + /** + * Every change to the routes matching `scope`, for code that forwards routes: a re-price + * is an `update`, and another instance serving a path a `restart`. Scope, options and the + * watch are as for {@link announced}. + * + * @internal + */ + routes(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): announce.Routes { const producer = new announce.Producer(); const patterns = this.#scope.patterns(scope); void producer.closed.then(this.#state.watch([...patterns].map(scopeHead))); diff --git a/js/net/src/request-deadline.test.ts b/js/net/src/request-deadline.test.ts index 9fdae479bfa..6052ddb6cd4 100644 --- a/js/net/src/request-deadline.test.ts +++ b/js/net/src/request-deadline.test.ts @@ -140,7 +140,7 @@ for (const protocol of ["lite", "ietf"] as const) { protocol === "lite" ? new LiteSubscriber(transport.quic, Lite.DRAFT_07, HopSchema.parse(1n)) : new IetfSubscriber({ session: new NativeSession(transport.quic, Ietf.DRAFT_18, true) }); - const announced = subscriber.announced(); + const announced = subscriber.routes(); try { await transport.created; await clock.advance(Nano.zero); diff --git a/js/net/src/session-limits.test.ts b/js/net/src/session-limits.test.ts index 345bd84d04d..af296baf1e1 100644 --- a/js/net/src/session-limits.test.ts +++ b/js/net/src/session-limits.test.ts @@ -87,7 +87,7 @@ test.each(["lite", "ietf"] as const)("%s closes on the first advertisement beyon limits, }); try { - const announced = subscriber.announced(); + const announced = subscriber.routes(); const ended = await announced.closed; expect(ended).toBeInstanceOf(SessionError); expect((ended as SessionError).code).toBe(SessionCode.TooManyRequests); diff --git a/js/room/src/room.test.ts b/js/room/src/room.test.ts index 8b8260cea0c..16beb40dcf4 100644 --- a/js/room/src/room.test.ts +++ b/js/room/src/room.test.ts @@ -20,7 +20,7 @@ test("room restores the announce prefix and reconciles local identity changes", let update: Net.Announce.Event | undefined = { prefix: Net.Path.from("room-a/bob/camera.hang"), captures: [Net.Path.Pattern.literal(Net.Path.from("bob/camera.hang"))], - kind: "start", + kind: "present", route: { hops: [], cost: 0n }, }; const stream = { diff --git a/js/room/src/room.ts b/js/room/src/room.ts index c9e038438a4..700d4df4cd3 100644 --- a/js/room/src/room.ts +++ b/js/room/src/room.ts @@ -95,7 +95,7 @@ export class Room { const local = this.identity.peek(); if (local && parsed.identity === local) continue; - if (update.kind !== "end") { + if (update.kind !== "absent") { this.#add(parsed.identity, parsed.kind, Moq.Path.from(covered)); } else { this.#remove(parsed.identity, parsed.kind); diff --git a/js/watch/src/broadcast.ts b/js/watch/src/broadcast.ts index 986e4de0626..ef63f0abde2 100644 --- a/js/watch/src/broadcast.ts +++ b/js/watch/src/broadcast.ts @@ -60,7 +60,7 @@ function watchCovered( for (;;) { const entry = await announced.next(); if (!entry) break; - if (entry.kind === "end") active.delete(entry.prefix); + if (entry.kind === "absent") active.delete(entry.prefix); else active.add(entry.prefix); covered.set([...active].some((prefix) => Path.hasPrefix(prefix, path))); } diff --git a/js/watch/src/echo.test.ts b/js/watch/src/echo.test.ts index 3eea05958dd..e80b3509954 100644 --- a/js/watch/src/echo.test.ts +++ b/js/watch/src/echo.test.ts @@ -31,7 +31,7 @@ function announced(origin: Origin.Producer): Set { for (;;) { const entry = await listing.next(); if (!entry) break; - if (entry.kind === "end") paths.delete(entry.prefix); + if (entry.kind === "absent") paths.delete(entry.prefix); else paths.add(entry.prefix); } })(); diff --git a/kt/README.md b/kt/README.md index 0cf7f99bfb9..10556290dc8 100644 --- a/kt/README.md +++ b/kt/README.md @@ -31,13 +31,11 @@ import kotlinx.coroutines.flow.collect // connect() wires up an internal origin and returns a live connection. Moq.connect("https://relay.example.com").use { moq -> moq.announced(AnnounceConfig(prefix = "demos/", filter = "*/camera")).updates().collect { event -> - // A restart is a new publisher run at the same path: request it again. A takeover - // continues what an earlier route served, like a start. + // The listing reports each path once while it stays present, even as its publisher + // restarts; `moq.follow(path)` hands out each new run, or null once it goes offline. val announce = when (event) { - is AnnounceEventStart -> event.announce - is AnnounceEventRestart -> event.announce - is AnnounceEventTakeover -> event.announce - else -> return@collect // Update or End + is AnnounceEventPresent -> event.announce + is AnnounceEventAbsent -> return@collect } // Prefix stays origin-relative; captures reports what * matched. println("got broadcast ${announce.prefix}") diff --git a/kt/moq/MIGRATE.md b/kt/moq/MIGRATE.md index 92423abd12b..77428b07a6b 100644 --- a/kt/moq/MIGRATE.md +++ b/kt/moq/MIGRATE.md @@ -4,6 +4,27 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `Following.available()` returns null once the path goes offline + +`following.available()` returns `BroadcastConsumer?`. +After the first call, it returns null once the followed path goes offline, or, for a `follow(path, instance)` pinned to an instance, once another instance takes it over; the next call returns whatever serves the path next. The first call never returns null. +Drop what you read from the previous broadcast either way. A caller that only waits for the first broadcast can take it with `!!`. + +### Announcements are a listing + +`announced(config).updates()` emits `AnnounceEventPresent` when a path becomes present and `AnnounceEventAbsent` when it goes, each carrying an `Announce` as `announce`. +`AnnounceEventStart` is `AnnounceEventPresent` and `AnnounceEventEnd` is `AnnounceEventAbsent`. +`AnnounceEventUpdate`, `AnnounceEventRestart`, and `AnnounceEventTakeover` are gone: a path whose route or publisher instance changes stays present and is not reported again. +Where you requested the path again on a restart, hold `follow(path)` instead; each `available()` returns the next instance's broadcast. + +```kotlin +val following = moq.follow("live/camera") +while (true) { + val broadcast = following.available() ?: continue // offline until the next instance + play(broadcast) +} +``` + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `Video` and `Audio` gain `warmup`, the catalog's decoder warmup; it defaults to `null` as the last field, so existing code keeps working. `TrackConsumer` and the audio and video consumers read it from the rendition and start that much earlier. diff --git a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt index 6ea2b02ec79..e60a13736c6 100644 --- a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt +++ b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt @@ -92,33 +92,20 @@ typealias Following = uniffi.moq.MoqFollowing typealias Announce = uniffi.moq.MoqAnnounce /** - * What an [AnnounceConsumer] yields: [AnnounceEventStart], [AnnounceEventUpdate], - * [AnnounceEventEnd], [AnnounceEventRestart], or [AnnounceEventTakeover]. + * What an [AnnounceConsumer] yields: [AnnounceEventPresent] or [AnnounceEventAbsent]. + * + * Another publisher instance taking a path over keeps it present: hold a [Following] to see the + * broadcast change. */ typealias AnnounceEvent = uniffi.moq.MoqAnnounceEvent // Kotlin cannot reach a sealed class's subtypes through its typealias, so each // variant gets its own. -/** A route now covers the prefix; the stream had none there. */ -typealias AnnounceEventStart = uniffi.moq.MoqAnnounceEvent.Start +/** A route now covers the path, which was absent. */ +typealias AnnounceEventPresent = uniffi.moq.MoqAnnounceEvent.Present -/** The route covering the prefix changed hops or cost. */ -typealias AnnounceEventUpdate = uniffi.moq.MoqAnnounceEvent.Update - -/** No route covers the prefix any more; carries its last route. */ -typealias AnnounceEventEnd = uniffi.moq.MoqAnnounceEvent.End - -/** - * Another publisher instance now serves the prefix: drop what was resolved under it and request - * afresh. - */ -typealias AnnounceEventRestart = uniffi.moq.MoqAnnounceEvent.Restart - -/** - * A route now covers the prefix and continues what the route at `predecessor` served beneath it: - * keep what was resolved through that one. - */ -typealias AnnounceEventTakeover = uniffi.moq.MoqAnnounceEvent.Takeover +/** No route covers the path any more; carries its last route. */ +typealias AnnounceEventAbsent = uniffi.moq.MoqAnnounceEvent.Absent // Broadcast / track / group producers and consumers. /** The write side of a broadcast: publish tracks into it. */ diff --git a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Moq.kt b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Moq.kt index fc1fbb9d6a4..89b15bc41a6 100644 --- a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Moq.kt +++ b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Moq.kt @@ -26,9 +26,10 @@ internal constructor( fun createBroadcast(path: String): Publication = session.publish().createBroadcast(path) /** - * Discover routes matching [config]; prefixes stay relative to the origin. Collect - * `announced(config).updates()` for a [Flow] of [AnnounceEvent] that cancels the handle when - * collection ends. + * List the paths matching [config] as each becomes present or absent; prefixes stay relative to + * the origin. Collect `announced(config).updates()` for a [Flow] of [AnnounceEvent] that + * cancels the handle when collection ends. A new publisher instance keeps its path present, so + * use [follow] to see it. */ fun announced(config: AnnounceConfig = AnnounceConfig()): AnnounceConsumer = session.consume().announced(config) @@ -37,8 +38,9 @@ internal constructor( * Follow exactly [path], resolving each stream that serves it. * * Unlike [request] this waits indefinitely for a future announcement, and each later - * `available()` waits for the next stream, such as a restarted publisher. Pass an [instance] to - * follow only that stream. Cancel the returned handle to stop waiting. + * `available()` waits for the next stream, such as a restarted publisher, or returns null once + * the path goes offline. Pass an [instance] to follow only that stream; another one taking over + * also returns null. Cancel the returned handle to stop waiting. */ fun follow(path: String, instance: String? = null): Following = session.consume().follow(path, instance) diff --git a/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt b/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt index 013cfaf18ff..9c40670f10d 100644 --- a/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt +++ b/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt @@ -779,16 +779,16 @@ class SmokeTest { publication.announce(AnnounceRoute()) val announced = consumer.announced(AnnounceConfig()) - val first = assertIs(announced.nextRoute()) + val first = assertIs(announced.nextRoute()) assertEquals("live", first.announce.prefix) publication.unannounce() - val retracted = assertIs(announced.nextRoute()) + val retracted = assertIs(announced.nextRoute()) assertEquals("live", retracted.announce.prefix) assertFailsWith { consumer.request("live") } publication.announce(AnnounceRoute()) - assertIs(announced.nextRoute()) + assertIs(announced.nextRoute()) consumer.request("live") } } @@ -802,11 +802,11 @@ class SmokeTest { val publication = origin.createBroadcast("live") publication.broadcast().use { broadcast -> publication.use { it.announce(AnnounceRoute()) } - assertIs(announced.nextRoute()) + assertIs(announced.nextRoute()) consumer.request("live") broadcast.end() - assertIs(announced.nextRoute()) + assertIs(announced.nextRoute()) assertFailsWith { consumer.request("live") } } } @@ -831,22 +831,48 @@ class SmokeTest { } @Test - fun `a new instance restarts announce consumers`() = runTest { + fun `a new instance restarts followers but stays listed`() = runTest { OriginProducer(OriginConfig()).use { origin -> - val announced = origin.consume().announced(AnnounceConfig()) + val consumer = origin.consume() + val announced = consumer.announced(AnnounceConfig()) + val following = consumer.follow("instance", null) origin.createBroadcast("instance").use { publication -> publication.announce(AnnounceRoute()) - val start = assertIs(announced.nextRoute()) - assertTrue(start.announce.route.instance.isNotEmpty()) + val present = assertIs(announced.nextRoute()) + assertTrue(present.announce.route.instance.isNotEmpty()) + assertNotNull(following.available()).close() publication.announce(AnnounceRoute(instance = "camera", epoch = 1uL)) - val restart = assertIs(announced.nextRoute()) - assertEquals("camera", restart.announce.route.instance) + assertNotNull(following.available()).close() + + // The takeover was not listed, so the next event is the path going absent. + publication.unannounce() + val absent = assertIs(announced.nextRoute()) + assertEquals("camera", absent.announce.route.instance) assertFailsWith { publication.announce(AnnounceRoute(instance = "")) } } + following.cancel() + } + } + + @Test + fun `a follower reports the path going offline and the next broadcast`() = runTest { + OriginProducer(OriginConfig()).use { origin -> + val following = origin.consume().follow("offline", null) + origin.createBroadcast("offline").use { publication -> + publication.announce(AnnounceRoute()) + assertNotNull(following.available()).close() + + publication.unannounce() + assertNull(following.available()) + + publication.announce(AnnounceRoute()) + assertNotNull(following.available()).close() + } + following.cancel() } } @@ -857,7 +883,7 @@ class SmokeTest { origin.consume().announced(AnnounceConfig(prefix = "room", filter = "*/chat")) origin.createBroadcast("room/alice/chat").use { publication -> publication.announce(AnnounceRoute()) - val update = assertIs(announced.nextRoute()) + val update = assertIs(announced.nextRoute()) assertEquals("room/alice/chat", update.announce.prefix) assertEquals(listOf("alice"), update.announce.captures) } diff --git a/py/moq-rs/MIGRATE.md b/py/moq-rs/MIGRATE.md index 9fdbbcff993..eba8ff96742 100644 --- a/py/moq-rs/MIGRATE.md +++ b/py/moq-rs/MIGRATE.md @@ -4,6 +4,18 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Announcements are a listing of present and absent paths + +`AnnounceConsumer` yields `AnnounceEventPresent` when a route starts covering a path and `AnnounceEventAbsent` when none covers it any more, each carrying an `Announce` as `announce`. +`AnnounceEventStart`, `AnnounceEventUpdate`, `AnnounceEventEnd`, `AnnounceEventRestart`, and `AnnounceEventTakeover` are removed: read `AnnounceEventStart` as `AnnounceEventPresent` and `AnnounceEventEnd` as `AnnounceEventAbsent`, and drop the `Update` branch, since a re-priced route is no longer reported. +Another publisher instance taking a path over keeps it present and is not listed. Where you requested the path again on `AnnounceEventRestart`, hold `follow(path)` instead and read each `BroadcastConsumer` that `await following.available()` returns, dropping what you read from the previous one. + +### `Following.available()` returns `None` once the path goes offline + +`await following.available()` (or `await following`) returns `BroadcastConsumer | None`. +Later calls return `None` when the path goes offline, or, when following an `instance`, when another instance takes it over, then the next broadcast once something serves the path again; the first call never returns `None`. +Stop reading the previous broadcast on `None` and call `available()` again. A caller that only awaits the first broadcast can assert it is not `None`. + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `Video` and `Audio` gain `warmup`, the catalog's decoder warmup; it defaults to `None` as the last field, so existing code keeps working. `TrackConsumer` and the audio and video consumers read it from the rendition and start that much earlier. diff --git a/py/moq-rs/README.md b/py/moq-rs/README.md index cf4c4c2dd08..bbaa3f5ae16 100644 --- a/py/moq-rs/README.md +++ b/py/moq-rs/README.md @@ -29,10 +29,8 @@ import moq async def main(): async with moq.connect("https://cdn.moq.dev/anon") as client: async for event in client.announced(): - # A restart is a new publisher run at the same path: request it again. A - # takeover continues what an earlier route served, like a start. - if not isinstance(event, (moq.AnnounceEventStart, moq.AnnounceEventRestart, moq.AnnounceEventTakeover)): - continue # AnnounceEventUpdate or AnnounceEventEnd + if not isinstance(event, moq.AnnounceEventPresent): + continue # AnnounceEventAbsent # A route covers a prefix and carries no broadcast, so resolve the path. broadcast = await client.request(event.announce.prefix) catalog = await moq.media.catalog(broadcast) @@ -262,8 +260,8 @@ Every handle whose cleanup is `cancel()` is an async context manager, so exiting - `await .requested_broadcast() → BroadcastRequest`. Call `.serve(consumer)` to answer with held content under the prefix route's continuous sequence space, `.prepare()` then fill its `.broadcast` and `.publish() → Publication` for an independently restarting job under a fresh instance, or `.reject(code)` to fail the requester. - Async iterator yielding `BroadcastRequest` - **`OriginConsumer`**. Discover broadcasts. - - `.announced(prefix, filter=None) → AnnounceConsumer` (async iterator of `AnnounceEvent`: `AnnounceEventStart`, `AnnounceEventUpdate`, `AnnounceEventEnd`, `AnnounceEventRestart`, or `AnnounceEventTakeover`, each carrying an `Announce` whose `.route.instance` names the stream; on a restart, request the path again; a takeover continues what the route at `.predecessor` served); `filter` is a pattern relative to the literal prefix, while each `Announce.prefix` stays origin-relative and `.captures` is a required list of wildcard matches, empty without wildcards - - `.follow(path, instance=None) → Following` (awaitable, waits until something serves the path, then each stream that takes over; an `instance` follows only that stream) + - `.announced(prefix, filter=None) → AnnounceConsumer` (async iterator of `AnnounceEvent`: `AnnounceEventPresent` or `AnnounceEventAbsent`, each carrying an `Announce` whose `.route.instance` names the stream; another instance taking a path over keeps it present, so `follow` the path to see that); `filter` is a pattern relative to the literal prefix, while each `Announce.prefix` stays origin-relative and `.captures` is a required list of wildcard matches, empty without wildcards + - `.follow(path, instance=None) → Following` (awaitable, waits until something serves the path, then returns each stream that takes over, or `None` once the path goes offline; an `instance` follows only that stream) - `.request(path, instance=None) → BroadcastConsumer` (awaitable; owns discovery and resolves one stream, or raises once synced without a route; an `instance` refuses any other stream) ### Types diff --git a/py/moq-rs/docs/index.md b/py/moq-rs/docs/index.md index b342bfb7814..fcb8b948027 100644 --- a/py/moq-rs/docs/index.md +++ b/py/moq-rs/docs/index.md @@ -17,7 +17,7 @@ import moq async def main(): async with moq.Client("https://cdn.moq.dev/anon") as client: async for event in client.announced(): - if isinstance(event, moq.AnnounceEventStart): + if isinstance(event, moq.AnnounceEventPresent): broadcast = await client.request(event.announce.prefix) print(await moq.media.catalog(broadcast)) @@ -176,11 +176,8 @@ broadcast, and producers advertise it in the catalog. Following Announce AnnounceEvent - AnnounceEventStart - AnnounceEventUpdate - AnnounceEventEnd - AnnounceEventRestart - AnnounceEventTakeover + AnnounceEventPresent + AnnounceEventAbsent ``` ## Data types diff --git a/py/moq-rs/examples/announced.py b/py/moq-rs/examples/announced.py index 74786144aee..77dc6617c28 100644 --- a/py/moq-rs/examples/announced.py +++ b/py/moq-rs/examples/announced.py @@ -14,9 +14,9 @@ async def run(url: str, prefix: str, insecure: bool) -> None: async with moq.Client(url, tls=moq.ClientTls(insecure=insecure)) as client: print(f"watching route announcements under {prefix!r} at {url}") async for event in client.announced(prefix): - if isinstance(event, moq.AnnounceEventStart): + if isinstance(event, moq.AnnounceEventPresent): print(f" + {event.announce.prefix}") - elif isinstance(event, moq.AnnounceEventEnd): + elif isinstance(event, moq.AnnounceEventAbsent): print(f" - {event.announce.prefix}") diff --git a/py/moq-rs/examples/clock.py b/py/moq-rs/examples/clock.py index 7fd03a5a569..4df4e102f8b 100644 --- a/py/moq-rs/examples/clock.py +++ b/py/moq-rs/examples/clock.py @@ -42,6 +42,7 @@ async def subscribe(url: str, broadcast_name: str, track_name: str, insecure: bo async with moq.Client(url, tls=moq.ClientTls(insecure=insecure)) as client: print(f"waiting for {broadcast_name!r} at {url}") broadcast = await client.follow(broadcast_name) + assert broadcast is not None print(f"subscribed to {broadcast_name!r} track={track_name!r}") track = await broadcast.subscribe_track(track_name) diff --git a/py/moq-rs/moq/__init__.py b/py/moq-rs/moq/__init__.py index ada21ed35f0..b5b74fc4c2b 100644 --- a/py/moq-rs/moq/__init__.py +++ b/py/moq-rs/moq/__init__.py @@ -18,11 +18,8 @@ Announce, AnnounceConsumer, AnnounceEvent, - AnnounceEventEnd, - AnnounceEventRestart, - AnnounceEventStart, - AnnounceEventTakeover, - AnnounceEventUpdate, + AnnounceEventAbsent, + AnnounceEventPresent, BroadcastPrepared, BroadcastRequest, Following, @@ -102,11 +99,8 @@ "Announce", "AnnounceConsumer", "AnnounceEvent", - "AnnounceEventEnd", - "AnnounceEventRestart", - "AnnounceEventTakeover", - "AnnounceEventStart", - "AnnounceEventUpdate", + "AnnounceEventAbsent", + "AnnounceEventPresent", "AnnounceRoute", "AudioCodec", "AudioEncoderInput", diff --git a/py/moq-rs/moq/origin.py b/py/moq-rs/moq/origin.py index 751735b1aea..090aabc1c53 100644 --- a/py/moq-rs/moq/origin.py +++ b/py/moq-rs/moq/origin.py @@ -33,27 +33,18 @@ """ AnnounceEvent = MoqAnnounceEvent -"""What :class:`AnnounceConsumer` yields: :data:`AnnounceEventStart`, -:data:`AnnounceEventUpdate`, :data:`AnnounceEventEnd`, :data:`AnnounceEventRestart`, -or :data:`AnnounceEventTakeover`, each carrying an :data:`Announce` as ``announce``. -""" - -AnnounceEventStart = MoqAnnounceEvent.START -"""A route now covers the prefix; the stream had none there.""" +"""What :class:`AnnounceConsumer` yields: :data:`AnnounceEventPresent` or +:data:`AnnounceEventAbsent`, each carrying an :data:`Announce` as ``announce``. -AnnounceEventUpdate = MoqAnnounceEvent.UPDATE -"""The route covering the prefix changed hops or cost.""" - -AnnounceEventEnd = MoqAnnounceEvent.END -"""No route covers the prefix any more; carries its last route.""" +Another publisher instance taking a path over keeps it present; hold +:meth:`OriginConsumer.follow` to see the broadcast change. +""" -AnnounceEventRestart = MoqAnnounceEvent.RESTART -"""Another publisher instance now serves the prefix: drop what was resolved under -it and request afresh.""" +AnnounceEventPresent = MoqAnnounceEvent.PRESENT +"""A route now covers the path, which was absent.""" -AnnounceEventTakeover = MoqAnnounceEvent.TAKEOVER -"""A route now covers the prefix and continues what the route at ``predecessor`` -served beneath it: keep what was resolved through that one.""" +AnnounceEventAbsent = MoqAnnounceEvent.ABSENT +"""No route covers the path any more; carries its last route.""" class AnnounceConsumer(_Stream[MoqAnnounceEvent]): @@ -78,8 +69,8 @@ class Following: """Follows a specific path across the publisher instances that serve it. ``await`` it (or call :meth:`available`) to get the :class:`BroadcastConsumer` - once the broadcast becomes available, and again for each instance that takes over. - Usable as an async context manager. + once the broadcast becomes available, again for each instance that takes over, + and ``None`` while the path is offline. Usable as an async context manager. """ def __init__(self, inner: MoqFollowing) -> None: @@ -91,15 +82,18 @@ async def __aenter__(self): async def __aexit__(self, *exc) -> None: self.cancel() - async def available(self) -> BroadcastConsumer: - """Await the next publisher instance serving the path and return its consumer. + async def available(self) -> BroadcastConsumer | None: + """Await the next change to the publisher instance serving the path. - The first call resolves the instance serving the path once one is announced. Each - later call waits for another to take over (a restarted publisher, or a replacement - session) or for the broadcast to return after it went offline. Drop what was read - from the previous instance then: its tracks end when its session does. + The first call resolves the instance serving the path once one is announced, and + never returns ``None``. Each later call returns another instance taking over (a + restarted publisher, or a replacement session), or ``None`` once the path goes + offline (or, when following an ``instance``, another one takes it over), then + whatever serves it next. Drop what was read from the previous instance then: its + tracks end when its session does. """ - return BroadcastConsumer(await self._inner.available()) + broadcast = await self._inner.available() + return BroadcastConsumer(broadcast) if broadcast is not None else None def __await__(self): return self.available().__await__() @@ -225,8 +219,8 @@ def follow(self, path: str, *, instance: str | None = None) -> Following: A broadcast created on this origin resolves once it is announced, like a remote one. Pass an ``instance`` to follow only that stream: another one - taking over is never handed out, and the stream is handed out again if it - returns after an end. + taking over reads as ``None`` and is never handed out, and the stream is + handed out again if it returns. """ return Following(self._inner.follow(path, instance)) diff --git a/py/moq-rs/tests/test_local.py b/py/moq-rs/tests/test_local.py index 0e8a01b560f..b84605c5297 100644 --- a/py/moq-rs/tests/test_local.py +++ b/py/moq-rs/tests/test_local.py @@ -28,9 +28,9 @@ def create_announced(origin: moq.OriginProducer, path: str) -> moq.BroadcastProd async def routes(announced: moq.AnnounceConsumer): - """Yield each newly announced route, skipping updates and ends.""" + """Yield each route that makes its path present, skipping absences.""" async for event in announced: - if isinstance(event, moq.AnnounceEventStart): + if isinstance(event, moq.AnnounceEventPresent): yield event.announce @@ -408,32 +408,48 @@ async def test_announced_broadcast(): break -async def test_announced_instance_restarts(): +async def test_following_restarts_on_a_new_instance(): origin = moq.OriginProducer() consumer = origin.consume() announced = consumer.announced() + following = consumer.follow("instance") publication = origin.create_broadcast("instance") # The broadcast mints its own instance and keeps it across re-announcing. publication.announce() - start = await next_route(announced) - assert isinstance(start, moq.AnnounceEventStart) - minted = start.announce.route.instance + present = await next_route(announced) + assert isinstance(present, moq.AnnounceEventPresent) + minted = present.announce.route.instance assert minted + assert await asyncio.wait_for(following.available(), timeout=5.0) is not None + # Another instance is another stream: followers restart onto it, while the path + # stays present, so neither the re-price nor the restart is listed. publication.announce(moq.AnnounceRoute(cost=1)) - update = await next_route(announced) - assert isinstance(update, moq.AnnounceEventUpdate) - assert update.announce.route.instance == minted - - # Another instance is another stream: consumers restart onto it. publication.announce(moq.AnnounceRoute(instance="cam", epoch=1)) - restart = await next_route(announced) - assert isinstance(restart, moq.AnnounceEventRestart) - assert restart.announce.route.instance == "cam" + assert await asyncio.wait_for(following.available(), timeout=5.0) is not None + publication.unannounce() + absent = await next_route(announced) + assert isinstance(absent, moq.AnnounceEventAbsent) + assert absent.announce.route.instance == "cam" with pytest.raises(moq.Error.InvalidInstance): publication.announce(moq.AnnounceRoute(instance="")) + following.cancel() + publication.broadcast.close() + + +async def test_following_reports_the_path_offline_until_it_returns(): + origin = moq.OriginProducer() + consumer = origin.consume() + publication = origin.create_broadcast("live") + publication.announce() + async with consumer.follow("live") as following: + assert await asyncio.wait_for(following.available(), timeout=5.0) is not None + publication.unannounce() + assert await asyncio.wait_for(following.available(), timeout=5.0) is None + publication.announce() + assert await asyncio.wait_for(following.available(), timeout=5.0) is not None publication.broadcast.close() @@ -442,7 +458,7 @@ async def test_finish_serves_until_closed(): consumer = origin.consume() announced = consumer.announced() broadcast = create_announced(origin, "finish") - assert isinstance(await next_route(announced), moq.AnnounceEventStart) + assert isinstance(await next_route(announced), moq.AnnounceEventPresent) track = broadcast.publish_track("status") track.write_frame(b"tail") @@ -456,7 +472,7 @@ async def test_finish_serves_until_closed(): assert frame is not None and frame.payload == b"tail" broadcast.close() - assert isinstance(await next_route(announced), moq.AnnounceEventEnd) + assert isinstance(await next_route(announced), moq.AnnounceEventAbsent) def test_publish_lifecycle(): @@ -1679,19 +1695,19 @@ async def test_broadcast_is_reachable_only_while_announced(): publication.announce() announced = consumer.announced() first = await next_route(announced) - assert isinstance(first, moq.AnnounceEventStart) + assert isinstance(first, moq.AnnounceEventPresent) assert first.announce.prefix == "live" publication.unannounce() retracted = await next_route(announced) - assert isinstance(retracted, moq.AnnounceEventEnd) + assert isinstance(retracted, moq.AnnounceEventAbsent) assert retracted.announce.prefix == "live" with pytest.raises(Exception): await asyncio.wait_for(consumer.request("live"), timeout=5.0) publication.announce() back = await next_route(announced) - assert isinstance(back, moq.AnnounceEventStart) + assert isinstance(back, moq.AnnounceEventPresent) await asyncio.wait_for(consumer.request("live"), timeout=5.0) announced.cancel() track.finish() @@ -1710,7 +1726,10 @@ async def test_an_instance_pins_request_and_follow(): with pytest.raises(moq.Error.InvalidInstance): consumer.follow("live", instance="") async with consumer.follow("live", instance="cam") as following: - await asyncio.wait_for(following.available(), timeout=5.0) + assert await asyncio.wait_for(following.available(), timeout=5.0) is not None + # Another instance taking the path over ends the pinned stream. + publication.announce(moq.AnnounceRoute(instance="other")) + assert await asyncio.wait_for(following.available(), timeout=5.0) is None publication.broadcast.close() @@ -1757,7 +1776,7 @@ async def test_announced_pattern_captures(): audio = create_announced(origin, "room/alice/audio") chat = create_announced(origin, "room/alice/chat") match = await next_route(announced) - assert isinstance(match, moq.AnnounceEventStart) + assert isinstance(match, moq.AnnounceEventPresent) assert match.announce.prefix == "room/alice/chat" assert match.announce.captures == ["alice"] diff --git a/py/moq-rs/tests/test_server.py b/py/moq-rs/tests/test_server.py index 1deab09e91d..076906ff84b 100644 --- a/py/moq-rs/tests/test_server.py +++ b/py/moq-rs/tests/test_server.py @@ -10,17 +10,12 @@ async def routes(announced: moq.AnnounceConsumer): - """Yield each newly announced route, skipping updates and ends.""" + """Yield each route that makes its path present, skipping absences.""" async for event in announced: - if isinstance(event, moq.AnnounceEventStart): + if isinstance(event, moq.AnnounceEventPresent): yield event.announce -async def next_route(announced: moq.AnnounceConsumer) -> moq.AnnounceEvent: - """The next announce event.""" - return await asyncio.wait_for(anext(announced), timeout=5.0) - - def opus_head() -> bytes: return ( b"OpusHead" @@ -480,50 +475,6 @@ async def test_broadcast_route_over_wire(): broadcast.close() -async def test_route_update_observes_restart(): - """A route metadata update arrives as an AnnounceEventUpdate. - - The publisher re-prices its announced route; the subscriber observes the new - cost in place (no retraction), and unannouncing retracts it. - """ - origin = moq.OriginProducer() - async with moq.Server("127.0.0.1:0", tls_generate=["localhost"], publish=origin) as server: - publication = origin.create_broadcast("routed") - publication.announce(moq.AnnounceRoute(cost=1)) - - serve_task = asyncio.create_task(server.serve()) - try: - async with moq.Client( - f"https://{server.local_addr}", - tls=moq.ClientTls(insecure=True), - bind="127.0.0.1:0", - ) as client: - announced = client.announced() - first = await next_route(announced) - assert isinstance(first, moq.AnnounceEventStart) - assert first.announce.prefix == "routed" - first_cost = first.announce.route.cost - - # The publisher raises its cost: an in-place update. - publication.announce(moq.AnnounceRoute(cost=5)) - updated = await next_route(announced) - assert isinstance(updated, moq.AnnounceEventUpdate) - assert updated.announce.prefix == "routed" - assert updated.announce.route.cost == first_cost + 4 - - # Unannouncing retracts the route. - publication.unannounce() - ended = await next_route(announced) - assert isinstance(ended, moq.AnnounceEventEnd) - assert ended.announce.prefix == "routed" - finally: - serve_task.cancel() - try: - await serve_task - except asyncio.CancelledError: - pass - - async def test_client_context_keeps_the_body_error(): """An error in the body survives the context manager, and the session is still cancelled. A live subscribed track never drains, so draining on the way out would diff --git a/quest/m1/announce-takeover.md b/quest/m1/announce-takeover.md index c9db540f92d..2de4418c268 100644 --- a/quest/m1/announce-takeover.md +++ b/quest/m1/announce-takeover.md @@ -49,12 +49,11 @@ this quest removes the predecessor machinery and grows to [XL]. `rs/moq-net/src/model/origin.rs` and `front.rs` (the `seen` stamps in `OriginConsumerState` and `PendingFront::via`); relay forwarding in `rs/moq-relay`; and `rs/moq-net/tests/integration/announce_takeover.rs`. - Remove the `Takeover` event from Rust (unless - [Listing and follow](/quest/m1/api-audit/route/events.md) has already - removed the event kinds), `TAKEOVER` and the predecessor fields from - `MoqAnnounceEvent` in `rs/moq-ffi/src/origin.rs` and from - `moq_announce_event` in `rs/moq-c/src/api.rs`, and their wrapper and - binding-doc mentions. In `js/net`, drop the ANNOUNCE_TAKEOVER decoding in + Remove `RouteEvent::Takeover` from the relay-level `routes()` stream + (`rs/moq-net/src/model/origin.rs`, and its folds in `follow.rs`, + `announced.rs` and the session publishers); the app listing, `MoqAnnounceEvent` + and `moq_announce_event` dropped it already in + [moq-dev/moq#7678](https://github.com/moq-dev/moq/pull/7678). In `js/net`, drop the ANNOUNCE_TAKEOVER decoding in `lite/announce.ts` and `lite/subscriber.ts`. Remove the takeover sections from `drafts/draft-lcurley-moq-lite.md`, `doc/concept/identity.md` and `doc/concept/moq-lite.md`. @@ -154,7 +153,6 @@ beyond [Instance and epoch](https://github.com/moq-dev/moq/pull/7489)'s fields. ## Related -- [Listing and follow](/quest/m1/api-audit/route/events.md) - continuity shows up inside `follow`, not as an event - [Claim answer epochs](https://github.com/moq-dev/moq/pull/5678) - why a restarted job must never be stitched to its predecessor - [Retired requests](https://github.com/moq-dev/moq/pull/5294) - replacements never substitute content into held handles - [Routes and announces](/quest/m1/cluster-routing/routes/README.md) - the route layer whose ANNOUNCE_START layout carries the instance diff --git a/quest/m1/api-audit/route/README.md b/quest/m1/api-audit/route/README.md index 0da644cc1c7..8a0b7c9c14c 100644 --- a/quest/m1/api-audit/route/README.md +++ b/quest/m1/api-audit/route/README.md @@ -168,7 +168,6 @@ stale, including `doc/concept/identity.md`. ## Required - [One Route handle](/quest/m1/api-audit/route/handle.md) - `Publication`, `Prepared`, `Announcer` and `Requests` become one `Route` that owns its broadcast, with app options separate from transit facts -- [Listing and follow](/quest/m1/api-audit/route/events.md) - apps read a path listing plus `follow`/`request`, and route metadata moves to a relay-level stream ## Related diff --git a/quest/m1/api-audit/route/events.md b/quest/m1/api-audit/route/events.md deleted file mode 100644 index 13416b5f679..00000000000 --- a/quest/m1/api-audit/route/events.md +++ /dev/null @@ -1,54 +0,0 @@ -# [M] Listing and follow - -## Goal - -Applications read announcements as a listing: each path is present or -absent. Instance changes, whether a restart or a takeover, are handled -inside `follow` and `request`, not seen as announce events. The `Start`, -`Update`, `End`, `Restart` and `Takeover` variants leave the app API in -Rust, JS, moq-ffi, moq-c and the wrappers. Route metadata changes (cost, hops, -preference) move to a relay-level `routes()` stream that moq-relay and -stats use. - -Non-goals: the wire's ANNOUNCE and ANNOUNCE_RESTART messages, which stay as -they are, and the route handle ([One Route handle](/quest/m1/api-audit/route/handle.md)). - -## Plan - -Decided 2026-10-30 (maintainer); see the [epic](/quest/m1/api-audit/route/README.md). -The audit found that almost every production caller merges the variants, -for example `rs/moq-relay/src/internal.rs:933`, `rs/moq-rtc/src/lib.rs:81` -and `rs/moq-cli/src/archive.rs:85`. Every JS consumer branches only on -`end`. Only `rs/moq-boy/src/input.rs:79` handles `Restart` alone, and it -moves to `follow`. - -- The listing yields present and absent per path, with the announcement's - read-only facts. A path that changes instance stays present. To see an - instance change, hold a `follow`. -- `follow` yields a new broadcast when the path's instance changes, as the - read side of [Instance and epoch](https://github.com/moq-dev/moq/pull/7489) - defines (an epoch change within one instance is invisible). It also needs an `End` - signal for when the path goes absent, so that `moq_mux::follow::Instances` - can fold into it: delete the wrapper's duplicate epoch filter and keep - its one-shot fixed mode only if a caller needs it. -- `routes()` lives on the origin consumer for relays and stats, and is - hidden from the app-level docs. -- Format the go and python interop clients while touching them (two lines - were already unformatted on 2026-10-30). -- `Takeover` (landed in [moq-dev/moq#6994](https://github.com/moq-dev/moq/pull/6994)) - goes with the rest, along with its predecessor fields in - `MoqAnnounceEvent` and `moq_announce_event`, unless - [announce takeover](/quest/m1/announce-takeover.md) has already removed it. -- Bindings: replace `MoqAnnounceEvent`'s variants, and the go and python - interop clients' and carla `publish.py:303-340`'s Restart handling, with - the listing plus `follow`. -- Update `doc/concept/identity.md`'s follower section and the binding docs. - -Run `just check` and `just test interop --all`. - -Public API: breaking across the announce stream in every language. Wire: -none. - -## Related - -- [Announce takeover](/quest/m1/announce-takeover.md) - a job's exact route continues its claim's readers inside `follow`, with no public event diff --git a/quest/m1/api-audit/route/handle.md b/quest/m1/api-audit/route/handle.md index adf63a37a88..edceeef3fb7 100644 --- a/quest/m1/api-audit/route/handle.md +++ b/quest/m1/api-audit/route/handle.md @@ -40,7 +40,8 @@ read-only facts on a received announcement, and `Hop`, `Hops` and `HopLimit` leave the public API. Non-goals: the instance model ([Instance and epoch](https://github.com/moq-dev/moq/pull/7489)), -announce events ([Listing and follow](/quest/m1/api-audit/route/events.md)), +announce events (a path listing plus `follow` since +[moq-dev/moq#7678](https://github.com/moq-dev/moq/pull/7678)), the read side, which instance owns, and the route layer's wire metric ([lite-07 route wire](/quest/m1/cluster-routing/routes/wire.md)). diff --git a/quest/m1/cluster-routing/routes/availability.md b/quest/m1/cluster-routing/routes/availability.md index eeeb9dbf560..30c4e8d0382 100644 --- a/quest/m1/cluster-routing/routes/availability.md +++ b/quest/m1/cluster-routing/routes/availability.md @@ -16,7 +16,7 @@ ends them. Bindings mirror Rust. route recovers; let-go reported as END, as today; and availability with let-go kept as END, which hides whether the origin ended or the network lost it. -- Build on [Listing and follow](/quest/m1/api-audit/route/events.md)'s +- Build on [Listing and follow](https://github.com/moq-dev/moq/pull/7678)'s listing and its offline report rather than adding event kinds beside it. - JS gets the API shape here, fed by what `js/net` knows today; the [js/net route layer](/quest/m1/cluster-routing/routes/js.md) feeds it from the wire. diff --git a/quest/m1/cluster-routing/routes/session.md b/quest/m1/cluster-routing/routes/session.md index c006ea8c030..ee92c083059 100644 --- a/quest/m1/cluster-routing/routes/session.md +++ b/quest/m1/cluster-routing/routes/session.md @@ -122,7 +122,6 @@ admission. - [lite-07 route wire](/quest/m1/cluster-routing/routes/wire.md) - the messages this drives - [One Route handle](/quest/m1/api-audit/route/handle.md) - the origin's route surface this rewrites -- [Listing and follow](/quest/m1/api-audit/route/events.md) - the announcement events this feeds ([moq-dev/moq#7678](https://github.com/moq-dev/moq/pull/7678)) ## Related diff --git a/quest/m2/follow-refused-retry.md b/quest/m2/follow-refused-retry.md index b01a7118d74..2b69a13bcf1 100644 --- a/quest/m2/follow-refused-retry.md +++ b/quest/m2/follow-refused-retry.md @@ -41,9 +41,10 @@ handler selected, so after each wake ask again only if the selected route entry is no longer the one that refused. Not chosen: a timed retry, which #7836's decision 2 (maintainer, 2026-11-04) ruled out. -Fold this into whatever `follow` becomes under -[Listing and follow](/quest/m1/api-audit/route/events.md), which moves -instance changes inside `follow`. If the path watch changes under +Build on `Following` as +[moq-dev/moq#7678](https://github.com/moq-dev/moq/pull/7678) left it, +which reports the path going offline beside its `refused` flag. If the +path watch changes under [front parking](/quest/m2/origin-front-parks.md), use its replacement. Tests: a refused follower asks the replacement after an overlapping @@ -56,6 +57,5 @@ Public API: none. Wire: none. ## Related -- [Listing and follow](/quest/m1/api-audit/route/events.md) - `follow` takes over instance changes from announce events - [Front parking](/quest/m2/origin-front-parks.md) - the same refusal wait for `request` - [The HLS server follows each path once](/quest/m2/hls-follow-once.md) - another `Following` addition diff --git a/quest/m2/room-bench.md b/quest/m2/room-bench.md index 13e7f1f87cd..dd91824a281 100644 --- a/quest/m2/room-bench.md +++ b/quest/m2/room-bench.md @@ -10,10 +10,9 @@ benchmark shows the change. ## Plan -Planned 2026-10-31 (`/quest-plan`, maintainer), a follow-up to the -required [Listing and follow](/quest/m1/api-audit/route/events.md) -implementation, after which `Room` will hold a follower per listed path and -poll every follower on each wake. That should be fine at room sizes seen +Planned 2026-10-31 (`/quest-plan`, maintainer), a follow-up to +[Listing and follow](https://github.com/moq-dev/moq/pull/7678), after which +`Room` holds a follower per listed path and polls every follower on each wake. That should be fine at room sizes seen today. Benchmark first, as [moq-dev/moq#7340](https://github.com/moq-dev/moq/pull/7340) did for the lite subscriber's prefixes, and fix @@ -21,7 +20,3 @@ only if it shows a slope. Wire the benchmark into the existing bench lanes ([Benchmark sources](/quest/m2/bench-sources.md)). Public API: none. Wire: none. - -## Required - -- [Listing and follow](/quest/m1/api-audit/route/events.md) - the follower-per-path room this measures diff --git a/rs/hang/examples/subscribe.rs b/rs/hang/examples/subscribe.rs index 5445d1863b2..f11c2f36698 100644 --- a/rs/hang/examples/subscribe.rs +++ b/rs/hang/examples/subscribe.rs @@ -37,34 +37,27 @@ async fn run_session(origin: moq_net::origin::Producer) -> anyhow::Result<()> { Ok(reconnect.closed().await?) } -// Follow the announced broadcast: play it once it starts, play it afresh when another -// publisher instance replaces it (a restart), keep playing when a route takes it over in -// place, and stop when it ends. +// List the announced broadcasts and play the first: follow it across publisher instances, +// playing afresh when another replaces it, until it goes offline. async fn run_subscribe(consumer: moq_net::origin::Consumer) -> anyhow::Result<()> { let mut announced = consumer.announced(); let mut playing: Option> = None; while let Some(event) = announced.next().await { match event { - // The same broadcast continued in place: nothing to do. - moq_net::announce::Event::Takeover { announce, predecessor } if predecessor == announce.prefix => {} // The convention is that a publisher announces each broadcast's exact path. - moq_net::announce::Event::Start(announce) - | moq_net::announce::Event::Restart(announce) - | moq_net::announce::Event::Takeover { announce, .. } => { + moq_net::announce::Event::Present(announce) => { tracing::info!(path = %announce.prefix, "broadcast announced"); if let Some(old) = playing.take() { old.abort(); } let consumer = consumer.clone(); playing = Some(tokio::spawn(async move { - if let Err(err) = play(consumer, announce).await { + if let Err(err) = follow(consumer, announce.prefix).await { tracing::warn!(%err, "playback failed"); } })); } - // The same broadcast over another route: nothing to do. - moq_net::announce::Event::Update(_) => {} - moq_net::announce::Event::End(announce) => { + moq_net::announce::Event::Absent(announce) => { tracing::info!(path = %announce.prefix, "broadcast ended"); if let Some(old) = playing.take() { old.abort(); @@ -75,10 +68,27 @@ async fn run_subscribe(consumer: moq_net::origin::Consumer) -> anyhow::Result<() anyhow::bail!("origin closed") } -// Resolve an announced broadcast and read media frames. -async fn play(consumer: moq_net::origin::Consumer, announce: moq_net::announce::Announce) -> anyhow::Result<()> { - let broadcast = consumer.request(&announce.prefix, announce.route.instance).await?; +// Play each publisher instance serving `path`, starting afresh when another replaces it. +async fn follow(consumer: moq_net::origin::Consumer, path: moq_net::Path) -> anyhow::Result<()> { + let mut following = consumer.follow(&path, None)?; + let mut next = following.available().await?; + while let Some(broadcast) = next { + tracing::info!(%path, instance = ?broadcast.info().instance, "broadcast online"); + next = tokio::select! { + played = play(broadcast) => { + if let Err(err) = played { + tracing::warn!(%err, "playback failed"); + } + following.available().await? + } + changed = following.available() => changed?, + }; + } + Ok(()) +} +// Read the catalog and media frames of one publisher instance. +async fn play(broadcast: moq_net::broadcast::Consumer) -> anyhow::Result<()> { // Read the catalog to discover available tracks. let mut catalog = moq_mux::catalog::hang::Consumer::<()>::new(broadcast.track(hang::Catalog::DEFAULT_NAME)?); diff --git a/rs/moq-audio/src/decode/consumer.rs b/rs/moq-audio/src/decode/consumer.rs index c596a70683b..af8671cf438 100644 --- a/rs/moq-audio/src/decode/consumer.rs +++ b/rs/moq-audio/src/decode/consumer.rs @@ -996,7 +996,13 @@ mod tests { let relay = origin(2); let _link = link(&publisher, &relay).await; let consumer = relay.consume(); - let remote = consumer.follow("bcast", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); // A first reader warms the relay's copy with every group. { @@ -1068,7 +1074,13 @@ mod tests { let subscriber = origin(2); let _link = link(&publisher, &subscriber).await; let consumer = subscriber.consume(); - let remote = consumer.follow("bcast", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, 48_000, 1); catalog.warmup = Some(Duration::from_millis(80)); diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index ea1e24004a0..23727f18165 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -912,6 +912,7 @@ mod tests { .unwrap() .available() .await + .unwrap() .unwrap(); let source = moq_mux::Source::new(origin.consume(), remote); let (broadcaster, export) = diff --git a/rs/moq-bench/src/connection.rs b/rs/moq-bench/src/connection.rs index 0fa2d0993fb..0707226ff22 100644 --- a/rs/moq-bench/src/connection.rs +++ b/rs/moq-bench/src/connection.rs @@ -317,7 +317,11 @@ async fn subscribe_named( max_delay: Duration, stats: Arc, ) -> anyhow::Result<()> { - let broadcast = consume.follow(path.as_str(), None)?.available().await?; + let broadcast = consume + .follow(path.as_str(), None)? + .available() + .await? + .ok_or_else(|| anyhow::anyhow!("the first change is an instance"))?; drain(broadcast, max_delay, &stats).await } @@ -359,11 +363,9 @@ async fn subscribe( update = announced.next() => { let update = match update { Some( - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) | moq_net::announce::Event::Takeover { announce: update, .. }, + moq_net::announce::Event::Present(update), ) => update, - Some(moq_net::announce::Event::End(_)) => continue, + Some(moq_net::announce::Event::Absent(_)) => continue, None => break, }; let path = update.prefix.to_string(); @@ -388,13 +390,8 @@ async fn subscribe( // there is nothing to spread over. while selected < want { let update = match announced.next().await { - Some( - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) - | moq_net::announce::Event::Takeover { announce: update, .. }, - ) => update, - Some(moq_net::announce::Event::End(_)) => continue, + Some(moq_net::announce::Event::Present(update)) => update, + Some(moq_net::announce::Event::Absent(_)) => continue, None => break, }; let path = update.prefix.to_string(); diff --git a/rs/moq-boy/src/input.rs b/rs/moq-boy/src/input.rs index 606a2359174..e50d8612d8f 100644 --- a/rs/moq-boy/src/input.rs +++ b/rs/moq-boy/src/input.rs @@ -65,63 +65,67 @@ pub async fn handle_viewers( cmd_tx: &tokio::sync::mpsc::Sender, ) -> anyhow::Result<()> { let mut announced = viewer_origin.announced(); - // The command reader per viewer, so a restarted viewer replaces the old one's. + // The follower per viewer, which reads each broadcast a restarted viewer replaces the old with. let mut readers: std::collections::HashMap = Default::default(); - loop { - let (update, active, restart) = match announced.next().await { - // A takeover continues what was read through its predecessor. - Some( - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Takeover { announce: update, .. }, - ) => (update, true, false), - // The viewer restarted: read the new broadcast instead of the old one. - Some(moq_net::announce::Event::Restart(update)) => (update, true, true), - Some(moq_net::announce::Event::End(update)) => (update, false, false), - None => break, - }; - - let viewer_id = update.prefix.to_string(); - - if active { - if !restart && readers.get(&viewer_id).is_some_and(|reader| !reader.is_finished()) { - continue; - } - let Ok(broadcast) = viewer_origin.request(&update.prefix, None).await else { - continue; - }; - tracing::info!(%viewer_id, restart, "viewer connected"); - if let Some(old) = readers.remove(&viewer_id) { - old.abort(); + while let Some(event) = announced.next().await { + match event { + moq_net::announce::Event::Present(announce) => { + let viewer_id = announce.prefix.to_string(); + let reader = tokio::spawn(follow_viewer(viewer_origin.clone(), announce.prefix, cmd_tx.clone())); + if let Some(old) = readers.insert(viewer_id, reader.abort_handle()) { + old.abort(); + } } - let cmd_tx = cmd_tx.clone(); - let vid = viewer_id.clone(); - let reader = tokio::spawn(async move { - if let Err(e) = handle_viewer_commands(&vid, broadcast, &cmd_tx).await { - tracing::warn!(viewer_id = %vid, error = %e, "viewer command error"); + moq_net::announce::Event::Absent(announce) => { + let viewer_id = announce.prefix.to_string(); + // Dropping an abort handle leaves its task running, so stop it here. + if let Some(old) = readers.remove(&viewer_id) { + old.abort(); } - tracing::info!(viewer_id = %vid, "viewer disconnected"); - let _ = cmd_tx.send(Command::ViewerLeft { viewer_id: vid }).await; - }); - readers.insert(viewer_id, reader.abort_handle()); - } else { - // Dropping an abort handle leaves its task running. Older versions send a restart - // as END then START, and the old track keeps flowing meanwhile, so the old reader - // must stop here or it would race the replacement. - if let Some(old) = readers.remove(&viewer_id) { - old.abort(); + tracing::info!(%viewer_id, "viewer went offline"); + let _ = cmd_tx.send(Command::ViewerLeft { viewer_id }).await; } - tracing::info!(%viewer_id, "viewer went offline"); - let _ = cmd_tx - .send(Command::ViewerLeft { - viewer_id: viewer_id.clone(), - }) - .await; } } Ok(()) } +/// Read the commands of each broadcast a viewer publishes at `path`, switching to the new one +/// when the viewer restarts. A refused resolution waits for the serving route to change, since +/// the listing reports nothing new while the path stays present. +async fn follow_viewer( + origin: moq_net::origin::Consumer, + path: moq_net::Path, + cmd_tx: tokio::sync::mpsc::Sender, +) { + let viewer_id = path.to_string(); + let Ok(mut following) = origin.follow(&path, None) else { + return; + }; + let mut reading = None; + loop { + tokio::select! { + change = following.available() => match change { + Ok(Some(broadcast)) => { + tracing::info!(%viewer_id, "viewer connected"); + reading = Some(Box::pin(handle_viewer_commands(&viewer_id, broadcast, &cmd_tx))); + } + Ok(None) => {} + Err(moq_net::Error::Closed) => return, + Err(err) => tracing::debug!(%viewer_id, %err, "viewer not resolved; waiting for a route change"), + }, + read = async { reading.as_mut().expect("guarded").await }, if reading.is_some() => { + reading = None; + if let Err(e) = read { + tracing::warn!(%viewer_id, error = %e, "viewer command error"); + } + tracing::info!(%viewer_id, "viewer disconnected"); + let _ = cmd_tx.send(Command::ViewerLeft { viewer_id: viewer_id.clone() }).await; + } + } + } +} + async fn handle_viewer_commands( viewer_id: &str, broadcast: moq_net::broadcast::Consumer, @@ -266,4 +270,29 @@ mod tests { kio::clock::sleep(Duration::from_secs(1)).await; assert!(rx.try_recv().is_err(), "the old reader is still running"); } + + /// A viewer whose handler refuses it stays followed: the listing reports nothing new when a + /// publisher takes the path over, so only the follower can pick it up. + #[tokio::test(start_paused = true)] + async fn a_refused_viewer_is_read_once_a_publisher_takes_over() { + let (origin, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::default()); + tokio::spawn(moq_net::time::run(driver)); + let (tx, mut rx) = tokio::sync::mpsc::channel(16); + let consumer = origin.consume(); + tokio::spawn(async move { handle_viewers(&consumer, &tx).await }); + + let claim = origin.requests("alice").unwrap(); + kio::clock::timeout(Duration::from_secs(1), claim.requested_broadcast()) + .await + .expect("never asked") + .expect("the claim closed") + .reject(moq_net::Error::NotFound); + kio::clock::sleep(Duration::from_secs(1)).await; + + let (_viewer, track) = viewer(&origin); + kio::clock::sleep(Duration::from_secs(1)).await; + send(&track, r#"{"type":"reset"}"#); + let next = kio::clock::timeout(Duration::from_secs(1), rx.recv()).await; + assert!(matches!(next, Ok(Some(Command::Reset))), "{next:?}"); + } } diff --git a/rs/moq-c/MIGRATE.md b/rs/moq-c/MIGRATE.md index 143245df61b..104b94f772b 100644 --- a/rs/moq-c/MIGRATE.md +++ b/rs/moq-c/MIGRATE.md @@ -4,6 +4,14 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Announcements are a listing ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`moq_announce_kind` is `MOQ_ANNOUNCE_KIND_PRESENT` (0) or `MOQ_ANNOUNCE_KIND_ABSENT` (1), and `moq_announce_event` loses `predecessor` and `predecessor_len`, changing the C ABI. +Another instance taking a path over keeps it present, and a route's cost changing is not reported. +Map `_START` to `_PRESENT` and `_END` to `_ABSENT`, and drop `_UPDATE` handling. Where you requested the path again on `_RESTART` or `_TAKEOVER`, call `moq_origin_follow`, whose callback delivers a broadcast handle for each new instance. + +`moq_origin_follow` takes a `moq_follow_callback`, `(void *user_data, moq_follow_event event, int32_t code)`, instead of a `moq_status_callback`. `MOQ_FOLLOW_EVENT_ONLINE` carries each broadcast handle (where a positive code did), `MOQ_FOLLOW_EVENT_OFFLINE` reports the path going offline (or, for a follow pinned to an instance, another instance taking it over) with code `0`, and `MOQ_FOLLOW_EVENT_END` is the terminal callback, with `0` or a negative error that `moq_error()` explains (where a code `<= 0` was). Release `user_data` on `MOQ_FOLLOW_EVENT_END`. + ### Separate datagram tracks Create a separate producer with `moq_publish_datagrams` and `moq_datagram_info`; replace `moq_publish_track_datagram` with `moq_publish_datagrams_append` on that handle, or `moq_publish_datagrams_insert` for an explicit sequence. `moq_consume_datagrams` now takes a subscriber priority before its callback and never subscribes to a stream track. Use `moq_publish_datagrams_demand` and `moq_datagram_demand_priority` for aggregate priority, and `moq_publish_datagrams_requests` / `moq_datagram_request_accept` for on-demand publishing. Finish or abort datagram producers separately. A zeroed datagram info is untimed, unlike the microsecond default of stream-track info. `moq_consume_datagrams` returns a negative code at once when the broadcast has neither the datagram track nor a datagram request handler, and a remote name without one ends its callback with a not-found error. diff --git a/rs/moq-c/README.md b/rs/moq-c/README.md index 445f6e51427..602d93adfd9 100644 --- a/rs/moq-c/README.md +++ b/rs/moq-c/README.md @@ -64,7 +64,7 @@ int32_t moq_origin_close(uint32_t origin); int32_t moq_origin_create_broadcast(uint32_t origin, const char *path, uintptr_t path_len, uint32_t *broadcast); int32_t moq_origin_request(uint32_t origin, const char *path, uintptr_t path_len, const char *instance, uintptr_t instance_len, moq_status_callback on_broadcast, void *user_data); int32_t moq_origin_request_cancel(uint32_t task); -int32_t moq_origin_follow(uint32_t origin, const char *path, uintptr_t path_len, const char *instance, uintptr_t instance_len, moq_status_callback on_broadcast, void *user_data); +int32_t moq_origin_follow(uint32_t origin, const char *path, uintptr_t path_len, const char *instance, uintptr_t instance_len, moq_follow_callback on_follow, void *user_data); int32_t moq_origin_follow_cancel(uint32_t task); int32_t moq_origin_announced(uint32_t origin, const moq_announce_config *config, moq_status_callback on_announce, void *user_data); int32_t moq_origin_announced_info(uint32_t announced, moq_announce_event *dst); diff --git a/rs/moq-c/src/api.rs b/rs/moq-c/src/api.rs index b7190ab3e67..f00cb6bf943 100644 --- a/rs/moq-c/src/api.rs +++ b/rs/moq-c/src/api.rs @@ -935,8 +935,8 @@ pub struct moq_announce_route { /// publisher is a new stream viewers restart onto. Set one only for a publisher that /// continues its group sequences across restarts and raises `epoch` on each: readers /// stitch every route of one instance together, so restarting the sequences under an - /// old instance splices unrelated groups into their tracks. Changing it is a - /// `MOQ_ANNOUNCE_KIND_RESTART`. + /// old instance splices unrelated groups into their tracks. Changing it hands followers + /// ([moq_origin_follow]) a new broadcast. pub instance: *const c_char, /// The length in bytes of `instance`. pub instance_len: usize, @@ -1006,22 +1006,14 @@ pub struct moq_announce_config { #[expect(non_camel_case_types, reason = "C ABI type names match moq.h")] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum moq_announce_kind { - /// A route now covers the prefix; the listener had none there. - MOQ_ANNOUNCE_KIND_START = 0, - /// The route covering the prefix changed hops or cost. - MOQ_ANNOUNCE_KIND_UPDATE = 1, - /// No route covers the prefix any more. - MOQ_ANNOUNCE_KIND_END = 2, - /// Another publisher instance now serves the prefix: drop what was resolved under - /// it and request afresh. - MOQ_ANNOUNCE_KIND_RESTART = 3, - /// A route now covers the prefix and continues what the route at - /// [moq_announce_event::predecessor] served beneath it: keep what was resolved through it. - MOQ_ANNOUNCE_KIND_TAKEOVER = 4, -} - -/// An announce event from an origin: a route starting, updating, ending, restarting, or -/// taking over. + /// A route now covers the path, which was absent. Another instance taking it over keeps it + /// present: follow it with [moq_origin_follow] to see the broadcast change. + MOQ_ANNOUNCE_KIND_PRESENT = 0, + /// No route covers the path any more. + MOQ_ANNOUNCE_KIND_ABSENT = 1, +} + +/// An announce event from an origin: a path becoming present or absent. #[repr(C)] pub struct moq_announce_event { /// The covered prefix, relative to the origin, NOT NULL terminated @@ -1038,14 +1030,9 @@ pub struct moq_announce_event { /// Which event this is. pub kind: moq_announce_kind, - /// The route serving the prefix. On `MOQ_ANNOUNCE_KIND_END` it carries the last + /// The route serving the prefix. On `MOQ_ANNOUNCE_KIND_ABSENT` it carries the last /// advertised route. pub route: moq_route, - /// For [moq_announce_kind::MOQ_ANNOUNCE_KIND_TAKEOVER], the prefix of the route continued, - /// relative to the origin, NOT NULL terminated. Empty for every other kind. - pub predecessor: *const c_char, - /// The length in bytes of `predecessor`. - pub predecessor_len: usize, } /// Where a session is in its lifecycle, reported by [moq_connection_state]. @@ -2301,34 +2288,50 @@ pub extern "C" fn moq_origin_announced_cancel(announced: u32) -> i32 { }) } +/// What a [moq_origin_follow] callback reports. +#[repr(C)] +#[expect(non_camel_case_types, reason = "C ABI type names match moq.h")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum moq_follow_event { + /// An instance now serves the path. `code` is its broadcast handle. + MOQ_FOLLOW_EVENT_ONLINE = 0, + /// The path went offline, or, for a pinned follow, another instance took it over. `code` + /// is `0`. + MOQ_FOLLOW_EVENT_OFFLINE = 1, + /// The follow ended. `code` is `0` (after [moq_origin_follow_cancel], or the origin closing + /// once a handle was delivered) or a negative error. + MOQ_FOLLOW_EVENT_END = 2, +} + /// Follow a broadcast on an origin by path, delivering each publisher instance that serves it. /// /// Resolves against future announcements: it waits for the announcement to arrive (e.g. over the -/// network) and then delivers the broadcast handle via `on_broadcast`. Use it right after +/// network) and then delivers the broadcast handle via `on_follow`. Use it right after /// [moq_session_connect] to avoid racing announcement gossip. To resolve one publisher instance /// instead, use [moq_origin_request]. A broadcast created on this origin resolves once it is /// announced, like a remote one. /// /// `instance` is the only stream to follow, or NULL (or empty) to follow every instance serving -/// the path. A pinned follow never delivers another instance taking over, and delivers its own -/// again if it returns after an end. -/// -/// `on_broadcast` is invoked with a positive broadcast handle once announced, and again with a -/// new handle each time another publisher instance takes over (a restarted publisher, or a -/// replacement session) or the broadcast returns after it went offline. Drop what was read -/// from the previous handle then: its tracks end when its session does. The wait ends with -/// exactly one terminal code: `0` (after [moq_origin_follow_cancel], or the origin closing -/// once a handle was delivered) or a negative error. After the terminal (`<= 0`) callback, -/// `on_broadcast` is never called again and `user_data` is never touched again, so release -/// `user_data` there. The broadcast handle is usable with [moq_consume_catalog] / [moq_consume_track] -/// and must be freed separately with [moq_consume_close]. -/// -/// Returns a non-zero handle to the wait on success, or a negative code on (immediate) failure. +/// the path. A pinned follow never delivers another instance taking over: that ends it, and its +/// own instance is delivered again when it returns. +/// +/// `on_follow` receives a [moq_follow_event] and a code. `MOQ_FOLLOW_EVENT_ONLINE` carries a +/// broadcast handle once announced, and again a new one each time another publisher instance +/// takes over (a restarted publisher, or a replacement session) or the broadcast returns. +/// `MOQ_FOLLOW_EVENT_OFFLINE` reports the path going offline, before whatever serves it next. +/// Drop what was read from the previous handle on either: its tracks end when its session does. +/// The follow ends with exactly one `MOQ_FOLLOW_EVENT_END`, whose code is `0` or a negative +/// error that [moq_error] explains inside the callback. After it, `on_follow` is never called +/// again and `user_data` is never touched again, so release `user_data` there. Each broadcast +/// handle is usable with [moq_consume_catalog] / [moq_consume_track] and must be freed separately +/// with [moq_consume_close]. +/// +/// Returns a non-zero handle to the follow on success, or a negative code on (immediate) failure. /// /// # Safety /// - The caller must ensure that path is a valid pointer to path_len bytes of data. /// - `instance` may be NULL, or must point at `instance_len` readable bytes. -/// - The caller must keep `user_data` valid until the terminal (`<= 0`) `on_broadcast` callback. +/// - The caller must keep `user_data` valid until the `MOQ_FOLLOW_EVENT_END` callback. #[unsafe(no_mangle)] pub unsafe extern "C" fn moq_origin_follow( origin: u32, @@ -2336,30 +2339,30 @@ pub unsafe extern "C" fn moq_origin_follow( path_len: usize, instance: *const c_char, instance_len: usize, - on_broadcast: ffi::moq_status_callback, + on_follow: ffi::moq_follow_callback, user_data: *mut c_void, ) -> i32 { ffi::enter(move || { let origin = ffi::parse_id(origin)?; let path = unsafe { ffi::parse_str(path, path_len)? }.to_string(); let instance = unsafe { parse_instance(instance, instance_len)? }; - let on_broadcast = unsafe { ffi::OnStatus::new(user_data, on_broadcast)? }; - State::lock().origin.follow(origin, path, instance, on_broadcast) + let on_follow = unsafe { ffi::OnFollow::new(user_data, on_follow)? }; + State::lock().origin.follow(origin, path, instance, on_follow) }) } -/// Abort a wait started by [moq_origin_follow]. +/// Stop a follow started by [moq_origin_follow]. /// /// Returns immediately: zero on success, or a negative code if already closed. Does NOT free -/// `user_data`. The [moq_origin_follow] `on_broadcast` callback still fires once more -/// with a terminal `0` (or a negative error), and that final callback is where `user_data` should -/// be released. Any broadcast handle already delivered is unaffected and must still be freed with -/// [moq_consume_close]. +/// `user_data`. The [moq_origin_follow] `on_follow` callback still fires once more with +/// `MOQ_FOLLOW_EVENT_END` (code `0`, or a negative error), and that final callback is where +/// `user_data` should be released. Any broadcast handle already delivered is unaffected and must +/// still be freed with [moq_consume_close]. #[unsafe(no_mangle)] pub extern "C" fn moq_origin_follow_cancel(task: u32) -> i32 { ffi::enter(move || { let task = ffi::parse_id(task)?; - State::lock().origin.resolve_close(task) + State::lock().origin.follow_close(task) }) } diff --git a/rs/moq-c/src/ffi.rs b/rs/moq-c/src/ffi.rs index be21fc414ee..ca0a3d78553 100644 --- a/rs/moq-c/src/ffi.rs +++ b/rs/moq-c/src/ffi.rs @@ -20,6 +20,16 @@ pub type moq_status_callback = Option; +/// Callback for [crate::moq_origin_follow]: each [crate::moq_follow_event] and its code. +/// +/// `code` is the broadcast handle for `MOQ_FOLLOW_EVENT_ONLINE`, `0` for +/// `MOQ_FOLLOW_EVENT_OFFLINE`, and the terminal status for `MOQ_FOLLOW_EVENT_END`: `0`, or a +/// negative error whose reason [crate::moq_error] reports inside the callback. +/// +/// Runs on moq-c's runtime thread. The callback and `user_data` must be safe to use on that thread. +#[expect(non_camel_case_types, reason = "C ABI type names match moq.h")] +pub type moq_follow_callback = Option; + pub static RUNTIME: LazyLock = LazyLock::new(|| { let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() @@ -270,6 +280,58 @@ impl OnState { // safe to use on moq-c's runtime thread until the terminal callback. unsafe impl Send for OnState {} +/// A [moq_follow_callback] and its user data. +#[derive(Clone, Copy)] +pub struct OnFollow { + user_data: *mut c_void, + on_follow: extern "C" fn(user_data: *mut c_void, event: crate::moq_follow_event, code: i32), +} + +impl OnFollow { + /// Wrap a C follow callback. + /// + /// # Safety + /// - The caller must keep `user_data` valid until the `MOQ_FOLLOW_EVENT_END` callback. + /// - The callback and `user_data` must be safe to use on moq-c's runtime thread. + pub unsafe fn new(user_data: *mut c_void, on_follow: moq_follow_callback) -> Result { + Ok(Self { + user_data, + on_follow: on_follow.ok_or(Error::InvalidPointer)?, + }) + } + + /// Deliver a broadcast handle for an instance now serving the path. + pub fn online(&self, broadcast: crate::Id) { + (self.on_follow)( + self.user_data, + crate::moq_follow_event::MOQ_FOLLOW_EVENT_ONLINE, + i32::from(broadcast), + ); + } + + /// Report that the path went offline. + pub fn offline(&self) { + (self.on_follow)(self.user_data, crate::moq_follow_event::MOQ_FOLLOW_EVENT_OFFLINE, 0); + } +} + +impl crate::task::Terminal for OnFollow { + /// Deliver the terminal status, recording its failure first so the callback can read + /// `moq_error()`. + fn terminal(&self, res: Result<(), Error>) { + record_error(&res); + (self.on_follow)( + self.user_data, + crate::moq_follow_event::MOQ_FOLLOW_EVENT_END, + res.code(), + ); + } +} + +// SAFETY: The constructor requires the callback and user_data to remain valid and +// safe to use on moq-c's runtime thread until the terminal callback. +unsafe impl Send for OnFollow {} + /// Types that can be converted to C-compatible return codes. pub trait ReturnCode { /// Convert to an i32 status code. diff --git a/rs/moq-c/src/lib.rs b/rs/moq-c/src/lib.rs index 194b44cdd98..b89a6a8f0f1 100644 --- a/rs/moq-c/src/lib.rs +++ b/rs/moq-c/src/lib.rs @@ -42,7 +42,7 @@ pub use bandwidth::*; pub use datagram::*; pub use decode::{moq_decode_gap, moq_decode_read_kind, moq_decode_start}; pub use error::*; -pub use ffi::{moq_state_callback, moq_status_callback}; +pub use ffi::{moq_follow_callback, moq_state_callback, moq_status_callback}; pub use id::*; pub use video::*; diff --git a/rs/moq-c/src/origin.rs b/rs/moq-c/src/origin.rs index 2463579830a..b1eb57a1e4e 100644 --- a/rs/moq-c/src/origin.rs +++ b/rs/moq-c/src/origin.rs @@ -1,7 +1,7 @@ use std::ffi::c_char; use tokio::sync::oneshot; -use crate::ffi::OnStatus; +use crate::ffi::{OnFollow, OnStatus}; use crate::task::Task; use crate::{Error, Id, NonZeroSlab, State, moq_announce_event, moq_announce_kind, moq_route, moq_string}; @@ -22,9 +22,12 @@ pub struct Origin { /// Announcement listener tasks. Close signals shutdown; the task delivers a final callback, then removes itself. announced_task: NonZeroSlab, - /// Pending consume-until-announced tasks. Close signals shutdown; the task delivers a final callback, then removes itself. + /// Pending requests. Close signals shutdown; the task delivers a final callback, then removes itself. consume_task: NonZeroSlab, + /// Follows. Close signals shutdown; the task delivers its end event, then removes itself. + follow_task: NonZeroSlab>, + /// Served routes from [Self::dynamic], retracted when the handle is closed. dynamic: NonZeroSlab, @@ -81,7 +84,6 @@ struct AnnouncedRecord { cost: u64, /// The route's instance. instance: String, - predecessor: String, } // The raw pointers only borrow immutable String allocations owned by this record. @@ -91,16 +93,9 @@ unsafe impl Send for AnnouncedRecord {} impl AnnouncedRecord { fn new(event: moq_net::announce::Event) -> Self { use moq_net::announce::Event; - let (update, kind, predecessor) = match event { - Event::Start(update) => (update, moq_announce_kind::MOQ_ANNOUNCE_KIND_START, String::new()), - Event::Update(update) => (update, moq_announce_kind::MOQ_ANNOUNCE_KIND_UPDATE, String::new()), - Event::End(update) => (update, moq_announce_kind::MOQ_ANNOUNCE_KIND_END, String::new()), - Event::Restart(update) => (update, moq_announce_kind::MOQ_ANNOUNCE_KIND_RESTART, String::new()), - Event::Takeover { announce, predecessor } => ( - announce, - moq_announce_kind::MOQ_ANNOUNCE_KIND_TAKEOVER, - predecessor.to_string(), - ), + let (update, kind) = match event { + Event::Present(update) => (update, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT), + Event::Absent(update) => (update, moq_announce_kind::MOQ_ANNOUNCE_KIND_ABSENT), }; let captures: Vec<_> = update .captures @@ -128,7 +123,6 @@ impl AnnouncedRecord { .instance .map(|instance| instance.to_string()) .unwrap_or_default(), - predecessor, } } } @@ -217,8 +211,6 @@ impl Origin { len: announced.instance.len(), }, }, - predecessor: announced.predecessor.as_ptr().cast::(), - predecessor_len: announced.predecessor.len(), }; Ok(()) } @@ -241,33 +233,37 @@ impl Origin { /// Follow the broadcast at `path`, delivering each publisher instance's handle via the callback. /// - /// The callback fires a broadcast handle (> 0) for each instance serving the path, until a - /// single terminal code (`0` on close, negative on error). Returns a task handle for - /// cancellation via [`Self::resolve_close`]. + /// The callback reports each instance serving the path as online with its broadcast handle, + /// the path going offline, and one end with `0` on close or a negative error. Returns a task + /// handle for cancellation via [`Self::follow_close`]. pub fn follow( &mut self, origin: Id, path: String, instance: Option, - on_broadcast: OnStatus, + on_follow: OnFollow, ) -> Result { let origin = self.active.get(origin).ok_or(Error::OriginNotFound)?; origin.driver.check()?; let driver = origin.driver.clone(); let consumer = origin.consume_owned(); - self.consume_task.spawn( - on_broadcast, - |state| &mut state.origin.consume_task, + self.follow_task.spawn( + on_follow, + |state| &mut state.origin.follow_task, |close| async move { driver - .watch(Self::run_follow(on_broadcast, consumer, path, instance, close)) + .watch(Self::run_follow(on_follow, consumer, path, instance, close)) .await }, ) } + pub fn follow_close(&mut self, task: Id) -> Result<(), Error> { + self.follow_task.close(task, Error::NotFound) + } + async fn run_follow( - callback: OnStatus, + callback: OnFollow, consumer: Consumer, path: String, instance: Option, @@ -288,7 +284,11 @@ impl Origin { biased; _ = &mut close => return Ok(()), available = announced.available() => match available { - Ok(broadcast) => broadcast, + Ok(Some(broadcast)) => broadcast, + Ok(None) => { + callback.offline(); + continue; + } // The origin closing ends the wait, cleanly once a handle was delivered. Err(moq_net::Error::Closed) if delivered => return Ok(()), Err(moq_net::Error::Closed) => return Err(Error::BroadcastNotFound), @@ -299,7 +299,7 @@ impl Origin { // Hold the lock only to buffer the broadcast; release it before the callback. let broadcast_id = State::lock().consume.start(broadcast, Some(consumer.clone()))?; delivered = true; - callback.call(broadcast_id); + callback.online(broadcast_id); } } diff --git a/rs/moq-c/src/task.rs b/rs/moq-c/src/task.rs index 58fde665388..7a0e41c0dc9 100644 --- a/rs/moq-c/src/task.rs +++ b/rs/moq-c/src/task.rs @@ -32,13 +32,25 @@ impl Task { } } -impl NonZeroSlab { +/// A task's callback, which receives the task's result once it ends. +pub(crate) trait Terminal: Copy + Send + 'static { + /// Deliver the terminal result. + fn terminal(&self, res: Result<(), Error>); +} + +impl Terminal for OnStatus { + fn terminal(&self, res: Result<(), Error>) { + self.call(res); + } +} + +impl NonZeroSlab> { /// Spawn `run` with its close receiver, then deliver its result as the terminal callback. /// /// `slab` finds this slab again from the task, so it can remove its own entry. pub fn spawn( &mut self, - callback: OnStatus, + callback: C, slab: fn(&mut State) -> &mut Self, run: impl FnOnce(oneshot::Receiver<()>) -> F, ) -> Result @@ -55,7 +67,7 @@ impl NonZeroSlab { // Pull the entry out from under the lock so the callback never runs while held. let entry = slab(&mut State::lock()).remove(id); if let Some(entry) = entry { - entry.callback.call(res); + entry.callback.terminal(res); } }); diff --git a/rs/moq-c/src/test.rs b/rs/moq-c/src/test.rs index dc416e220b2..c49fd18d9a8 100644 --- a/rs/moq-c/src/test.rs +++ b/rs/moq-c/src/test.rs @@ -165,7 +165,7 @@ fn announce_info(announced: u32) -> moq_announce_event { prefix_len: 0, captures: std::ptr::null(), captures_len: 0, - kind: moq_announce_kind::MOQ_ANNOUNCE_KIND_START, + kind: moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT, route: moq_route { hops: std::ptr::null(), hops_len: 0, @@ -175,8 +175,6 @@ fn announce_info(announced: u32) -> moq_announce_event { len: 0, }, }, - predecessor: std::ptr::null(), - predecessor_len: 0, }; assert_eq!(unsafe { moq_origin_announced_info(announced, &mut info) }, 0); info @@ -222,6 +220,34 @@ extern "C" fn channel_callback(user_data: *mut c_void, code: i32) { let _ = tx.send(code); } +/// What [follow_channel_callback] forwards for `MOQ_FOLLOW_EVENT_OFFLINE`, so follow tests read +/// one channel of codes: a handle, this, or a terminal code. Test-only: the ABI reports +/// offline as its own event. +const FOLLOW_OFFLINE: i32 = i32::MAX; + +/// The code a follow event forwards as, checking that each event carries what it promises. +fn follow_code(event: moq_follow_event, code: i32) -> i32 { + match event { + moq_follow_event::MOQ_FOLLOW_EVENT_ONLINE => { + assert!(code > 0, "online carries a handle, got {code}"); + code + } + moq_follow_event::MOQ_FOLLOW_EVENT_OFFLINE => { + assert_eq!(code, 0, "offline carries no code"); + FOLLOW_OFFLINE + } + moq_follow_event::MOQ_FOLLOW_EVENT_END => { + assert!(code <= 0, "end carries a terminal code, got {code}"); + code + } + } +} + +/// [channel_callback] for [moq_origin_follow], forwarding through [follow_code]. +extern "C" fn follow_channel_callback(user_data: *mut c_void, event: moq_follow_event, code: i32) { + channel_callback(user_data, follow_code(event, code)); +} + /// FFI callback for tests that only inspect the registrar's immediate return code. extern "C" fn ignore_callback(_user_data: *mut c_void, _code: i32) {} @@ -3563,7 +3589,7 @@ fn announced_free_lifecycle() { // The first callback is the announcement for our broadcast. let (announced, mut info) = ann_cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); let got = unsafe { std::slice::from_raw_parts(info.prefix.cast::(), info.prefix_len) }; assert_eq!(got, path, "announced prefix should match"); @@ -3713,7 +3739,7 @@ fn local_announce() { let broadcast = publish_broadcast(origin, path); let (announced_id, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); let announced_prefix = unsafe { std::str::from_utf8(std::slice::from_raw_parts(info.prefix.cast::(), info.prefix_len)).unwrap() }; @@ -3815,7 +3841,7 @@ fn announced_deactivation() { assert_eq!(unsafe { moq_publication_announce(publication, std::ptr::null()) }, 0); let (announced_id, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); assert_eq!(moq_origin_announced_free(announced_id), 0); // Going non-live unannounces the broadcast without tearing it down: local @@ -3823,13 +3849,13 @@ fn announced_deactivation() { assert_eq!(moq_publication_unannounce(publication), 0); let (deactivated_id, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_END); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_ABSENT); assert_eq!(moq_origin_announced_free(deactivated_id), 0); assert!(request_once(origin, path) < 0, "an unannounced broadcast is unroutable"); assert_eq!(unsafe { moq_publication_announce(publication, std::ptr::null()) }, 0); let (reannounced_id, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); assert_eq!(moq_origin_announced_free(reannounced_id), 0); let _ = request_broadcast(origin, path); @@ -3882,7 +3908,7 @@ fn create_broadcast_is_unroutable_until_announced() { assert_eq!(moq_publication_free(publication), 0); let _ = request_broadcast(origin, path); let (announced_id, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); assert_eq!(moq_origin_announced_free(announced_id), 0); assert_eq!(moq_origin_announced_cancel(announced_task), 0); @@ -3905,7 +3931,7 @@ fn announce_route_carries_cost_and_instance_without_hops() { assert_eq!(unsafe { moq_publication_announce(publication, &route) }, 0); let (announced, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); assert_eq!(info.route.hops_len, 0, "a local route claims no hops"); assert_eq!(info.route.cost, 7); assert_eq!(route_instance(&info.route).as_ref(), Some(&instance)); @@ -3934,14 +3960,27 @@ fn announce_route_carries_cost_and_instance_without_hops() { assert_eq!(moq_origin_close(origin), 0); } -/// Announcing under another instance is another stream: subscribers see a restart rather -/// than a stalled broadcast. +/// Announcing under another instance is another stream: the path stays listed, and a follower +/// is handed the new instance's broadcast. #[test] fn announce_under_a_new_instance_restarts() { let origin = id(moq_origin_create()); let cb = Callback::new(); let announced_task = id(unsafe { moq_origin_announced(origin, std::ptr::null(), Some(channel_callback), cb.ptr) }); let (publication, broadcast) = create_broadcast(origin, b"restarted"); + let follow_cb = Callback::new(); + let path = b"restarted"; + let follow = id(unsafe { + moq_origin_follow( + origin, + path.as_ptr() as *const c_char, + path.len(), + std::ptr::null(), + 0, + Some(follow_channel_callback), + follow_cb.ptr, + ) + }); let first = fresh_instance(); assert_eq!( @@ -3949,22 +3988,26 @@ fn announce_under_a_new_instance_restarts() { 0 ); let (announced, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); assert_eq!(route_instance(&info.route), Some(first)); assert_eq!(moq_origin_announced_free(announced), 0); + let old = id(follow_cb.recv()); let second = fresh_instance(); assert_eq!( unsafe { moq_publication_announce(publication, &announce_route(0, &second)) }, 0 ); - let (announced, info) = cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_RESTART); - assert_eq!(route_instance(&info.route), Some(second)); - assert_eq!(moq_origin_announced_free(announced), 0); + let new = id(follow_cb.recv()); + assert_ne!(old, new); + assert_eq!(moq_origin_follow_cancel(follow), 0); + assert_eq!(follow_cb.recv_terminal(), 0); + assert_eq!(moq_consume_close(old), 0); + assert_eq!(moq_consume_close(new), 0); + // The path stayed present throughout: the cancel's terminal is the next callback. assert_eq!(moq_origin_announced_cancel(announced_task), 0); - assert_eq!(cb.recv_announce_terminal(), 0); + assert_eq!(cb.recv(), 0, "a restart is not listed"); assert_eq!(moq_publication_free(publication), 0); assert_eq!(moq_publish_close(broadcast), 0); assert_eq!(moq_origin_close(origin), 0); @@ -4900,7 +4943,7 @@ fn follow_local() { path.len(), std::ptr::null(), 0, - Some(channel_callback), + Some(follow_channel_callback), cb.ptr, ) }); @@ -5063,7 +5106,7 @@ fn follow_close_cancels() { path.len(), std::ptr::null(), 0, - Some(channel_callback), + Some(follow_channel_callback), cb.ptr, ) }); @@ -5100,7 +5143,7 @@ fn follow_delivers_a_restart() { path.len(), std::ptr::null(), 0, - Some(channel_callback), + Some(follow_channel_callback), cb.ptr, ) }); @@ -5139,7 +5182,7 @@ fn follow_delivers_a_takeover_once(dynamic_at: &str) { path.len(), std::ptr::null(), 0, - Some(channel_callback), + Some(follow_channel_callback), cb.ptr, ) }); @@ -5172,7 +5215,7 @@ fn follow_delivers_a_held_takeover_once() { follow_delivers_a_takeover_once("live"); } -/// `moq_origin_request` and `moq_origin_follow` share one signature. +/// The signature of `moq_origin_request`. type Resolve = unsafe extern "C" fn( u32, *const c_char, @@ -5229,11 +5272,25 @@ fn an_instance_pins_request_and_follow() { // follower delivers it while it still serves, and nothing for the winner. let second = publish_run(origin, "live"); let cb = Callback::new(); - let task = id(resolve_pinned(moq_origin_follow, origin, &instance, &cb)); + let path = b"live"; + let task = id(unsafe { + moq_origin_follow( + origin, + path.as_ptr() as *const c_char, + path.len(), + instance.as_ptr() as *const c_char, + instance.len(), + Some(follow_channel_callback), + cb.ptr, + ) + }); let delivered = id(cb.recv()); - let tx = unsafe { &*(cb.ptr as *const mpsc::Sender) }.clone(); - crate::ffi::RUNTIME.spawn(async move { tx.send(i32::MAX).unwrap() }); - assert_eq!(cb.recv(), i32::MAX, "the follower delivered another instance"); + assert!( + cb.rx + .recv_timeout(moq_net::origin::Config::default().update_hold * 2) + .is_err(), + "the follower delivered another instance" + ); assert_eq!(moq_consume_close(delivered), 0); assert_eq!(moq_origin_follow_cancel(task), 0); assert_eq!(cb.recv_terminal(), 0, "cancel delivers terminal 0"); @@ -5257,7 +5314,7 @@ fn follow(origin: u32, path: &[u8], cb: &Callback) -> u32 { path.len(), std::ptr::null(), 0, - Some(channel_callback), + Some(follow_channel_callback), cb.ptr, ) }) @@ -5270,6 +5327,11 @@ struct Gate { hold: std::sync::Mutex>>, } +/// [gated_callback] for [moq_origin_follow], forwarding through [follow_code]. +extern "C" fn gated_follow_callback(user_data: *mut c_void, event: moq_follow_event, code: i32) { + gated_callback(user_data, follow_code(event, code)); +} + extern "C" fn gated_callback(user_data: *mut c_void, code: i32) { let gate = unsafe { &*(user_data as *const Gate) }; // The receiver may free gate as soon as it receives the terminal code. @@ -5301,7 +5363,7 @@ fn follow_delivers_the_instance_again_after_an_end() { path.len(), std::ptr::null(), 0, - Some(gated_callback), + Some(gated_follow_callback), &*gate as *const Gate as *mut c_void, ) }); @@ -5319,6 +5381,7 @@ fn follow_delivers_the_instance_again_after_an_end() { run.announce(route.clone()).unwrap(); } release.send(()).unwrap(); + assert_eq!(recv(), FOLLOW_OFFLINE, "the gap is reported before the return"); let again = id(recv()); assert_eq!(moq_consume_close(first), 0); @@ -7606,7 +7669,7 @@ fn announce_route_hops_cross_a_relay_session() { }; assert_eq!(unsafe { moq_publication_announce(publication, &route) }, 0); let (announced, info) = announced_cb.recv_route(); - assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_START); + assert_eq!(info.kind, moq_announce_kind::MOQ_ANNOUNCE_KIND_PRESENT); assert_eq!( borrowed_string(info.prefix, info.prefix_len).as_deref(), Some("relayed") @@ -7706,7 +7769,7 @@ fn server_accepts_a_session() { path.len(), std::ptr::null(), 0, - Some(channel_callback), + Some(follow_channel_callback), announced_cb.ptr, ) }); diff --git a/rs/moq-cli/src/announced.rs b/rs/moq-cli/src/announced.rs index f695d386e5a..c46d412f25f 100644 --- a/rs/moq-cli/src/announced.rs +++ b/rs/moq-cli/src/announced.rs @@ -91,14 +91,8 @@ async fn follow( }; let (announce, active) = match event { - Event::Start(announce) => (announce, true), - Event::End(announce) => (announce, false), - // A new route or a new instance for a path already announced, which changes - // nothing shown. - Event::Update(_) | Event::Restart(_) => continue, - // A route continuing another: new to the list unless it replaced that one in place. - Event::Takeover { announce, predecessor } if predecessor != announce.prefix => (announce, true), - Event::Takeover { .. } => continue, + Event::Present(announce) => (announce, true), + Event::Absent(announce) => (announce, false), }; let path = announce.prefix.as_str(); @@ -234,7 +228,8 @@ mod tests { kio::clock::timeout(TIMEOUT, relay.follow(*path, None).unwrap().available()) .await .expect("relay routes the broadcast") - .expect("broadcast"); + .expect("broadcast") + .unwrap(); } Self { diff --git a/rs/moq-cli/src/archive.rs b/rs/moq-cli/src/archive.rs index 4012c556d4e..a0b71ccfa03 100644 --- a/rs/moq-cli/src/archive.rs +++ b/rs/moq-cli/src/archive.rs @@ -70,6 +70,10 @@ pub async fn export(origin: moq_net::origin::Consumer, prefix: String, args: Exp let hidden = prefix.split('/').any(|segment| segment.starts_with('.')); let prefix = moq_net::Path::new(&prefix)?; let mut announced = origin.clone().with_hidden(hidden).announced(); + // A follower per listed path, reporting each instance that serves it, stopped with the export. + let mut following = tokio::task::JoinSet::new(); + let mut followers: HashMap = HashMap::new(); + let (served, mut instances) = tokio::sync::mpsc::unbounded_channel::(); let mut recordings = tokio::task::JoinSet::new(); let mut running = Running::default(); let claims = Claims::default(); @@ -80,18 +84,33 @@ pub async fn export(origin: moq_net::origin::Consumer, prefix: String, args: Exp loop { let instance = tokio::select! { event = announced.next() => { - let announce = match event.context("announcements ended")? { - // Another instance, or the same one announced again. - moq_net::announce::Event::Start(announce) | moq_net::announce::Event::Restart(announce) | moq_net::announce::Event::Takeover { announce, .. } => announce, - _ => continue, - }; - if !announce.prefix.has_prefix(&prefix) { - continue; + match event.context("announcements ended")? { + moq_net::announce::Event::Present(announce) if announce.prefix.has_prefix(&prefix) => { + let path = announce.prefix; + let follower = following.spawn(follow(origin.clone(), path.clone(), served.clone())); + if let Some(old) = followers.insert(path, follower) { + old.abort(); + } + } + moq_net::announce::Event::Absent(announce) => { + if let Some(follower) = followers.remove(&announce.prefix) { + follower.abort(); + } + } + moq_net::announce::Event::Present(_) => {} } - let Some(instance) = announce.route.instance else { - continue; - }; - let instance: Instance = (announce.prefix, instance); + continue; + } + Some(followed) = following.join_next() => { + match followed { + Ok(result) => result?, + // Aborted once its path went absent. + Err(err) if err.is_cancelled() => {} + Err(err) => return Err(err).context("follower panicked"), + } + continue; + } + Some(instance) = instances.recv() => { if !running.start(&instance) { continue; } @@ -115,6 +134,33 @@ pub async fn export(origin: moq_net::origin::Consumer, prefix: String, args: Exp } } +/// Report each instance serving `path` as it comes online or takes over. An instance comes +/// again after the path went offline, since a return is announced anew. +/// +/// Fails when the path cannot be followed or a handler refuses it, as a recording would. +async fn follow( + origin: moq_net::origin::Consumer, + path: moq_net::Path, + served: tokio::sync::mpsc::UnboundedSender, +) -> anyhow::Result<()> { + let unavailable = || format!("broadcast `{path}` is unavailable"); + let mut following = origin.follow(&path, None).with_context(unavailable)?; + loop { + let change = match following.available().await { + Ok(change) => change, + // The origin closed, which ends the export through its announcements. + Err(moq_net::Error::Closed) => return Ok(()), + Err(err) => return Err(err).with_context(unavailable), + }; + let Some(instance) = change.and_then(|broadcast| broadcast.info().instance.clone()) else { + continue; + }; + if served.send((path.clone(), instance)).is_err() { + return Ok(()); + } + } +} + /// The broadcasts being recorded. Announcements read after another instance took over all /// resolve that one, so it is recorded once. #[derive(Clone, Default)] @@ -404,6 +450,32 @@ mod tests { assert!(!files.iter().any(|file| file.starts_with("other")), "{files:?}"); } + /// A handler refusing a listed path fails the export, as a refused recording did. + #[tokio::test] + async fn a_refused_path_fails_the_export() { + let dir = tempfile::tempdir().unwrap(); + let args = ExportArgs { + store: Url::from_directory_path(dir.path()).unwrap(), + retention: None, + retention_grace: None, + }; + let origin = moq_tokio::origin::spawn(); + let claim = origin.requests("live/a").unwrap(); + let recording = tokio::spawn(export(origin.consume(), "live".into(), args)); + + let request = kio::clock::timeout(Duration::from_secs(10), claim.requested_broadcast()) + .await + .expect("the export follows the listed path") + .unwrap(); + request.reject(moq_net::Error::NotFound); + let result = kio::clock::timeout(Duration::from_secs(10), recording) + .await + .expect("the export ends") + .unwrap(); + let err = result.expect_err("a refused path fails the export"); + assert!(err.to_string().contains("live/a"), "{err:#}"); + } + /// `import archive` republishes a recording `export archive` wrote under the given name and /// instance, serving its stored groups. #[tokio::test] @@ -448,7 +520,7 @@ mod tests { let mut announced = origin.consume().announced(); kio::clock::timeout(Duration::from_secs(10), async { loop { - if let Some(moq_net::announce::Event::Start(announce)) = announced.next().await { + if let Some(moq_net::announce::Event::Present(announce)) = announced.next().await { assert_eq!(announce.prefix.as_str(), "replay"); assert_eq!(announce.route.instance, Some(instance.clone())); break; diff --git a/rs/moq-cli/src/fetch.rs b/rs/moq-cli/src/fetch.rs index 08165b67250..9f9c16c2412 100644 --- a/rs/moq-cli/src/fetch.rs +++ b/rs/moq-cli/src/fetch.rs @@ -122,7 +122,8 @@ async fn fetch_from( .follow(path, None)? .available() .await - .with_context(|| format!("broadcast `{}` not found", crate::display_name(path)))?; + .with_context(|| format!("broadcast `{}` not found", crate::display_name(path)))? + .context("went offline before serving")?; let track = broadcast.track(&args.track)?; let mut group = moq_relay::fetch_group(&track, args.group) .await diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index d748f1566eb..ab2aefdd4b4 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -965,7 +965,9 @@ async fn run_container( // rendition's `broadcast` field references (e.g. "./source") through the same origin. let mut follow = consumer.follow(name.as_str(), None)?; let broadcast = tokio::select! { - broadcast = follow.available() => broadcast.with_context(|| format!("failed to resolve broadcast `{name}`"))?, + broadcast = follow.available() => broadcast + .with_context(|| format!("failed to resolve broadcast `{name}`"))? + .context("went offline before serving")?, () = finish.cancelled() => return Ok(()), }; @@ -1388,6 +1390,7 @@ mod tests { .unwrap() .available() .await + .unwrap() .unwrap(); let mut track = broadcast .track("status") @@ -1499,6 +1502,7 @@ mod tests { .available() .await .unwrap() + .unwrap() .track("live") .unwrap() .subscribe(None) diff --git a/rs/moq-cli/src/play/media.rs b/rs/moq-cli/src/play/media.rs index c7a00b815aa..dfe7b92491f 100644 --- a/rs/moq-cli/src/play/media.rs +++ b/rs/moq-cli/src/play/media.rs @@ -8,7 +8,6 @@ use std::time::Duration; use anyhow::Context; use hang::moq_net; use moq_mux::catalog::{self, Stream}; -use moq_mux::follow::{Event as FollowEvent, Instances}; // The ambient clock, which follows tokio's paused clock when a test drives the // playout clock itself. use kio::clock::Instant; @@ -69,12 +68,11 @@ impl Media { output.send(event); } - /// Follow the path's announcements for as long as the origin lasts: a start - /// plays the broadcast, a restart drops it for the instance that replaced it, - /// and an end lets what is playing finish while waiting for the next start. An - /// update is the same instance, which its subscriptions already ride out. + /// Follow the path for as long as the origin lasts: each instance that comes + /// online replaces what is playing, and going offline lets what is playing + /// finish while waiting for the next one. async fn play(mut self) -> anyhow::Result<()> { - let mut follow = Instances::new(&self.origin, &self.broadcast, None)?; + let mut follow = self.origin.follow(&self.broadcast, None)?; // The device belongs to the player, so replacing a broadcast never waits // for it to close and reopen. Each sink adapts its own rate and layout. let device = tokio::sync::OnceCell::new(); @@ -82,14 +80,14 @@ impl Media { loop { tokio::select! { - event = follow.next() => { + event = follow.available() => { let broadcast = match event { - Ok(FollowEvent::Start(broadcast)) => broadcast, - Ok(FollowEvent::End) => { + Ok(Some(broadcast)) => broadcast, + Ok(None) => { tracing::info!(broadcast = %self.broadcast, "offline, waiting for it to return"); continue; } - Err(moq_mux::Error::Moq(moq_net::Error::Closed)) => return Ok(()), + Err(moq_net::Error::Closed) => return Ok(()), Err(err) => return Err(err).context("failed to follow the broadcast"), }; tracing::info!( diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index 39393d5621e..5bf7fc1400d 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -1726,6 +1726,7 @@ pub(crate) mod tests { let consumer = kio::clock::timeout(WAIT, remote.consume().follow("pt.hang", None).unwrap().available()) .await .unwrap() + .unwrap() .unwrap(); let mut catalogs = hang::catalog::Catalog::<()>::subscribe(&consumer).await.unwrap(); let catalog = kio::clock::timeout(WAIT, catalogs.next()) @@ -2175,7 +2176,7 @@ pub(crate) mod tests { _ = kio::clock::sleep(Duration::from_secs(30)) => panic!("never announced"), resolved = async { let event = announced.next().await.expect("origin closed"); - assert!(matches!(event, moq_net::announce::Event::Start(_)), "{event:?}"); + assert!(matches!(event, moq_net::announce::Event::Present(_)), "{event:?}"); let broadcast = consumer.request("demo", None).await.unwrap(); let mut catalogs = hang::catalog::Catalog::<()>::subscribe(&broadcast).await.unwrap(); let catalog = catalogs.next().await.unwrap().expect("a catalog"); @@ -2296,7 +2297,7 @@ pub(crate) mod tests { instance = async { loop { use moq_net::announce::Event; - if let Event::Start(announce) | Event::Restart(announce) = announced.next().await.unwrap() { + if let Event::Present(announce) = announced.next().await.unwrap() { break announce.route.instance.unwrap(); } } diff --git a/rs/moq-cli/src/report.rs b/rs/moq-cli/src/report.rs index 310d3ee519c..8dab104c54d 100644 --- a/rs/moq-cli/src/report.rs +++ b/rs/moq-cli/src/report.rs @@ -417,7 +417,8 @@ pub async fn print_report(origin: moq_net::origin::Consumer, path: String) -> an .follow(path.as_str(), None)? .available() .await - .with_context(|| format!("failed to resolve broadcast `{path}`"))?; + .with_context(|| format!("failed to resolve broadcast `{path}`"))? + .context("went offline before serving")?; let source = moq_mux::Source::new(origin, broadcast.clone()); let mut catalog = Some(source.catalog::<()>(CatalogFormat::Hang).await?); @@ -485,12 +486,8 @@ async fn write_echo( event = announced.next() => { let event = event.context("announcements ended")?; let (announce, active) = match event { - // A restart is another publisher instance, which a reader still on the old one never sees. - moq_net::announce::Event::Start(announce) | moq_net::announce::Event::Restart(announce) | moq_net::announce::Event::Takeover { announce, .. } => { - (announce, true) - } - moq_net::announce::Event::End(announce) => (announce, false), - _ => continue, + moq_net::announce::Event::Present(announce) => (announce, true), + moq_net::announce::Event::Absent(announce) => (announce, false), }; let path = announce.prefix; if !path.has_prefix(&prefix) || !hang::echo::is_echo(&path) { @@ -562,29 +559,59 @@ impl Readers { /// Send a line for each feedback snapshot the broadcast at `path` publishes, until it ends. async fn read_echo(origin: moq_net::origin::Consumer, path: moq_net::Path, lines: tokio::sync::mpsc::Sender) { - let read = async { - let broadcast = origin.request(&path, None).await?; - let mut track = reader(broadcast.track(hang::echo::TRACK)?); - while let Some(moq_net::Timed { value: feedback, .. }) = track.latest().await? { - let line = EchoLine { - broadcast: path.as_str(), - feedback, - }; - if lines - .send(format!("{}\n", serde_json::to_string(&line)?)) - .await - .is_err() - { - break; + let mut following = match origin.follow(&path, None) { + Ok(following) => following, + Err(err) => return tracing::debug!(%err, path = %path.as_str(), "stopped reading feedback"), + }; + // The instance being read; a restarted publisher replaces it, and going offline keeps it + // draining its final snapshot. + let mut reading = None; + loop { + tokio::select! { + change = following.available() => match change { + Ok(Some(broadcast)) => reading = Some(Box::pin(read_feedback(broadcast, path.clone(), lines.clone()))), + Ok(None) => {} + Err(moq_net::Error::Closed) => return, + // The listing reports nothing new while the path stays present, so a refusal + // waits here for the serving route to change. + Err(err) => tracing::debug!(%err, path = %path.as_str(), "feedback not resolved; waiting for a route change"), + }, + read = async { reading.as_mut().expect("guarded").await }, if reading.is_some() => { + reading = None; + if let Err(err) = read { + tracing::debug!(%err, path = %path.as_str(), "stopped reading feedback"); + } + if lines.is_closed() { + return; + } } } - anyhow::Ok(()) - }; - if let Err(err) = read.await { - tracing::debug!(%err, path = %path.as_str(), "stopped reading feedback"); } } +/// Write one publisher instance's feedback to `lines` until its track ends or `lines` closes. +async fn read_feedback( + broadcast: moq_net::broadcast::Consumer, + path: moq_net::Path, + lines: tokio::sync::mpsc::Sender, +) -> anyhow::Result<()> { + let mut track = reader(broadcast.track(hang::echo::TRACK)?); + while let Some(moq_net::Timed { value: feedback, .. }) = track.latest().await? { + let line = EchoLine { + broadcast: path.as_str(), + feedback, + }; + if lines + .send(format!("{}\n", serde_json::to_string(&line)?)) + .await + .is_err() + { + break; + } + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -647,6 +674,33 @@ mod tests { assert!(late.is_err(), "a drained reader wrote {late:?}"); } + /// A refused path stays followed: the listing reports nothing new when a publisher takes it + /// over, so only the follower can pick it up. + #[tokio::test(start_paused = true)] + async fn a_refused_feedback_path_is_read_once_a_publisher_takes_over() { + let origin = moq_tokio::origin::spawn(); + let (out, read) = tokio::io::duplex(4096); + let mut lines = BufReader::new(read).lines(); + tokio::spawn(write_echo(origin.consume(), String::new(), out)); + + let claim = origin.requests("viewer.echo").unwrap(); + kio::clock::timeout(Duration::from_secs(1), claim.requested_broadcast()) + .await + .expect("never asked") + .expect("the claim closed") + .reject(moq_net::Error::NotFound); + kio::clock::sleep(Duration::from_secs(1)).await; + + let broadcast = origin.create_broadcast("viewer.echo").unwrap(); + let track = broadcast.create_track(hang::echo::TRACK, None).unwrap(); + let mut feedback = moq_json::snapshot::Producer::::new(track, Default::default()); + broadcast.announce(Default::default()).unwrap(); + feedback.update(&serde_json::json!({ "n": 1 })).unwrap(); + let line = kio::clock::timeout(Duration::from_secs(5), lines.next_line()).await; + let line = line.expect("the takeover was never read").unwrap().unwrap(); + assert!(line.contains(r#""n":1"#), "{line}"); + } + /// A reader is forgotten once its drain passes, unless a start at its path replaced it. #[tokio::test(start_paused = true)] async fn a_drained_reader_is_forgotten() { diff --git a/rs/moq-cli/src/report/echo_test.rs b/rs/moq-cli/src/report/echo_test.rs index 7647229c890..5d803d26135 100644 --- a/rs/moq-cli/src/report/echo_test.rs +++ b/rs/moq-cli/src/report/echo_test.rs @@ -30,7 +30,13 @@ async fn export_echo_reports_where_the_import_solicits() { import.decode(&CLIP[..HALF]).unwrap(); publication.announce(Default::default()).unwrap(); let consumer = origin.consume(); - let resolved = consumer.follow("demo", None).unwrap().available().await.unwrap(); + let resolved = consumer + .follow("demo", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let args = SubscribeArgs { format: SubscribeFormat::Ts, diff --git a/rs/moq-cli/src/rtc.rs b/rs/moq-cli/src/rtc.rs index a91fccd9073..27e7055e016 100644 --- a/rs/moq-cli/src/rtc.rs +++ b/rs/moq-cli/src/rtc.rs @@ -121,7 +121,8 @@ pub async fn connect_export(origin: moq_net::origin::Consumer, url: Url, name: S .follow(name.as_str(), None)? .available() .await - .with_context(|| format!("failed to resolve broadcast `{name}`"))?; + .with_context(|| format!("failed to resolve broadcast `{name}`"))? + .context("went offline before serving")?; let source = moq_mux::Source::new(origin, broadcast); tracing::info!(url = %RedactedUrl::new(&url), %name, "WHIP client pushing"); diff --git a/rs/moq-cli/src/rtmp.rs b/rs/moq-cli/src/rtmp.rs index 180d190f7b4..f7975d7c0d7 100644 --- a/rs/moq-cli/src/rtmp.rs +++ b/rs/moq-cli/src/rtmp.rs @@ -176,7 +176,8 @@ pub async fn connect_export( .follow(&name, None)? .available() .await - .with_context(|| format!("failed to resolve broadcast `{name}`"))?; + .with_context(|| format!("failed to resolve broadcast `{name}`"))? + .context("went offline before serving")?; let source = moq_mux::Source::new(origin, broadcast); // The stream key is the ingest credential, so log the dial target and app instead. diff --git a/rs/moq-cli/src/transcode.rs b/rs/moq-cli/src/transcode.rs index 52ae7cdab40..fe499fafb87 100644 --- a/rs/moq-cli/src/transcode.rs +++ b/rs/moq-cli/src/transcode.rs @@ -168,14 +168,12 @@ struct Following { impl Following { async fn run(self) -> anyhow::Result<()> { - use moq_mux::follow::{Event, Instances}; - - let mut instances = Instances::new(&self.origin, &self.source_path, None)?; + let mut instances = self.origin.follow(&self.source_path, None)?; let mut running = None; loop { tokio::select! { - event = instances.next() => match event? { - Event::Start(source) => { + event = instances.available() => match event? { + Some(source) => { // Dropping the old ladder cancels its pending requests and rung tasks. drop(running.take()); let output = self.publish.create_broadcast(&self.output_path)?; @@ -184,7 +182,7 @@ impl Following { tracing::info!(source = %self.source_path, output = %self.output_path, "transcoding"); running = Some(Box::pin(transcoder.run())); } - Event::End => tracing::info!(source = %self.source_path, "offline, waiting for it to return"), + None => tracing::info!(source = %self.source_path, "offline, waiting for it to return"), }, result = async { running.as_mut().expect("guarded").await }, if running.is_some() => { running = None; @@ -233,7 +231,7 @@ mod tests { }; let old = origin.publish("source", route()).unwrap(); let _old_dynamic = old.requests(); - let mut derivatives = moq_mux::follow::Instances::new(&publish.consume(), "derived", None).unwrap(); + let mut derivatives = publish.consume().follow("derived", None).unwrap(); let following = Following { origin: origin.consume(), publish: publish.clone(), @@ -280,7 +278,7 @@ mod tests { let route = || moq_net::origin::Route::default(); let old = origin.publish("source", route()).unwrap(); let mut catalog = moq_mux::catalog::Producer::new(&mut old.broadcast().clone(), Default::default()).unwrap(); - let mut derivatives = moq_mux::follow::Instances::new(&publish.consume(), "derived", None).unwrap(); + let mut derivatives = publish.consume().follow("derived", None).unwrap(); let following = Following { origin: origin.consume(), publish: publish.clone(), @@ -306,12 +304,11 @@ mod tests { } /// The next derivative instance announced at the output path. - async fn next(instances: &mut moq_mux::follow::Instances) -> moq_net::broadcast::Consumer { + async fn next(instances: &mut moq_net::origin::Following) -> moq_net::broadcast::Consumer { let start = async { loop { - match instances.next().await.unwrap() { - moq_mux::follow::Event::Start(broadcast) => return broadcast, - moq_mux::follow::Event::End => continue, + if let Some(broadcast) = instances.available().await.unwrap() { + return broadcast; } } }; diff --git a/rs/moq-cli/tests/integration/export.rs b/rs/moq-cli/tests/integration/export.rs index 5d3efab29af..11c68fd2e5e 100644 --- a/rs/moq-cli/tests/integration/export.rs +++ b/rs/moq-cli/tests/integration/export.rs @@ -577,6 +577,7 @@ async fn the_same_instance_within_the_follow_continues() { let back = kio::clock::timeout(TIMEOUT, relay.origin.follow("demo", None).unwrap().available()) .await .expect("the relay observes the returning publication") + .unwrap() .unwrap(); assert_eq!(back.info().instance.as_ref(), Some(&instance)); assert!(!back.is_clone(&old), "the old relay copy was replaced"); @@ -1061,13 +1062,13 @@ async fn redundant_failover(bufferbloat: bool) { } /// The instance of the next route announced for the path. -async fn next_instance(announced: &mut moq_net::announce::Consumer) -> moq_net::Instance { +async fn next_instance(announced: &mut moq_net::origin::Routes) -> moq_net::Instance { kio::clock::timeout(TIMEOUT, async { loop { match announced.next().await.expect("the relay stays up") { - moq_net::announce::Event::Start(announce) - | moq_net::announce::Event::Restart(announce) - | moq_net::announce::Event::Takeover { announce, .. } => { + moq_net::origin::RouteEvent::Start(announce) + | moq_net::origin::RouteEvent::Restart(announce) + | moq_net::origin::RouteEvent::Takeover { announce, .. } => { return announce .route .instance @@ -1088,7 +1089,7 @@ async fn next_instance(announced: &mut moq_net::announce::Consumer) -> moq_net:: async fn a_passthrough_export_ends_with_its_redundant_importer() { let relay = relay().await; let demo = moq_net::Patterns::from(moq_net::Pattern::literal("demo").unwrap()); - let mut announced = relay.origin.scope("", &demo).unwrap().announced(); + let mut announced = relay.origin.scope("", &demo).unwrap().routes(); let (feed, feeding) = redundant_feed(); let mut first = redundant_import(&relay, &feed, &["--passthrough"]); let incumbent = next_instance(&mut announced).await; @@ -1200,7 +1201,7 @@ fn delivered(relay: &Relay) -> Arc { let counting = count.clone(); let origin = relay.origin.clone(); tokio::spawn(async move { - let broadcast = origin.follow("demo", None).unwrap().available().await.unwrap(); + let broadcast = origin.follow("demo", None).unwrap().available().await.unwrap().unwrap(); let mut catalogs = hang::catalog::Catalog::<()>::subscribe(&broadcast).await.unwrap(); let name = loop { let catalog = catalogs.next().await.unwrap().expect("the catalog ended"); diff --git a/rs/moq-cli/tests/integration/import.rs b/rs/moq-cli/tests/integration/import.rs index 6a00c31c3db..a6de2f29657 100644 --- a/rs/moq-cli/tests/integration/import.rs +++ b/rs/moq-cli/tests/integration/import.rs @@ -88,7 +88,7 @@ async fn import_delivers_the_catalog_finish_at_eof() { let event = running(&mut child, "the announce", announced.next()) .await .expect("origin closed"); - let moq_tokio::moq_net::announce::Event::Start(announce) = event else { + let moq_tokio::moq_net::announce::Event::Present(announce) = event else { panic!("the first announce event is {event:?}, not a start"); }; assert_eq!(announce.prefix.as_str(), "demo"); diff --git a/rs/moq-cli/tests/integration/instances.rs b/rs/moq-cli/tests/integration/instances.rs index d1156764d71..2e38197c738 100644 --- a/rs/moq-cli/tests/integration/instances.rs +++ b/rs/moq-cli/tests/integration/instances.rs @@ -5,7 +5,7 @@ use std::time::Duration; -use hang::moq_net::{self, Instance, Timestamp, announce, origin, track}; +use hang::moq_net::{self, Instance, Timestamp, origin, track}; use moq_mux::catalog::Stream; const LITE_07: &str = "moq-lite-07-wip"; @@ -102,15 +102,15 @@ fn publish(origin: &origin::Producer, instance: &Instance, tag: &str) -> Publish } /// Ignore same-instance metadata updates, but keep the actual replacement representation. -async fn changed(follow: &mut announce::Consumer) -> bool { +async fn changed(follow: &mut origin::Routes) -> bool { loop { match follow.next().await.unwrap() { - announce::Event::Restart(_) => return true, - announce::Event::End(_) => { - assert!(matches!(follow.next().await, Some(announce::Event::Start(_)))); + origin::RouteEvent::Restart(_) => return true, + origin::RouteEvent::End(_) => { + assert!(matches!(follow.next().await, Some(origin::RouteEvent::Start(_)))); return false; } - announce::Event::Update(_) => {} + origin::RouteEvent::Update(_) => {} other => panic!("expected replacement, got {other:?}"), } } @@ -132,7 +132,7 @@ async fn replacement(version: &str) { let viewer = viewer_origin.consume(); let watch = |path| { let path = moq_net::Patterns::from(moq_net::Pattern::literal(path).unwrap()); - viewer.scope("", &path).unwrap().announced() + viewer.scope("", &path).unwrap().routes() }; let mut catalog_follow = watch("live/catalog.hang"); let mut media_follow = watch("live/media"); @@ -143,8 +143,11 @@ async fn replacement(version: &str) { .established() .await .unwrap(); - assert!(matches!(catalog_follow.next().await, Some(announce::Event::Start(_)))); - assert!(matches!(media_follow.next().await, Some(announce::Event::Start(_)))); + assert!(matches!( + catalog_follow.next().await, + Some(origin::RouteEvent::Start(_)) + )); + assert!(matches!(media_follow.next().await, Some(origin::RouteEvent::Start(_)))); let held = viewer.request("live/catalog.hang", None).await.unwrap(); let old = moq_mux::Source::new(viewer.clone(), held); diff --git a/rs/moq-cli/tests/integration/rtsp.rs b/rs/moq-cli/tests/integration/rtsp.rs index b1a048e3584..aa31f27363b 100644 --- a/rs/moq-cli/tests/integration/rtsp.rs +++ b/rs/moq-cli/tests/integration/rtsp.rs @@ -56,17 +56,17 @@ fn import(url: &str, fingerprint: &str, rtsp: &str) -> Child { async fn next_broadcast( child: &mut Child, consumer: &moq_net::origin::Consumer, - announced: &mut moq_net::announce::Consumer, + announced: &mut moq_net::origin::Routes, ) -> hang::catalog::Consumer { loop { match running(child, "the announce", announced.next()) .await .expect("origin closed") { - moq_net::announce::Event::Start(_) - | moq_net::announce::Event::Restart(_) - | moq_net::announce::Event::Takeover { .. } => break, - moq_net::announce::Event::End(_) | moq_net::announce::Event::Update(_) => {} + moq_net::origin::RouteEvent::Start(_) + | moq_net::origin::RouteEvent::Restart(_) + | moq_net::origin::RouteEvent::Takeover { .. } => break, + moq_net::origin::RouteEvent::End(_) | moq_net::origin::RouteEvent::Update(_) => {} } } let broadcast = running(child, "the broadcast", consumer.request("camera", None)) @@ -135,7 +135,7 @@ async fn every_rtsp_session_is_a_fresh_broadcast() { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let mut config = moq_tokio::connect::Config::default(); config.tls.insecure = Some(true); config.bind = Some("127.0.0.1:0".parse().unwrap()); diff --git a/rs/moq-crypto/tests/transport.rs b/rs/moq-crypto/tests/transport.rs index 4923266be46..1ac5db1ccc6 100644 --- a/rs/moq-crypto/tests/transport.rs +++ b/rs/moq-crypto/tests/transport.rs @@ -62,7 +62,13 @@ async fn connect_protected(version: Version, label: &str) -> Fixture { // The subscriber knows the opaque path and takes the epoch from the announced route's // instance. A wire without instances leaves it to the application's own channel. let consumer = consumer_origin.consume(); - let remote = consumer.follow(&path, None).unwrap().available().await.unwrap(); + let remote = consumer + .follow(&path, None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); if version.to_string().starts_with("moq-lite-07") { assert_eq!( moq_crypto::e2ee::Epoch::of(&remote).as_ref(), diff --git a/rs/moq-ffi/MIGRATE.md b/rs/moq-ffi/MIGRATE.md index d5927e2007d..ef5640f5164 100644 --- a/rs/moq-ffi/MIGRATE.md +++ b/rs/moq-ffi/MIGRATE.md @@ -5,6 +5,13 @@ These are the UniFFI names; each language wrapper has its own `MIGRATE.md` in it ## Unreleased +### Announcements are a listing ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`MoqAnnounceEvent` is `Present { announce }` or `Absent { announce }`. `Start`, `Update`, `End`, `Restart` and `Takeover` (with its `predecessor`) are gone: another instance taking a path over keeps it present, and a route's cost changing is not reported. +Map `Start` to `Present` and `End` to `Absent`, and drop `Update` handling. Where you requested the path again on `Restart`, hold `MoqOriginConsumer::follow(path)` and await `available()` for each new instance. + +`MoqFollowing::available()` returns `Option>`: `None` once the path goes offline (or, for a follow pinned to an `instance`, once another instance takes it over), then the next broadcast when something serves the path again. The first call never returns `None`. Handle `None` as offline: drop what you read from the last broadcast and call `available()` again. + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `MoqVideo` and `MoqAudio` gain `warmup`, the catalog's decoder warmup, null when absent. `MoqMediaTrackConsumer::subscribe` reads it from the rendition: it starts that much further back than the subscription's min delay and asks the publisher for its max delay plus the warmup, while it still skips at the max delay alone. `MoqAudioConsumer::subscribe` and `MoqVideoConsumer::subscribe` now honor it too. diff --git a/rs/moq-ffi/src/origin.rs b/rs/moq-ffi/src/origin.rs index 711bf329ad2..08779f590d4 100644 --- a/rs/moq-ffi/src/origin.rs +++ b/rs/moq-ffi/src/origin.rs @@ -47,8 +47,8 @@ pub struct MoqAnnounceRoute { /// publisher is a new stream viewers restart onto. Set one only for a publisher that /// continues its group sequences across restarts and raises `epoch` on each: readers /// stitch every route of one instance together, so restarting the sequences under an - /// old instance splices unrelated groups into their tracks. Changing it is a - /// `MoqAnnounceEvent::Restart`. + /// old instance splices unrelated groups into their tracks. Changing it hands + /// `MoqFollowing` a new broadcast. #[uniffi(default = None)] pub instance: Option, /// Which run of a stable `instance` this is: only its highest epoch is eligible, cheaper @@ -197,60 +197,34 @@ impl From for MoqAnnounce { } } -/// What a [`MoqAnnounceConsumer`] yields. +/// What a [`MoqAnnounceConsumer`] yields: a path becoming present or absent. +/// +/// Another publisher instance taking a path over keeps it present: follow it with +/// `MoqOriginConsumer::follow` to see the broadcast change. #[derive(Clone, Debug, PartialEq, Eq, uniffi::Enum)] pub enum MoqAnnounceEvent { - /// A route now covers the prefix; the stream had none there. - Start { - /// The broadcast announcement affected by this event. - announce: MoqAnnounce, - }, - /// The route covering the prefix changed hops or cost. - Update { - /// The broadcast announcement affected by this event. - announce: MoqAnnounce, - }, - /// No route covers the prefix any more. Carries its last advertised route. - End { - /// The broadcast announcement affected by this event. + /// A route now covers the path, which was absent. + Present { + /// The announcement that made the path present. announce: MoqAnnounce, }, - /// Another publisher instance now serves the prefix: drop what was resolved under - /// it and request afresh. - Restart { - /// The broadcast announcement affected by this event. + /// No route covers the path any more. Carries its last advertised route. + Absent { + /// The announcement that went. announce: MoqAnnounce, }, - /// A route now covers the prefix and continues what the route at `predecessor` - /// served beneath it: keep what was resolved through that one. - Takeover { - /// The broadcast announcement affected by this event. - announce: MoqAnnounce, - /// The prefix of the route it continues, at or above `announce`'s. - predecessor: String, - }, } impl From for MoqAnnounceEvent { fn from(event: moq_net::announce::Event) -> Self { use moq_net::announce::Event; match event { - Event::Start(announce) => Self::Start { - announce: announce.into(), - }, - Event::Update(announce) => Self::Update { + Event::Present(announce) => Self::Present { announce: announce.into(), }, - Event::End(announce) => Self::End { + Event::Absent(announce) => Self::Absent { announce: announce.into(), }, - Event::Restart(announce) => Self::Restart { - announce: announce.into(), - }, - Event::Takeover { announce, predecessor } => Self::Takeover { - announce: announce.into(), - predecessor: predecessor.to_string(), - }, } } } @@ -411,8 +385,8 @@ impl MoqOriginConsumer { /// /// Unlike `request`, this waits for future announcements and keeps following the /// instances serving the path. A local broadcast resolves once announced, like a remote - /// one. Pass an `instance` to follow only that stream: another one taking over is never - /// handed out, and the stream is handed out again if it returns after an end. + /// one. Pass an `instance` to follow only that stream: another one taking over ends it, + /// and the stream is handed out again if it returns. #[uniffi::method(default(instance = None))] pub fn follow(&self, path: String, instance: Option) -> Result, MoqError> { let _guard = crate::ffi::enter(); @@ -627,24 +601,27 @@ impl MoqAnnounceConsumer { #[uniffi::export] impl MoqFollowing { - /// Wait for the next publisher instance serving the broadcast. Returns `Closed` if cancelled - /// or the origin is closed. + /// Wait for the next change to the publisher instance serving the broadcast. Returns `Closed` + /// if cancelled or the origin is closed. /// /// The first call resolves the instance serving the path, once one is announced. Each later - /// call waits for another to take over, as a restarted publisher or a replacement session - /// does, or for the broadcast to return after it went offline. Drop everything read from the - /// previous instance then and read the new one: the old one's tracks end when its route - /// dies, with an error unless this side closed that session. - pub async fn available(&self) -> Result, MoqError> { + /// call returns another instance taking over, as a restarted publisher or a replacement + /// session does, or `None` once the path goes offline (or, with an `instance`, another + /// instance takes it over), then whatever serves it next. Drop everything read from the + /// previous instance then: its tracks end when its route dies, with an error unless this + /// side closed that session. + pub async fn available(&self) -> Result>, MoqError> { let broadcast = self .reads .wait(|following, waiter| Poll::Ready(Ok(ready!(following.poll_available(waiter))?))) .await?; - Ok(Arc::new(MoqBroadcastConsumer::routed( - broadcast, - self.origin.clone(), - self.driver.clone(), - ))) + Ok(broadcast.map(|broadcast| { + Arc::new(MoqBroadcastConsumer::routed( + broadcast, + self.origin.clone(), + self.driver.clone(), + )) + })) } /// Cancel all current and future `available()` calls. diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 86163084be1..29f84a9d701 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -253,6 +253,7 @@ async fn await_announced(consumer: &MoqOriginConsumer, path: &str) -> Arc MoqAnnounceEvent { /// The next newly announced route. async fn next_announced(announced: &MoqAnnounceConsumer) -> MoqAnnounce { match next_event(announced).await { - MoqAnnounceEvent::Start { announce } => announce, + MoqAnnounceEvent::Present { announce } => announce, other => panic!("expected an announcement, got {other:?}"), } } @@ -426,8 +427,7 @@ async fn announced_route_keeps_static_cost_on_reannounce() { // Reading an advertisement preserves its static production price. let announced = consumer.announced(MoqAnnounceConfig::default()).unwrap(); let route = loop { - if let MoqAnnounceEvent::Start { announce } | MoqAnnounceEvent::Update { announce } = - next_event(&announced).await + if let MoqAnnounceEvent::Present { announce } = next_event(&announced).await && announce.prefix == "priced-route" { break announce.route; @@ -471,10 +471,11 @@ fn instance_routes_parse_and_refuse() { } #[tokio::test] -async fn announced_restarts_on_a_new_instance() { +async fn following_restarts_on_a_new_instance() { let origin = MoqOriginProducer::new(MoqOriginConfig::default()); let consumer = origin.consume(); let announced = consumer.announced(MoqAnnounceConfig::default()).unwrap(); + let following = consumer.follow("instance-route".into(), None).unwrap(); let publication = origin.create_broadcast("instance-route".into()).unwrap(); let first = moq_net::Instance::random().to_string(); @@ -486,8 +487,10 @@ async fn announced_restarts_on_a_new_instance() { .unwrap(); let announce = next_announced(&announced).await; assert_eq!(announce.route.instance, first); + following.available().await.unwrap().unwrap(); - // A new run of the same publisher is another instance: consumers restart onto it. + // A new run of the same publisher is another instance: followers restart onto it, while + // the path stays listed. let second = moq_net::Instance::random().to_string(); publication .announce(MoqAnnounceRoute { @@ -495,12 +498,18 @@ async fn announced_restarts_on_a_new_instance() { ..Default::default() }) .unwrap(); + kio::clock::timeout(TIMEOUT, following.available()) + .await + .expect("the new instance is handed out") + .unwrap() + .unwrap(); + + // The next listed event is the path going absent under the new instance: the restart + // itself was never listed. + publication.unannounce().unwrap(); match next_event(&announced).await { - MoqAnnounceEvent::Restart { announce } => { - assert_eq!(announce.prefix, "instance-route"); - assert_eq!(announce.route.instance, second); - } - other => panic!("expected a restart, got {other:?}"), + MoqAnnounceEvent::Absent { announce } => assert_eq!(announce.route.instance, second), + other => panic!("expected an absence, got {other:?}"), } publication.broadcast().close().unwrap(); @@ -3359,7 +3368,7 @@ async fn create_broadcast_is_invisible_until_announced() { .expect("announce reaches the cursor") .expect("the cursor is still open"); assert!( - matches!(&update, MoqAnnounceEvent::Start { announce } if announce.prefix == "live"), + matches!(&update, MoqAnnounceEvent::Present { announce } if announce.prefix == "live"), "{update:?}" ); publication.broadcast().close().unwrap(); @@ -3379,6 +3388,7 @@ async fn follow_keeps_the_requested_path() { let waited = kio::clock::timeout(TIMEOUT, announced.available()) .await .expect("timed out waiting for the announcement") + .unwrap() .unwrap(); assert_eq!(waited.inner().info().path.as_str(), "a/pub"); @@ -3785,8 +3795,8 @@ async fn announce_and_unannounce_toggles_discovery() { async fn wait_live(announced: &MoqAnnounceConsumer, active: bool) { loop { match next_event(announced).await { - MoqAnnounceEvent::Start { announce } if active && announce.prefix == "live" => return, - MoqAnnounceEvent::End { announce } if !active && announce.prefix == "live" => return, + MoqAnnounceEvent::Present { announce } if active && announce.prefix == "live" => return, + MoqAnnounceEvent::Absent { announce } if !active && announce.prefix == "live" => return, _ => {} } } @@ -3876,6 +3886,7 @@ async fn finish_unpublishes() { kio::clock::timeout(TIMEOUT, announced.available()) .await .expect("timed out waiting for the announcement") + .unwrap() .unwrap(); // A graceful finish detaches immediately; the path stops resolving. Removal is @@ -4732,7 +4743,7 @@ fn close_twice_is_a_noop() { /// Wait for the announcement of `path` to end, failing the test if it doesn't. async fn await_unannounced(announced: &MoqAnnounceConsumer) { match next_event(announced).await { - MoqAnnounceEvent::End { .. } => {} + MoqAnnounceEvent::Absent { .. } => {} other => panic!("expected the announcement to end, got {other:?}"), } } @@ -5681,7 +5692,7 @@ fn without_runtime() { let announced = consumer.announced(MoqAnnounceConfig::default()).unwrap(); let announcement = match pollster::block_on(announced.next()).unwrap().unwrap() { - MoqAnnounceEvent::Start { announce } => announce, + MoqAnnounceEvent::Present { announce } => announce, other => panic!("expected an announcement, got {other:?}"), }; assert_eq!(announcement.prefix, "test"); @@ -5804,6 +5815,7 @@ async fn server_client_run_end(version: &str) { let bc = kio::clock::timeout(TIMEOUT, following.available()) .await .expect("timed out waiting for the first run") + .unwrap() .unwrap(); let unfinished = broadcast .publish_track("unfinished".into(), Some(timed_info())) @@ -5893,11 +5905,16 @@ async fn server_client_run_end(version: &str) { // The announcement, not the track's error, ends the run. match next_event(&announced).await { - MoqAnnounceEvent::End { announce } => assert_eq!(announce.prefix, "hello"), + MoqAnnounceEvent::Absent { announce } => assert_eq!(announce.prefix, "hello"), other => panic!("{version}: expected the run's end, got {other:?}"), } - // A replacement run at the same path reaches the follower on its next `available()`. + // The follower reports the run's end, then hands out a replacement run at the same path. + let ended = kio::clock::timeout(TIMEOUT, following.available()) + .await + .unwrap_or_else(|_| panic!("{version}: the run's end never reached the follower")) + .unwrap(); + assert!(ended.is_none(), "{version}: expected the run's end"); let replacement = create_announced(&server_origin, "hello"); let replacement_media = MoqMediaTrackProducer::audio( &replacement, @@ -5908,7 +5925,8 @@ async fn server_client_run_end(version: &str) { let next = kio::clock::timeout(TIMEOUT, following.available()) .await .unwrap_or_else(|_| panic!("{version}: the next run was never handed out")) - .unwrap(); + .unwrap() + .expect("the next run, not an end"); let next_track = subscribe_audio(&next).await; roundtrip(&replacement_media, &next_track, b"next run", 2_000_000).await; replacement_media.finish().unwrap(); @@ -6016,6 +6034,7 @@ async fn datagram_connection( let remote = kio::clock::timeout(TIMEOUT, followed.available()) .await .expect("timed out waiting for the broadcast") + .unwrap() .unwrap(); (broadcast, remote, session, (server, server_origin, server_session)) } @@ -6151,6 +6170,7 @@ async fn follow_follows_a_minted_replacement() { let old = kio::clock::timeout(TIMEOUT, followed.available()) .await .expect("timed out waiting for the broadcast") + .unwrap() .unwrap(); let old_track = subscribe_audio(&old).await; roundtrip(&media, &old_track, b"before", 1_000_000).await; @@ -6159,6 +6179,7 @@ async fn follow_follows_a_minted_replacement() { let fresh = kio::clock::timeout(TIMEOUT, followed.available()) .await .expect("the replacement was never handed out") + .unwrap() .unwrap(); first.shutdown(); first.closed().await.unwrap(); @@ -6259,12 +6280,16 @@ async fn follow_hands_out_the_instance_again_after_an_end() { let first = kio::clock::timeout(TIMEOUT, followed.available()) .await .expect("timed out waiting for the broadcast") + .unwrap() .unwrap(); // A covering claim answering under the same instance. let dynamic = origin.inner().inner.requests("").unwrap(); publication.unannounce().unwrap(); - let again = tokio::spawn(async move { followed.available().await }); + let again = tokio::spawn(async move { + assert!(followed.available().await?.is_none(), "the end comes first"); + followed.available().await + }); let request = kio::clock::timeout(TIMEOUT, dynamic.requested_broadcast()) .await .expect("the instance never asked the route above the path") @@ -6278,7 +6303,8 @@ async fn follow_hands_out_the_instance_again_after_an_end() { .await .expect("the instance was never handed out again") .unwrap() - .unwrap(); + .unwrap() + .expect("the instance, not an end"); assert_eq!( again.inner().info().instance, first.inner().info().instance, @@ -6333,6 +6359,7 @@ async fn an_instance_pins_request_and_follow() { kio::clock::timeout(TIMEOUT, followed.available()) .await .expect("timed out following the pinned instance") + .unwrap() .unwrap(); let second = origin.create_broadcast("live".into()).unwrap(); second @@ -6351,6 +6378,12 @@ async fn an_instance_pins_request_and_follow() { takeover.inner().info().instance.as_ref().map(ToString::to_string), Some(instance) ); + // Another instance taking over ends the pinned follow, which hands out nothing else. + let ended = kio::clock::timeout(TIMEOUT, followed.available()) + .await + .expect("timed out waiting for the pinned follow's end") + .unwrap(); + assert!(ended.is_none(), "the follower handed out another instance"); let next = std::pin::pin!(followed.available()); assert!( next.poll(&mut std::task::Context::from_waker(std::task::Waker::noop())) @@ -6427,6 +6460,7 @@ async fn follow_waits_for_another_instance_after_a_dropped_takeover() { kio::clock::timeout(TIMEOUT, followed.available()) .await .expect("the next instance was never handed out") + .unwrap() .unwrap(); takeover.close().unwrap(); next.close().unwrap(); @@ -6504,7 +6538,8 @@ async fn server_client_roundtrip_auto_origin() { kio::clock::timeout(TIMEOUT, local_announced.available()) .await .expect("timed out waiting for the loopback broadcast") - .expect("an auto-origin session should discover its own announcement"); + .expect("an auto-origin session should discover its own announcement") + .unwrap(); local_broadcast.close().unwrap(); media.finish().unwrap(); diff --git a/rs/moq-gst/src/source/imp.rs b/rs/moq-gst/src/source/imp.rs index c42c9782852..3aeb3d36678 100644 --- a/rs/moq-gst/src/source/imp.rs +++ b/rs/moq-gst/src/source/imp.rs @@ -842,9 +842,7 @@ impl Session { path: &str, lost: impl Future, ) -> Result<()> { - use moq_mux::follow::{Event, Instances}; - - let mut follow = Instances::new(origin, path, None)?; + let mut follow = origin.follow(path, None)?; let mut shutdown = self.shutdown.clone(); let mut runs = tokio::task::JoinSet::new(); // Cancels the latest run. @@ -856,16 +854,16 @@ impl Session { let broadcast = tokio::select! { _ = shutdown.changed() => break Ok(()), err = &mut lost => break Err(err), - event = follow.next() => match event { - Ok(Event::Start(broadcast)) => { + event = follow.available() => match event { + Ok(Some(broadcast)) => { tracing::info!(%path, instance = ?broadcast.info().instance, "online"); Some(broadcast) } - Ok(Event::End) => { + Ok(None) => { tracing::info!(%path, "offline, holding pads until it returns"); None } - Err(moq_mux::Error::Moq(moq_net::Error::Closed)) => break Ok(()), + Err(moq_net::Error::Closed) => break Ok(()), Err(err) => break Err(anyhow::Error::from(err).context("broadcast refused")), }, joined = runs.join_next(), if !runs.is_empty() => match joined.expect("guarded by is_empty") { @@ -1322,7 +1320,7 @@ impl Follower { // The catalog's own broadcast is ready at once, so its renditions play even when the // catalog closed before this follower ran. match instances.poll_next(&moq_net::kio::Waiter::noop()) { - std::task::Poll::Ready(Ok(moq_mux::follow::Event::Start(instance))) => self.start(instance), + std::task::Poll::Ready(Ok(Some(instance))) => self.start(instance), std::task::Poll::Ready(Err(err)) => { gst::warning!(CAT, "lost {}: {err:?}", self.path); return; @@ -1382,11 +1380,11 @@ impl Follower { } } event = instances.next(), if following => match event { - Ok(moq_mux::follow::Event::Start(instance)) => { + Ok(Some(instance)) => { gst::info!(CAT, "{} is online, starting its renditions", self.path); self.start(instance); } - Ok(moq_mux::follow::Event::End) => { + Ok(None) => { gst::info!(CAT, "{} is offline, holding its pads", self.path); self.offline(); } @@ -4080,7 +4078,7 @@ mod session_tests { } /// Block until the path's announcements report an event `wanted` accepts. - fn await_event(follow: &mut moq_net::announce::Consumer, wanted: impl Fn(&moq_net::announce::Event) -> bool) { + fn await_event(follow: &mut moq_net::origin::Routes, wanted: impl Fn(&moq_net::origin::RouteEvent) -> bool) { let next = async { while let Some(event) = follow.next().await { if wanted(&event) { @@ -4104,15 +4102,17 @@ mod session_tests { } /// The announcements of the routes covering `path` on `origin`. - fn watch(origin: &moq_net::origin::Producer, path: &str) -> moq_net::announce::Consumer { + fn watch(origin: &moq_net::origin::Producer, path: &str) -> moq_net::origin::Routes { let path = moq_net::Patterns::from(moq_net::Pattern::literal(path).unwrap()); - origin.consume().scope("", &path).unwrap().announced() + origin.consume().scope("", &path).unwrap().routes() } /// Follow `room` on `origin` past its start. - fn announced(origin: &moq_net::origin::Producer) -> moq_net::announce::Consumer { + fn announced(origin: &moq_net::origin::Producer) -> moq_net::origin::Routes { let mut follow = watch(origin, "room"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); follow } @@ -4233,9 +4233,13 @@ mod session_tests { let mut first = Publisher::sibling(&origin, Default::default()); let pad = recorder.written(&mut first, 1); let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); drop(first); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::End(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::End(_)) + }); let mut second = Publisher::sibling(&origin, Default::default()); assert_eq!(recorder.written(&mut second, 2), pad, "the return moved to another pad"); @@ -4290,9 +4294,13 @@ mod session_tests { assert_eq!(recorder.written(&mut first, 2), pad); let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); drop(first); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::End(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::End(_)) + }); let mut second = Publisher::sibling(&origin, Default::default()); assert_eq!(recorder.written(&mut second, 3), pad, "the return moved to another pad"); assert_eq!( @@ -4386,11 +4394,17 @@ mod session_tests { let pad = recorder.written(&mut first, 1); first.video.finish().unwrap(); await_finished(&first); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); first.publication.as_ref().unwrap().unannounce(); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::End(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::End(_)) + }); first.publication.as_ref().unwrap().announce(route).unwrap(); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); let mut second = Publisher::sibling(&origin, Default::default()); assert_eq!( @@ -4465,9 +4479,13 @@ mod session_tests { let pad = recorder.written(&mut first, 1); room.catalog.finish().unwrap(); let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); drop(first); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::End(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::End(_)) + }); let mut second = Publisher::sibling(&origin, Default::default()); assert_eq!(recorder.written(&mut second, 2), pad, "the return moved to another pad"); @@ -4742,14 +4760,16 @@ mod session_tests { let unpinned = recorder.written(&mut second, 2); let pad = played.into_iter().find(|pad| *pad != unpinned).unwrap(); let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); drop(second); await_event(&mut follow, |event| { matches!( event, - moq_net::announce::Event::Restart(_) - | moq_net::announce::Event::Takeover { .. } - | moq_net::announce::Event::Start(_) + moq_net::origin::RouteEvent::Restart(_) + | moq_net::origin::RouteEvent::Takeover { .. } + | moq_net::origin::RouteEvent::Start(_) ) }); @@ -4797,9 +4817,13 @@ mod session_tests { let mut first = Publisher::sibling(&origin, Default::default()); let pad = recorder.written(&mut first, 1); let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); drop(first); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::End(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::End(_)) + }); room.catalog.modify().unwrap().video.renditions.clear(); for _ in 0..200 { @@ -4814,7 +4838,9 @@ mod session_tests { ); let mut second = Publisher::sibling(&origin, Default::default()); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); for _ in 0..10 { second.write(2); block(Duration::from_millis(50)); @@ -4839,9 +4865,13 @@ mod session_tests { let mut first = Publisher::sibling(&origin, Default::default()); let pad = recorder.written(&mut first, 1); let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); drop(first); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::End(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::End(_)) + }); let listed = room.catalog.modify().unwrap().video.renditions.clone(); room.catalog.modify().unwrap().video.renditions.clear(); @@ -4915,16 +4945,18 @@ mod session_tests { // The reference lets the old instance go once another wins its path, and only then // does the new one leave, so the return is a resume rather than a missed restart. let mut follow = watch(&origin, "cam"); - await_event(&mut follow, |event| matches!(event, moq_net::announce::Event::Start(_))); + await_event(&mut follow, |event| { + matches!(event, moq_net::origin::RouteEvent::Start(_)) + }); let mut new = Publisher::sibling(&origin, Default::default()); await_event(&mut follow, |event| { - matches!(event, moq_net::announce::Event::Restart(_)) + matches!(event, moq_net::origin::RouteEvent::Restart(_)) }); await_demand(&old, false); new.write(2); drop(new); await_event(&mut follow, |event| { - matches!(event, moq_net::announce::Event::Restart(_)) + matches!(event, moq_net::origin::RouteEvent::Restart(_)) }); await_demand(&old, true); @@ -5031,7 +5063,7 @@ mod session_tests { .announce(route.with_cost(5)) .unwrap(); await_event(&mut announced, |event| { - matches!(event, moq_net::announce::Event::Update(_)) + matches!(event, moq_net::origin::RouteEvent::Update(_)) }); recorder.written(&mut publisher, 2); stop(session); @@ -5146,7 +5178,7 @@ mod session_tests { old.catalog.finish().unwrap(); drop(old); await_event(&mut announced, |event| { - matches!(event, moq_net::announce::Event::End(_)) + matches!(event, moq_net::origin::RouteEvent::End(_)) }); let mut new = Publisher::new(&origin, Default::default(), video_rendition()); @@ -5176,7 +5208,7 @@ mod session_tests { old.publication.as_ref().unwrap().unannounce(); await_event(&mut announced, |event| { - matches!(event, moq_net::announce::Event::End(_)) + matches!(event, moq_net::origin::RouteEvent::End(_)) }); drop(old); @@ -5230,7 +5262,7 @@ mod session_tests { let publication = next.publication.as_ref().unwrap(); publication.unannounce(); await_event(&mut announced, |event| { - matches!(event, moq_net::announce::Event::End(_)) + matches!(event, moq_net::origin::RouteEvent::End(_)) }); publication.announce(route).unwrap(); assert_eq!( @@ -5710,7 +5742,7 @@ mod session_tests { let mut announced = announced(&relay.origin); session.abort(moq_net::Error::App(1)); await_event(&mut announced, |event| { - matches!(event, moq_net::announce::Event::End(_)) + matches!(event, moq_net::origin::RouteEvent::End(_)) }); let mut new = Publisher::new(&relay.origin, Default::default(), video_rendition()); diff --git a/rs/moq-hls/src/export/contributions.rs b/rs/moq-hls/src/export/contributions.rs index 13ee050ea1c..e08b341c928 100644 --- a/rs/moq-hls/src/export/contributions.rs +++ b/rs/moq-hls/src/export/contributions.rs @@ -246,7 +246,7 @@ async fn follow_instances(mut instances: follow::Instances, current: kio::Produc loop { let next = instances.next().await; let next = match next { - Ok(follow::Event::Start(broadcast)) => { + Ok(Some(broadcast)) => { let id = broadcast .info() .instance @@ -266,7 +266,7 @@ async fn follow_instances(mut instances: follow::Instances, current: kio::Produc _owner: owner, })) } - Ok(follow::Event::End) => Current::Offline, + Ok(None) => Current::Offline, // An authorization refusal is final: the contribution stays out and its URLs answer 403. Err(err @ moq_mux::Error::Moq(moq_net::Error::Unauthorized)) => { tracing::debug!(%err, "contribution refused"); diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index c3df8030db7..39291ffeea7 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -2444,6 +2444,7 @@ mod tests { .unwrap() .available() .await + .unwrap() .unwrap(); let source = crate::resolved(consuming, "live").await; let broadcaster = (!late).then(|| Broadcaster::spawned(source.clone(), Config::default())); diff --git a/rs/moq-hls/src/server/mod.rs b/rs/moq-hls/src/server/mod.rs index bcd26910f4b..8b64de537d4 100644 --- a/rs/moq-hls/src/server/mod.rs +++ b/rs/moq-hls/src/server/mod.rs @@ -71,7 +71,6 @@ use std::task::Poll; use std::time::Duration; use axum::Router; -use moq_mux::follow; use crate::export::{Broadcaster, Config}; @@ -230,7 +229,10 @@ impl Server { return Some(followed.clone()); } - let instances = follow::Instances::new(&self.inner.origin, name, None) + let instances = self + .inner + .origin + .follow(name, None) .map_err(|err| tracing::warn!(%err, %name, "failed to follow broadcast")) .ok()?; // A request answers once the origin has synced with the relay, so a path with no route @@ -297,7 +299,7 @@ async fn run_follow( inner: Weak, name: String, followed: Weak, - mut instances: follow::Instances, + mut instances: moq_net::origin::Following, served: moq_net::broadcast::Consumer, ) { let mut served = Some(served); @@ -307,13 +309,13 @@ async fn run_follow( return Poll::Ready(None); }; followed.forget_closed(waiter); - if let Poll::Ready(next) = instances.poll_next(waiter) { + if let Poll::Ready(next) = instances.poll_available(waiter) { return Poll::Ready(Some(next)); } // The follower folds away an instance that started and ended before its first poll, // so the served instance closing before it is handed out means nothing serves the path. match &served { - Some(served) => served.poll_closed(waiter).map(|()| Some(Ok(follow::Event::End))), + Some(served) => served.poll_closed(waiter).map(|()| Some(Ok(None))), None => Poll::Pending, } }) @@ -322,12 +324,12 @@ async fn run_follow( return; }; match next { - Some(Ok(follow::Event::Start(broadcast))) => { + Some(Ok(Some(broadcast))) => { if !served.take().is_some_and(|served| served.is_clone(&broadcast)) { followed.start(inner.serve(broadcast)); } } - Some(Ok(follow::Event::End)) => break (inner, followed), + Some(Ok(None)) => break (inner, followed), Some(Err(err)) => { tracing::warn!(%err, %name, "stopped following broadcast"); break (inner, followed); diff --git a/rs/moq-hls/tests/integration/server.rs b/rs/moq-hls/tests/integration/server.rs index 65dce640351..0b679cdabb0 100644 --- a/rs/moq-hls/tests/integration/server.rs +++ b/rs/moq-hls/tests/integration/server.rs @@ -53,7 +53,8 @@ async fn a_cursor_records_a_publisher_without_fetch() { let remote = kio::clock::timeout(TIMEOUT, origin.follow("live", None).unwrap().available()) .await .expect("request timed out") - .expect("remote broadcast"); + .expect("remote broadcast") + .unwrap(); let (broadcaster, driver) = Broadcaster::new(moq_mux::Source::new(origin, remote), Default::default()).unwrap(); tokio::spawn(driver.run()); // Recorded as soon as the catalog lists it with its records, as a recorder does. @@ -158,7 +159,8 @@ async fn a_sibling_resolves_through_a_client_session() { let broadcast = kio::clock::timeout(TIMEOUT, origin.follow("room/live", None).unwrap().available()) .await .expect("catalog resolve timed out") - .expect("catalog broadcast"); + .expect("catalog broadcast") + .unwrap(); let source = moq_mux::Source::new(origin, broadcast); let sibling = kio::clock::timeout( TIMEOUT, @@ -216,7 +218,8 @@ async fn a_disconnected_sibling_publisher_ends_the_playlist() { ) .await .expect("sibling resolve timed out") - .expect("sibling broadcast"); + .expect("sibling broadcast") + .unwrap(); // Only the sibling goes: the catalog keeps listing the rendition, so the request reaches the // media fetch rather than missing the rendition. media_broadcast.close(); diff --git a/rs/moq-mux/MIGRATE.md b/rs/moq-mux/MIGRATE.md index 922ddcf04fa..2817ab45268 100644 --- a/rs/moq-mux/MIGRATE.md +++ b/rs/moq-mux/MIGRATE.md @@ -4,6 +4,11 @@ How to adopt new APIs and update for breaking changes, newest first. ## Unreleased +### `follow::Instances` folds into `Following` ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`follow::Instances::new` is removed: call `origin.follow(path, instance)` and read `Following::available()`, which returns `Some(broadcast)` where `Event::Start` did and `None` where `Event::End` did. +`Source::follow` still returns `follow::Instances`, for its one-shot hand-out of the catalog's own broadcast; its `next()` and `poll_next()` now return `Option`, and `follow::Event` is gone. + ### `container::Playout` replaces `with_warmup` ([#7864](https://github.com/moq-dev/moq/pull/7864)) `container::Consumer::with_warmup` is gone. Build a `container::Playout::new(delay, warmup)` from the `Delay` and the catalog rendition's `warmup`, subscribe with `playout.subscription(subscription)`, which widens the max delay by the warmup, and build the consumer with `playout.consumer(track, format)`, which skips at the max delay alone. The order no longer matters, and a track subscribed without the widening is widened when the consumer is built. diff --git a/rs/moq-mux/src/container/source.rs b/rs/moq-mux/src/container/source.rs index a180f53d053..80fd521c1aa 100644 --- a/rs/moq-mux/src/container/source.rs +++ b/rs/moq-mux/src/container/source.rs @@ -1135,7 +1135,13 @@ mod tests { .unwrap(); broadcast.announce(Default::default()).unwrap(); let consumer = subscriber.consume(); - let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let source = crate::Source::new(consumer, remote); let mut export = ExportSource::for_audio(&source, "audio", &audio(None), Duration::from_secs(1)) .unwrap() @@ -1212,7 +1218,13 @@ mod tests { .unwrap(); broadcast.announce(Default::default()).unwrap(); let consumer = subscriber.consume(); - let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let source = crate::Source::new(consumer, remote); // MPEG-1 audio from TS: one 24 ms frame per group, a PES of five written at once. diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index f0c1ea39498..b8d7aceb0b0 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -9009,6 +9009,7 @@ async fn same_instance_returns(finish: bool) { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); let mut export = export.follow(back, stream).unwrap(); @@ -9091,6 +9092,7 @@ async fn a_return_keeps_catalog_and_group_history() { .unwrap() .available() .await + .unwrap() .unwrap(); assert_eq!(back.info().instance.as_ref(), Some(&instance)); assert!(!back.is_clone(&old), "the withdrawn front is replaced"); @@ -9375,6 +9377,7 @@ async fn export_stats_count_only_returned_output() { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); let mut export = export.follow(back, stream).unwrap(); @@ -11516,6 +11519,7 @@ async fn a_track_renamed_on_its_pid_across_returns_retires_the_one_before() { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); export = export.follow(back, stream).unwrap(); @@ -11582,6 +11586,7 @@ async fn a_track_renamed_on_its_pid_across_a_return_is_quiet_from_its_listing() .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); let mut export = export.follow(back, stream).unwrap(); @@ -11988,6 +11993,7 @@ async fn an_expired_follow_stops_healthy_input_but_drains_its_buffered_tail() { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&target, Default::default()) .await @@ -12145,6 +12151,7 @@ async fn a_replacement_leaving_before_it_commits_keeps_the_old_program() { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&target, Default::default()) .await @@ -12275,6 +12282,7 @@ async fn an_empty_finished_replacement_drains_the_old_program() { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&replacement, Default::default()) .await @@ -12333,6 +12341,7 @@ async fn cold_same_instance_return(pending: bool) { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&target, Default::default()) .await @@ -12363,6 +12372,7 @@ async fn cold_same_instance_return(pending: bool) { .unwrap() .available() .await + .unwrap() .unwrap(); let stream = crate::catalog::Consumer::new(&target, Default::default()) .await diff --git a/rs/moq-mux/src/container/ts/follower.rs b/rs/moq-mux/src/container/ts/follower.rs index 1f3bdbf0beb..4ffe82c9cd2 100644 --- a/rs/moq-mux/src/container/ts/follower.rs +++ b/rs/moq-mux/src/container/ts/follower.rs @@ -9,7 +9,6 @@ use kio::clock::Instant; use super::{Export, catalog}; use crate::container::Frame; -use crate::follow::{Event, Instances}; type Subscribing = Pin>> + Send>>; @@ -60,7 +59,7 @@ enum State { } /// Carries an [`Export`] across its broadcast's returns, following the publisher instances -/// serving its path through [`Instances`]. +/// serving its path through [`Following`](moq_net::origin::Following). /// /// The same stream (the same instance) returning within the [follow](Self::with_follow) /// continues the export. Another instance taking the path fails with @@ -77,11 +76,11 @@ enum State { /// An instance that comes back is awaited until its catalog serves a first snapshot, with no /// deadline but its own end. A return that refuses its catalog waits for the next change, and /// the follow running out reports that refusal. A refusal to resolve the path at all fails at -/// once, as it does for every [`Instances`] reader. +/// once, as it does for every [`Following`](moq_net::origin::Following) reader. pub struct Follower { export: Export>, state: State, - instances: Instances, + instances: moq_net::origin::Following, path: moq_net::Path, serving: Serving, /// Taking the instances on hand when built, which replay the route the export resolved. @@ -103,7 +102,7 @@ impl Follower { /// Fails when the export's origin can never cover its path. pub fn new(export: Export>) -> crate::Result { let source = export.source(); - let instances = Instances::new(source.origin(), source.path(), None)?; + let instances = source.origin().follow(source.path(), None)?; let mut follower = Self { path: source.path().clone(), export, @@ -344,8 +343,8 @@ impl Follower { fn poll_instances(&mut self, waiter: &kio::Waiter) -> crate::Result { let mut any = false; while !self.closed { - match self.instances.poll_next(waiter) { - Poll::Ready(Ok(Event::Start(broadcast))) => { + match self.instances.poll_available(waiter) { + Poll::Ready(Ok(Some(broadcast))) => { self.serving = match self.export.instance() { // A broadcast not resolved through an origin has no instance to match, // so only the broadcast being exported is its own. @@ -359,9 +358,9 @@ impl Follower { _ => Serving::Other(broadcast), }; } - Poll::Ready(Ok(Event::End)) => self.serving = Serving::Gone, - Poll::Ready(Err(crate::Error::Moq(moq_net::Error::Closed))) => self.closed = true, - Poll::Ready(Err(err)) => return Err(err), + Poll::Ready(Ok(None)) => self.serving = Serving::Gone, + Poll::Ready(Err(moq_net::Error::Closed)) => self.closed = true, + Poll::Ready(Err(err)) => return Err(err.into()), Poll::Pending => break, } any = true; diff --git a/rs/moq-mux/src/follow.rs b/rs/moq-mux/src/follow.rs index 8182b083c1b..675cc5e18c7 100644 --- a/rs/moq-mux/src/follow.rs +++ b/rs/moq-mux/src/follow.rs @@ -1,110 +1,92 @@ -//! Following a broadcast across the publisher instances that serve it. +//! Following a catalog rendition's broadcast across the publisher instances that serve it. //! //! An announcement is the online and offline signal, so a reader that restarts on each instance //! follows [`Instances`] rather than pinning one broadcast as a [`Binding`](crate::Binding) does. -use std::task::Poll; +use std::task::{Poll, ready}; -/// The publisher instances serving a broadcast, as each comes online, takes over, and goes. +/// The publisher instances serving a rendition's broadcast, as each comes online, takes over, +/// and goes. Created by [`Source::follow`](crate::Source::follow). /// -/// Built on [`follow`](moq_net::origin::Consumer::follow). Create one -/// with [`Source::follow`](crate::Source::follow) for a catalog reference, or [`Instances::new`] -/// for a path. -pub struct Instances { - feed: Feed, - /// The instance a reference named, if any. - instance: Option, - /// The instance handed out last, until it stops serving. - current: Option, -} +/// A sibling broadcast is followed through [`Following`](moq_net::origin::Following); the +/// catalog's own broadcast is handed out once and its end never reported, since a reader +/// follows its catalog's end itself. +pub struct Instances(Feed); enum Feed { /// Every instance announced at a path. - Announced(Box), + Following { + following: Box, + /// The instance a reference named, if any. + instance: Option, + /// An instance was handed out and its end not reported yet. + live: bool, + }, /// One instance, handed out once. Fixed(Option), } -/// A change to the instance [`Instances`] serves. -pub enum Event { - /// Start on this instance, dropping whatever was read from the one before. - Start(moq_net::broadcast::Consumer), - /// The instance handed out last stopped serving: idle until the next start. - End, -} - impl Instances { - /// Follow the broadcast at `path` through `origin`, handing out only `instance` when given. + /// Follow every instance, handing out only `instance` when given. /// - /// Unlike [`Source::follow`](crate::Source::follow), this needs no catalog broadcast, so a - /// reader can follow the broadcast whose catalog it reads and build a - /// [`Source`](crate::Source) for each instance. - pub fn new( - origin: &moq_net::origin::Consumer, - path: impl TryInto>, - instance: Option<&moq_net::Instance>, - ) -> crate::Result { - let announced = origin.follow(path, None)?; - Ok(Self::with(Feed::Announced(Box::new(announced)), instance)) + /// The path is followed unpinned and each instance checked here, rather than pinned in the + /// origin: a hop older than moq-lite 07 mints instances a pin never matches, which + /// [`check_instance`](crate::source::check_instance) lets through, and another instance + /// winning the path ends the rendition rather than leaving an older one to resolve. + pub(crate) fn following(following: moq_net::origin::Following, instance: Option) -> Self { + Self(Feed::Following { + following: Box::new(following), + instance, + live: false, + }) } - /// Hand out `broadcast` once and never report its end, which its owner follows. pub(crate) fn fixed(broadcast: moq_net::broadcast::Consumer) -> Self { - Self::with(Feed::Fixed(Some(broadcast)), None) - } - - fn with(feed: Feed, instance: Option<&moq_net::Instance>) -> Self { - Self { - feed, - instance: instance.cloned(), - current: None, - } + Self(Feed::Fixed(Some(broadcast))) } /// Poll for the next change, registering `waiter` when there is none yet. /// - /// Each announcement that brings an instance hands it out, so the same one returns after an - /// end. Fails once the origin closes, and with any resolution failure, such as a handler - /// refusing the path; the call after a refusal waits for the path's next announcement. - pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { - loop { - let next = match &mut self.feed { - // Followed unpinned and checked here, so another instance taking over ends the - // rendition rather than waiting for the named one to come back. - Feed::Announced(announced) => announced.poll_available(waiter).map_err(crate::Error::from), - Feed::Fixed(broadcast) => match broadcast.take() { - Some(broadcast) => Poll::Ready(Ok(broadcast)), + /// Returns each instance as it comes online or takes over, and `None` once the path goes + /// offline or, with an instance named, another one takes it over; see + /// [`Following::available`](moq_net::origin::Following::available). Fails once the origin + /// closes, and with any resolution failure, such as a handler refusing the path; the call + /// after a refusal waits for the serving route to change. + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { + let (following, instance, live) = match &mut self.0 { + Feed::Following { + following, + instance, + live, + } => (following, instance, live), + Feed::Fixed(broadcast) => { + return match broadcast.take() { + Some(broadcast) => Poll::Ready(Ok(Some(broadcast))), None => Poll::Pending, - }, - }; - if let Poll::Ready(next) = next { - let broadcast = next?; - if crate::source::check_instance(self.instance.as_ref(), &broadcast).is_err() { - tracing::debug!(path = %broadcast.info().path, "skipping a broadcast of another instance"); - match self.current.take() { - Some(_) => return Poll::Ready(Ok(Event::End)), - None => continue, - } + }; + } + }; + loop { + match ready!(following.poll_available(waiter))? { + Some(broadcast) if crate::source::check_instance(instance.as_ref(), &broadcast).is_ok() => { + *live = true; + return Poll::Ready(Ok(Some(broadcast))); } - // A fixed instance ends with its owner, which follows that itself. - if let Feed::Announced(_) = self.feed { - self.current = Some(broadcast.clone()); + Some(broadcast) => { + tracing::debug!(path = %broadcast.info().path, "skipping a broadcast of another instance"); } - return Poll::Ready(Ok(Event::Start(broadcast))); + None => {} } - - if let Some(current) = &self.current - && current.poll_closed(waiter).is_ready() - { - self.current = None; - return Poll::Ready(Ok(Event::End)); + // Report the end of what was handed out once, whether it went offline or another + // instance took over. + if std::mem::take(live) { + return Poll::Ready(Ok(None)); } - return Poll::Pending; } } /// The next change; see [`Self::poll_next`]. - pub async fn next(&mut self) -> crate::Result { + pub async fn next(&mut self) -> crate::Result> { kio::wait(|waiter| self.poll_next(waiter)).await } } @@ -128,14 +110,10 @@ mod tests { /// The next change a follower reports, which must arrive within the origin's update hold. async fn followed(follow: &mut Instances) -> Option { - let next = kio::clock::timeout(std::time::Duration::from_secs(1), follow.next()) + kio::clock::timeout(std::time::Duration::from_secs(1), follow.next()) .await .expect("no change") - .expect("the follow failed"); - match next { - Event::Start(broadcast) => Some(broadcast), - Event::End => None, - } + .expect("the follow failed") } /// The follower stays quiet past the origin's update hold. @@ -204,6 +182,25 @@ mod tests { assert_eq!(back.info().instance.as_ref(), Some(&old_instance)); } + /// A reference naming an instance still follows a sibling whose instance an older hop + /// minted, which proves nothing either way. + #[tokio::test(start_paused = true)] + async fn a_named_instance_follows_a_minted_one() { + let origin = produce_origin(); + let _catalog = origin.publish("a/pub", Default::default()).unwrap(); + let minted: moq_net::Instance = "~minted".parse().unwrap(); + assert!(minted.is_minted()); + let _sibling = origin.publish("a/source", route(&minted)).unwrap(); + settle().await; + let source = resolved(&origin, "a/pub").await; + let named = moq_net::Instance::random(); + let mut follow = source + .follow(Some(&Relative::new("./source").unwrap()), Some(&named)) + .unwrap(); + let served = followed(&mut follow).await.expect("the minted sibling"); + assert_eq!(served.info().instance.as_ref(), Some(&minted)); + } + /// A self-reference reads only the catalog instance, whatever replaces it at the path. #[tokio::test(start_paused = true)] async fn a_follow_of_the_catalog_keeps_its_instance() { diff --git a/rs/moq-mux/src/source.rs b/rs/moq-mux/src/source.rs index f7673a46ee6..c90d9955712 100644 --- a/rs/moq-mux/src/source.rs +++ b/rs/moq-mux/src/source.rs @@ -389,7 +389,7 @@ impl Source { check_instance(instance, &broadcast)?; crate::follow::Instances::fixed(broadcast) } - false => crate::follow::Instances::new(&self.origin, &path, instance)?, + false => crate::follow::Instances::following(self.origin.follow(&path, None)?, instance.cloned()), }) } } @@ -506,7 +506,14 @@ fn poll_first( waiter: &kio::Waiter, ) -> Poll> { let result = match following { - Ok(following) => ready!(following.poll_available(waiter)), + // The first change is always an instance: an end follows only one handed out. + Ok(following) => loop { + match ready!(following.poll_available(waiter)) { + Ok(Some(broadcast)) => break Ok(broadcast), + Ok(None) => continue, + Err(err) => break Err(err), + } + }, Err(err) => Err(err.clone()), }; if let Err(err) = &result { @@ -708,6 +715,7 @@ mod tests { let catalog = kio::clock::timeout(timeout, consumer.follow("a/pub", None).unwrap().available()) .await .expect("the catalog resolves") + .unwrap() .unwrap(); let source = Source::new(consumer, catalog); let sibling = kio::clock::timeout(timeout, source.resolve(Some(&Relative::new("./source").unwrap()), None)) diff --git a/rs/moq-net-tests/tests/announce_allocs.rs b/rs/moq-net-tests/tests/announce_allocs.rs index 77c0e0a80cb..643ff25699b 100644 --- a/rs/moq-net-tests/tests/announce_allocs.rs +++ b/rs/moq-net-tests/tests/announce_allocs.rs @@ -90,7 +90,7 @@ fn churn_allocs(root: &str, cursors: usize) -> usize { let _winner = producer.publish("room/a", peer_route(1, 1)).unwrap(); let all = Patterns::from(Pattern::all()); let mut cursors: Vec<_> = (0..cursors) - .map(|_| producer.consume().scope(root, &all).unwrap().announced()) + .map(|_| producer.consume().scope(root, &all).unwrap().routes()) .collect(); for cursor in &mut cursors { while cursor.try_next().is_some() {} @@ -281,6 +281,7 @@ async fn a_lite_request_keeps_one_copy_of_its_track_name() { .unwrap() .available() .await + .unwrap() .unwrap(); WATCH.with(|watch| watch.set(TRACK.len())); SIZED.with(|sized| sized.set((0, 0))); diff --git a/rs/moq-net-tests/tests/integration/ietf_conformance.rs b/rs/moq-net-tests/tests/integration/ietf_conformance.rs index c0ad21768a5..43360dc6ebc 100644 --- a/rs/moq-net-tests/tests/integration/ietf_conformance.rs +++ b/rs/moq-net-tests/tests/integration/ietf_conformance.rs @@ -62,6 +62,7 @@ async fn routing(version: &str, cluster: bool) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut subscription = remote.track("test").unwrap().subscribe(None).await.unwrap(); crate::support::harness::subscribed(&track).await; diff --git a/rs/moq-net-tests/tests/integration/refresh.rs b/rs/moq-net-tests/tests/integration/refresh.rs index 45659e063d7..d33f8ce6d32 100644 --- a/rs/moq-net-tests/tests/integration/refresh.rs +++ b/rs/moq-net-tests/tests/integration/refresh.rs @@ -48,6 +48,7 @@ async fn json_snapshot_refresh_crosses_a_two_relay_cluster() { .unwrap() .available() .await + .unwrap() .unwrap(); let remote = remote.track("state").unwrap(); let mut baseline = remote.subscribe(None).await.unwrap(); @@ -165,7 +166,14 @@ async fn ratio_joins_cross_two_relays() { options.client_subscribe = Some(downstream.clone()); links.push(connect_mock(options).await); } - let remote = b.consume().follow("room", None).unwrap().available().await.unwrap(); + let remote = b + .consume() + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let remote = remote.track("data").unwrap(); let mut baseline = remote.subscribe(None).await.unwrap(); assert_eq!(baseline.info().refresh, (name == "moq-lite-07-wip").then_some(1)); diff --git a/rs/moq-net-tests/tests/integration/restart.rs b/rs/moq-net-tests/tests/integration/restart.rs index 39ac6dcdd4b..d201d7558e9 100644 --- a/rs/moq-net-tests/tests/integration/restart.rs +++ b/rs/moq-net-tests/tests/integration/restart.rs @@ -44,7 +44,7 @@ async fn settle() { } /// The next announce event, failing on a hang. -async fn next_event(announced: &mut announce::Consumer) -> announce::Event { +async fn next_event(announced: &mut origin::Routes) -> origin::RouteEvent { kio::clock::timeout(Duration::from_secs(10), announced.next()) .await .expect("announce hung") @@ -53,7 +53,7 @@ async fn next_event(announced: &mut announce::Consumer) -> announce::Event { /// Expect the restart of `prefix` onto another instance, as `version` carries it: one restart /// on lite-07, an end then a start elsewhere. -async fn expect_restart(version: Version, announced: &mut announce::Consumer, prefix: &str) { +async fn expect_restart(version: Version, announced: &mut origin::Routes, prefix: &str) { expect_replaced( version, announced, @@ -65,13 +65,13 @@ async fn expect_restart(version: Version, announced: &mut announce::Consumer, pr /// Expect the restart of `prefix` as one restart when the wire replaced the instance in /// place, so the path was never absent, or as an end then a start. -async fn expect_replaced(version: Version, announced: &mut announce::Consumer, prefix: &str, restarts: bool) { +async fn expect_replaced(version: Version, announced: &mut origin::Routes, prefix: &str, restarts: bool) { match next_event(announced).await { - announce::Event::Restart(announce) if restarts => assert_eq!(announce.prefix.as_str(), prefix), - announce::Event::End(announce) if !restarts => { + origin::RouteEvent::Restart(announce) if restarts => assert_eq!(announce.prefix.as_str(), prefix), + origin::RouteEvent::End(announce) if !restarts => { assert_eq!(announce.prefix.as_str(), prefix); match next_event(announced).await { - announce::Event::Start(announce) => assert_eq!(announce.prefix.as_str(), prefix), + origin::RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), prefix), other => panic!("{version}: expected the start, got {other:?}"), } } @@ -80,7 +80,7 @@ async fn expect_replaced(version: Version, announced: &mut announce::Consumer, p } /// Nothing more arrives, past the update hold. -async fn quiet(version: Version, announced: &mut announce::Consumer) { +async fn quiet(version: Version, announced: &mut origin::Routes) { if let Ok(event) = kio::clock::timeout(Duration::from_secs(1), announced.next()).await { panic!("{version}: expected nothing more, got {event:?}"); } @@ -123,9 +123,9 @@ async fn relay_chain(version: &str) { let old = publisher.publish("live", origin::Route::default()).unwrap(); let old_track = old.create_track("video", timed()).unwrap(); - let mut announced = viewer.consume().announced(); + let mut announced = viewer.consume().routes(); match next_event(&mut announced).await { - announce::Event::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), + origin::RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), other => panic!("{version}: expected the route, got {other:?}"), } @@ -247,9 +247,9 @@ async fn prefix_pool(version: &str) { let instance = announce.route.instance.expect("a delivered route has an instance"); instance.as_str().strip_prefix("worker-").expect("a worker").to_string() }; - let mut announced = downstream.consume().announced(); + let mut announced = downstream.consume().routes(); let mut prefix = match next_event(&mut announced).await { - announce::Event::Start(announce) => advertised(announce), + origin::RouteEvent::Start(announce) => advertised(announce), other => panic!("{version}: expected the pool, got {other:?}"), }; @@ -275,7 +275,7 @@ async fn prefix_pool(version: &str) { settle().await; while let Ok(Some(event)) = kio::clock::timeout(Duration::from_secs(1), announced.next()).await { match event { - announce::Event::Restart(announce) => prefix = advertised(announce), + origin::RouteEvent::Restart(announce) => prefix = advertised(announce), other => panic!("{version}: expected a restart at most, got {other:?}"), } } @@ -360,9 +360,9 @@ async fn dynamic_instance(version: &str, from: Option, to: Instance) { let _downstream = link(version, &relay, &viewer).await; let claim = Claim::new(&publisher, from.clone()); - let mut announced = viewer.consume().announced(); + let mut announced = viewer.consume().routes(); match next_event(&mut announced).await { - announce::Event::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), + origin::RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), other => panic!("{version}: expected the claim, got {other:?}"), } @@ -374,7 +374,7 @@ async fn dynamic_instance(version: &str, from: Option, to: Instance) { claim.update_route(claim.dynamic.route().with_cost(5)); while let Ok(event) = kio::clock::timeout(Duration::from_secs(1), announced.next()).await { assert!( - matches!(event, Some(announce::Event::Update(_))), + matches!(event, Some(origin::RouteEvent::Update(_))), "{version}: a re-price delivered {event:?}" ); } @@ -464,8 +464,8 @@ async fn retired_fetch(version: &str, explicit: bool) { }; let old = publisher.publish("live", old_route).unwrap(); let old_track = old.create_track("video", timed()).unwrap(); - let mut announced = viewer.consume().announced(); - assert!(matches!(next_event(&mut announced).await, announce::Event::Start(_))); + let mut announced = viewer.consume().routes(); + assert!(matches!(next_event(&mut announced).await, origin::RouteEvent::Start(_))); let (held, mut subscription) = subscribe(&viewer, "live").await; let track = held.track("video").unwrap(); write(&old_track, "old:0"); @@ -542,8 +542,8 @@ async fn unanswered_takeover(version: &str) { let old = publisher.publish("live", origin::Route::default()).unwrap(); // Quiet, so the publisher has nothing to answer the SUBSCRIBE with yet. let old_track = old.create_track("video", timed()).unwrap(); - let mut announced = viewer.consume().announced(); - assert!(matches!(next_event(&mut announced).await, announce::Event::Start(_))); + let mut announced = viewer.consume().routes(); + assert!(matches!(next_event(&mut announced).await, origin::RouteEvent::Start(_))); let (_held, mut subscription) = subscribe(&viewer, "live").await; settle().await; assert!( @@ -601,14 +601,14 @@ async fn withdrawal_refuses_new_fetches_but_keeps_open_reads() { let _session = link(version, &publisher, &viewer).await; let source = publisher.publish("live", origin::Route::default()).unwrap(); let produced = source.create_track("video", timed()).unwrap(); - let mut announced = viewer.consume().announced(); - assert!(matches!(next_event(&mut announced).await, announce::Event::Start(_))); + let mut announced = viewer.consume().routes(); + assert!(matches!(next_event(&mut announced).await, origin::RouteEvent::Start(_))); let (held, mut subscription) = subscribe(&viewer, "live").await; let track = held.track("video").unwrap(); write(&produced, "old:0"); assert_eq!(read(&mut subscription).await, "old:0"); source.unannounce(); - assert!(matches!(next_event(&mut announced).await, announce::Event::End(_))); + assert!(matches!(next_event(&mut announced).await, origin::RouteEvent::End(_))); assert!(track.fetch_group(1, None).await.is_err(), "{version}"); write(&produced, "old:1"); assert_eq!(read(&mut subscription).await, "old:1"); @@ -623,8 +623,8 @@ async fn a_more_specific_source_cannot_fill_a_held_covering_track() { let viewer = produce_origin(2); let _session = link(version, &publisher, &viewer).await; let old = Claim::new(&publisher, None); - let mut announced = viewer.consume().announced(); - assert!(matches!(next_event(&mut announced).await, announce::Event::Start(_))); + let mut announced = viewer.consume().routes(); + assert!(matches!(next_event(&mut announced).await, origin::RouteEvent::Start(_))); let (held, mut subscription) = subscribe(&viewer, "live/job").await; let track = held.track("video").unwrap(); old.write(0, "old:0"); @@ -633,7 +633,7 @@ async fn a_more_specific_source_cannot_fill_a_held_covering_track() { let replacement_track = replacement.create_track("video", timed()).unwrap(); write(&replacement_track, "new:0"); write(&replacement_track, "new:1"); - assert!(matches!(next_event(&mut announced).await, announce::Event::Start(_))); + assert!(matches!(next_event(&mut announced).await, origin::RouteEvent::Start(_))); assert!( track.fetch_group(1, None).await.is_err(), "{version}: covering handle fetched replacement" @@ -780,9 +780,9 @@ async fn priority_update(version: &str, relayed: bool, switch: bool) { .publish("live", origin::Route::default().with_instance(a, 0)) .unwrap(); let old_track = old.create_track("video", timed()).unwrap(); - let mut announced = viewer.consume().announced(); + let mut announced = viewer.consume().routes(); assert!( - matches!(next_event(&mut announced).await, announce::Event::Start(_)), + matches!(next_event(&mut announced).await, origin::RouteEvent::Start(_)), "{case}" ); diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 79fb80906f3..5963f90782d 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -4,6 +4,35 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Announcements are a listing; `follow` reports the path going offline ([#7678](https://github.com/moq-dev/moq/pull/7678)) + +`origin::Consumer::announced()` lists paths: `announce::Event` is `Present(Announce)` or `Absent(Announce)`. +Another instance taking a path over keeps it present, and a route's cost or hops changing is not reported, so `Start`, `Update`, `End`, `Restart` and `Takeover` are gone. +Map `Start` to `Present` and `End` to `Absent`, and drop `Update` arms. +Where you requested the path again on `Restart`, hold `consumer.follow(path, None)` instead: each `available()` returns the next instance's broadcast. + +`Following::available()` returns `Result, Error>`: `None` reports the path going offline (or, when pinned, another instance taking it over), and the next `Some` is whatever serves it again. +The first call never returns `None`. After a resolution failure, such as a handler refusing the path, the next `available()` waits for the serving route to change instead of asking the same handler again, so keep the follower rather than dropping it. + +```rust +// Before +match announced.next().await { + Some(announce::Event::Start(a) | announce::Event::Restart(a)) => play(consumer.request(&a.prefix, None).await?), + _ => {} +} +let broadcast = consumer.follow(path, None)?.available().await?; +// After +if let Some(announce::Event::Present(a)) = announced.next().await { + let mut following = consumer.follow(&a.prefix, None)?; + while let Some(broadcast) = following.available().await? { + play(broadcast); + } +} +let broadcast = consumer.follow(path, None)?.available().await?.expect("the first change is an instance"); +``` + +Relays and stats that forward every route change call `consumer.routes()`, a hidden `origin::Routes` stream of `origin::RouteEvent`, which keeps the old five variants. + ### Opens wait their turn for stream credit ([#7894](https://github.com/moq-dev/moq/pull/7894)) A request no longer fails with `Error::NoStreamCredit` when the peer grants no more streams: every stream a session opens waits for credit, and returning credit goes to the open whose stream the transport would send first. The variant is removed; drop any arm matching it. diff --git a/rs/moq-net/benches/announce.rs b/rs/moq-net/benches/announce.rs index 898798cec81..ca59f4e4080 100644 --- a/rs/moq-net/benches/announce.rs +++ b/rs/moq-net/benches/announce.rs @@ -20,6 +20,7 @@ use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, use moq_net::{ Hop, Hops, Path, announce, fuzz::{AnnounceWriter, Announced, decode_announces, encode_announces}, + origin, }; // Compile the actual private replay code, as the priority bench does for its queue. @@ -27,15 +28,15 @@ use moq_net::{ mod replay; /// The previous replay, retained here as a scaling reference. -fn scanned(events: Vec) -> Vec { +fn scanned(events: Vec) -> Vec { let mut initial: Vec = Vec::new(); for event in events { let (update, active) = match event { - announce::Event::Start(update) - | announce::Event::Update(update) - | announce::Event::Restart(update) - | announce::Event::Takeover { announce: update, .. } => (update, true), - announce::Event::End(update) => (update, false), + origin::RouteEvent::Start(update) + | origin::RouteEvent::Update(update) + | origin::RouteEvent::Restart(update) + | origin::RouteEvent::Takeover { announce: update, .. } => (update, true), + origin::RouteEvent::End(update) => (update, false), }; initial.retain(|held| held.prefix != update.prefix); if active { @@ -52,7 +53,7 @@ fn initial(c: &mut Criterion) { let events: Vec<_> = (0..updates) .flat_map(|cost| { (0..routes).map(move |route| { - announce::Event::Update(announce::Announce { + origin::RouteEvent::Update(announce::Announce { prefix: moq_net::Path::new(&format!("route/{route}")).unwrap(), captures: None, route: moq_net::origin::Route::default().with_cost(cost), diff --git a/rs/moq-net/benches/auth.rs b/rs/moq-net/benches/auth.rs index bcdf18e8916..38edfbb2af7 100644 --- a/rs/moq-net/benches/auth.rs +++ b/rs/moq-net/benches/auth.rs @@ -291,14 +291,14 @@ fn waiting(c: &mut Criterion) { })); } let tokens: Vec<_> = presenting.await.into_iter().map(Result::unwrap).collect(); - let mut announced = receive.consume().announced(); + let mut announced = receive.consume().routes(); let mut seen = std::collections::HashSet::new(); while seen.len() < paths { match announced.next().await.unwrap() { - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) - | moq_net::announce::Event::Takeover { announce: update, .. } + moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Update(update) + | moq_net::origin::RouteEvent::Restart(update) + | moq_net::origin::RouteEvent::Takeover { announce: update, .. } if update.prefix.as_str().starts_with(&prefix) => { seen.insert(update.prefix); @@ -342,7 +342,7 @@ fn shared_requests(c: &mut Criterion) { let (origin, driver) = origin::Producer::new(config); harness::spawn(driver); let (client, _server, mut pending, receive) = runtime.block_on(publisher(&origin)); - let mut announced = receive.consume().announced(); + let mut announced = receive.consume().routes(); let auths = runtime.block_on(async { let mut auths = Vec::new(); for token in 0..tokens { @@ -374,8 +374,8 @@ fn shared_requests(c: &mut Criterion) { } let mut seen = std::collections::HashSet::new(); while seen.len() < tokens * requests { - if let moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Restart(update) = announced.next().await.unwrap() + if let moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Restart(update) = announced.next().await.unwrap() && update.prefix.as_str().starts_with(&prefix) { seen.insert(update.prefix); diff --git a/rs/moq-net/benches/interests.rs b/rs/moq-net/benches/interests.rs index 505aecf666e..bd88a09630a 100644 --- a/rs/moq-net/benches/interests.rs +++ b/rs/moq-net/benches/interests.rs @@ -15,7 +15,7 @@ //! Run with `cargo bench -p moq-net --features fuzz --bench interests`. use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; -use moq_net::{Hop, Pattern, Patterns, announce, fuzz, origin}; +use moq_net::{Hop, Pattern, Patterns, fuzz, origin}; /// `(watches, sessions)` shapes. const SHAPES: [(usize, usize); 4] = [(10, 10), (1_000, 10), (10, 1_000), (1_000, 1_000)]; @@ -26,16 +26,16 @@ const SHAPES: [(usize, usize); 4] = [(10, 10), (1_000, 10), (10, 1_000), (1_000, struct Table { producer: origin::Producer, _driver: origin::Driver, - _watches: Vec, + _watches: Vec, sessions: Vec, } fn table(watches: usize, sessions: usize) -> Table { let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); let mut held: Vec<_> = (0..watches) - .map(|i| scoped(&producer, &format!("other/{i}")).announced()) + .map(|i| scoped(&producer, &format!("other/{i}")).routes()) .collect(); - held.push(scoped(&producer, "room").announced()); + held.push(scoped(&producer, "room").routes()); let sessions: Vec<_> = (0..sessions).map(|_| fuzz::interests(&producer)).collect(); for session in &sessions { session.drain(); @@ -63,7 +63,7 @@ fn bench_uncovered(c: &mut Criterion) { let table = table(watches, sessions); let incoming = scoped(&table.producer, "incoming"); b.iter(|| { - drop(incoming.announced()); + drop(incoming.routes()); for session in &table.sessions { assert_eq!(session.drain(), 2); } @@ -81,7 +81,7 @@ fn bench_covered(c: &mut Criterion) { let table = table(watches, sessions); let incoming = scoped(&table.producer, "room/incoming"); b.iter(|| { - drop(incoming.announced()); + drop(incoming.routes()); }); for session in &table.sessions { assert_eq!(session.drain(), 0); diff --git a/rs/moq-net/benches/memory.rs b/rs/moq-net/benches/memory.rs index d6bea4918ee..73d84991de7 100644 --- a/rs/moq-net/benches/memory.rs +++ b/rs/moq-net/benches/memory.rs @@ -157,7 +157,13 @@ impl Link { broadcast.announce(Default::default()).unwrap(); let consumer = subscriber.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut subscribers = Vec::new(); for index in 0..streams { let track = remote.track(&format!("{index}")).unwrap(); diff --git a/rs/moq-net/benches/origin.rs b/rs/moq-net/benches/origin.rs index 3ad61f8c0f8..506f86071a0 100644 --- a/rs/moq-net/benches/origin.rs +++ b/rs/moq-net/benches/origin.rs @@ -51,7 +51,7 @@ use std::time::Duration; use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::FutureExt; -use moq_net::{Hop, Hops, Instance, Pattern, Patterns, Timestamp, announce, auth, broadcast, kio, origin, track}; +use moq_net::{Hop, Hops, Instance, Pattern, Patterns, Timestamp, auth, broadcast, kio, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; /// A millisecond track: the frames written here carry timestamps. @@ -77,7 +77,7 @@ struct Fanout { consumer: origin::Consumer, driver: origin::Driver, _publishers: Vec, - subscribers: Vec, + subscribers: Vec, } fn fanout(publishers: usize, subscribers: usize) -> Fanout { @@ -86,7 +86,7 @@ fn fanout(publishers: usize, subscribers: usize) -> Fanout { let publishers = (0..publishers) .map(|i| producer.publish(format!("room/{i}"), origin::Route::default()).unwrap()) .collect(); - let mut subscribers: Vec = (0..subscribers).map(|_| consumer.announced()).collect(); + let mut subscribers: Vec = (0..subscribers).map(|_| consumer.routes()).collect(); // Drain the replay so each iteration measures only what it adds. for cursor in &mut subscribers { while cursor.next().now_or_never().flatten().is_some() {} @@ -143,7 +143,7 @@ fn bench_announce_covering(c: &mut Criterion) { .scope("", &Patterns::from("**/*.chat".parse::().unwrap())) .unwrap(); let consumer = if covering { consumer.covering() } else { consumer }; - let mut cursors: Vec<_> = (0..readers).map(|_| consumer.announced()).collect(); + let mut cursors: Vec<_> = (0..readers).map(|_| consumer.routes()).collect(); for cursor in &mut cursors { while cursor.next().now_or_never().flatten().is_some() {} } @@ -176,7 +176,7 @@ fn bench_pending_churn(c: &mut Criterion) { let _routes: Vec<_> = (0..routes) .map(|i| producer.publish(format!("z/{i}"), origin::Route::default()).unwrap()) .collect(); - let mut cursors: Vec<_> = (0..readers).map(|_| producer.consume().announced()).collect(); + let mut cursors: Vec<_> = (0..readers).map(|_| producer.consume().routes()).collect(); let mut sequence = 0u64; b.iter(|| { sequence += 1; @@ -221,7 +221,7 @@ fn bench_withdraw(c: &mut Criterion) { .unwrap() }) .collect(); - let mut cursors: Vec<_> = (0..subscribers).map(|_| producer.consume().announced()).collect(); + let mut cursors: Vec<_> = (0..subscribers).map(|_| producer.consume().routes()).collect(); for cursor in &mut cursors { while cursor.next().now_or_never().flatten().is_some() {} } @@ -294,7 +294,7 @@ fn bench_withdraw_wave(c: &mut Criterion) { .unwrap() }) .collect(); - let mut cursors: Vec<_> = (0..subscribers).map(|_| producer.consume().announced()).collect(); + let mut cursors: Vec<_> = (0..subscribers).map(|_| producer.consume().routes()).collect(); for cursor in &mut cursors { while cursor.try_next().is_some() {} } @@ -345,7 +345,7 @@ fn bench_route_churn(c: &mut Criterion) { .unwrap() }) .collect(); - let mut cursors: Vec<_> = (0..subscribers).map(|_| producer.consume().announced()).collect(); + let mut cursors: Vec<_> = (0..subscribers).map(|_| producer.consume().routes()).collect(); for cursor in &mut cursors { while cursor.try_next().is_some() {} } @@ -386,7 +386,7 @@ fn bench_announce_mounted(c: &mut Criterion) { .unwrap() }) .collect(); - let mut cursors: Vec = (0..subscribers) + let mut cursors: Vec = (0..subscribers) .map(|project| { producer .mount(format!("p{project}/.svc"), ".svc/p0") @@ -395,7 +395,7 @@ fn bench_announce_mounted(c: &mut Criterion) { .unwrap() .consume() .with_hidden(true) - .announced() + .routes() }) .collect(); for cursor in &mut cursors { @@ -442,7 +442,7 @@ fn bench_announce_fleet(c: &mut Criterion) { let patterns: Patterns = [Pattern::subtree(&format!(".stats/p{}", peer % projects)).unwrap()] .into_iter() .collect(); - consumer.scope("", &patterns).unwrap().announced() + consumer.scope("", &patterns).unwrap().routes() }) .collect(); b.iter(|| { @@ -491,8 +491,8 @@ fn bench_announce_duplicate(c: &mut Criterion) { .unwrap() }) .collect(); - let mut cursors: Vec = (0..subscribers) - .map(|_| consumer.clone().with_hidden(true).announced()) + let mut cursors: Vec = (0..subscribers) + .map(|_| consumer.clone().with_hidden(true).routes()) .collect(); for cursor in &mut cursors { while cursor.next().now_or_never().flatten().is_some() {} @@ -554,7 +554,7 @@ fn bench_reprice_duplicate(c: &mut Criterion) { peer_route(challenger, incumbent_cost + 1), ) .unwrap(); - let mut cursors: Vec = (0..subscribers) + let mut cursors: Vec = (0..subscribers) .map(|peer| { producer .consume() @@ -564,7 +564,7 @@ fn bench_reprice_duplicate(c: &mut Criterion) { ) .unwrap() .with_hidden(true) - .announced() + .routes() }) .collect(); for cursor in &mut cursors { @@ -576,7 +576,7 @@ fn bench_reprice_duplicate(c: &mut Criterion) { for cursor in &mut cursors { // The winner switches between unnamed routes, which is another instance. let event = cursor.next().now_or_never().flatten().expect("winner changed"); - let moq_net::announce::Event::Restart(update) = event else { + let moq_net::origin::RouteEvent::Restart(update) = event else { panic!("expected a restart: got {event:?}"); }; assert_eq!(update.route.cost, moq_net::origin::Cost::new(cost.min(incumbent_cost))); @@ -666,7 +666,7 @@ fn bench_takeover(c: &mut Criterion) { (request, answer(&claim, path)) }; let _answered: Vec<_> = (0..publishers).map(|i| ask(&format!("room/{i}"))).collect(); - let mut cursors: Vec<_> = (0..subscribers).map(|_| consumer.announced()).collect(); + let mut cursors: Vec<_> = (0..subscribers).map(|_| consumer.routes()).collect(); for cursor in &mut cursors { while cursor.next().now_or_never().flatten().is_some() {} } @@ -676,7 +676,7 @@ fn bench_takeover(c: &mut Criterion) { let (request, output) = ask(&format!("room/incoming{n}")); for cursor in &mut cursors { let event = cursor.next().now_or_never().flatten(); - assert!(matches!(event, Some(announce::Event::Takeover { .. })), "{event:?}"); + assert!(matches!(event, Some(origin::RouteEvent::Takeover { .. })), "{event:?}"); } drop(request); drop(output); @@ -737,9 +737,9 @@ fn bench_subscribe(c: &mut Criterion) { group.bench_function(id, |b| { let fleet = fanout(publishers, subscribers); b.iter(|| { - let mut cursor = fleet.consumer.announced(); + let mut cursor = fleet.consumer.routes(); let mut replayed = 0; - while let Some(announce::Event::Start(_)) = cursor.next().now_or_never().flatten() { + while let Some(origin::RouteEvent::Start(_)) = cursor.next().now_or_never().flatten() { replayed += 1; } assert_eq!(replayed, publishers); @@ -844,7 +844,7 @@ struct Pool { _members: Vec, _producers: Vec, _consumers: Vec, - cursors: Vec, + cursors: Vec, } impl Pool { @@ -859,7 +859,7 @@ impl Pool { fn pool(members: usize, paths: usize, cursors: usize) -> Pool { let (producer, mut driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); let consumer = producer.consume(); - let cursors = (0..cursors).map(|_| consumer.announced()).collect(); + let cursors = (0..cursors).map(|_| consumer.routes()).collect(); let members: Vec<_> = (0..members) .map(|i| { moq_net::fuzz::requests_route( @@ -1168,6 +1168,7 @@ async fn watching(routes: usize, subscribers: usize) -> Watching { .unwrap() .available() .await + .unwrap() .unwrap(); let subscription = remote.track("video").unwrap().subscribe(None).await.unwrap(); subscriptions.push(subscription); diff --git a/rs/moq-net/benches/path.rs b/rs/moq-net/benches/path.rs index 2b8f5bd2621..26d8665e784 100644 --- a/rs/moq-net/benches/path.rs +++ b/rs/moq-net/benches/path.rs @@ -14,7 +14,7 @@ use std::sync::atomic::{AtomicU64, Ordering}; use futures::FutureExt; use moq_net::{ - Hop, Hops, Path, Pattern, Patterns, announce, + Hop, Hops, Path, Pattern, Patterns, fuzz::{Announced, Messages, decode_announces, decode_namespaces, encode_announces, encode_namespaces}, kio, origin, }; @@ -173,7 +173,7 @@ fn fanout() { let _pubs: Vec<_> = (0..100) .map(|i| producer.publish(format!("room/{i}"), origin::Route::default()).unwrap()) .collect(); - let mut cursors: Vec = (0..100).map(|_| consumer.announced()).collect(); + let mut cursors: Vec = (0..100).map(|_| consumer.routes()).collect(); for cursor in &mut cursors { while cursor.next().now_or_never().flatten().is_some() {} } diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index 61744ecb853..761f544dc61 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -554,7 +554,7 @@ pub(crate) struct Pair { pub client: moq_net::Session, pub server_transport: MockSession, pub server: Option, - pub discovery: Option, + pub discovery: Option, pub task: Option>, } @@ -710,7 +710,7 @@ impl Cluster { struct Viewer { /// Announcements watched without a subscription, one announce request each. - following: Vec, + following: Vec, subscribers: Vec, /// Datagram track consumers, in the order of the broadcasts watched. datagrams: Vec, @@ -729,9 +729,9 @@ impl Viewer { .consume() .scope(path, &moq_net::Patterns::from(moq_net::Pattern::all())) .unwrap() - .announced(); + .routes(); let event = announced.next().await.expect("announced"); - assert!(matches!(event, moq_net::announce::Event::Start(_)), "{event:?}"); + assert!(matches!(event, moq_net::origin::RouteEvent::Start(_)), "{event:?}"); self.following.push(announced); } @@ -744,6 +744,7 @@ impl Viewer { .unwrap() .available() .await + .unwrap() .unwrap(); let consumer = broadcast.datagrams(DATAGRAMS).unwrap(); consumer.info().await.unwrap(); @@ -759,6 +760,7 @@ impl Viewer { .unwrap() .available() .await + .unwrap() .unwrap(); for track in tracks { self.subscribers.push( @@ -831,6 +833,7 @@ impl Room { .unwrap() .available() .await + .unwrap() .unwrap(); viewer.fetches.push((broadcast, source.track(TRACK).unwrap())); } else { @@ -1696,6 +1699,7 @@ impl ChurnRoom { .unwrap() .available() .await + .unwrap() .unwrap(), ); viewers.push(viewer); @@ -1878,7 +1882,7 @@ pub(crate) fn withdrawal(c: &mut Criterion) { ..Shape::BASE }; let cluster = Cluster::::new("moq-lite-06", shape).await; - let mut announced = B::origin(cluster.relays.last().unwrap()).consume().announced(); + let mut announced = B::origin(cluster.relays.last().unwrap()).consume().routes(); let live: Vec<_> = (0..broadcasts) .map(|i| { B::origin(&cluster.relays[i % (relays - 1)]) @@ -1896,7 +1900,7 @@ pub(crate) fn withdrawal(c: &mut Criterion) { while let Ok(Some(event)) = kio::clock::timeout(Duration::from_secs(1), announced.next()).await { - assert!(matches!(event, moq_net::announce::Event::End(_)), "{event:?}"); + assert!(matches!(event, moq_net::origin::RouteEvent::End(_)), "{event:?}"); retracted += 1; } let elapsed = start.elapsed(); diff --git a/rs/moq-net/benches/stats.rs b/rs/moq-net/benches/stats.rs index b35f04a31a8..e04d3e28b30 100644 --- a/rs/moq-net/benches/stats.rs +++ b/rs/moq-net/benches/stats.rs @@ -6,7 +6,7 @@ use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::FutureExt; -use moq_net::{announce, fuzz, origin, stats, transport}; +use moq_net::{fuzz, origin, stats, transport}; /// `(broadcasts, tiers)` shapes; every broadcast records under every tier. const SHAPES: [(usize, usize); 4] = [(100, 1), (1_000, 1), (100, 10), (1_000, 10)]; @@ -17,7 +17,7 @@ struct Registry { registry: stats::Registry, _driver: origin::Driver, _publishers: Vec, - _cursors: Vec, + _cursors: Vec, } fn registry(broadcasts: usize, tiers: usize) -> Registry { @@ -31,7 +31,7 @@ fn registry(broadcasts: usize, tiers: usize) -> Registry { let session = registry .tier(stats::Tier::new(moq_net::Path::new(&format!("tier{t}")).unwrap())) .session(moq_net::Path::from_static("root")); - let mut cursor = producer.consume().with_stats(session).announced(); + let mut cursor = producer.consume().with_stats(session).routes(); // Each announce the cursor replays records under its tier. while cursor.next().now_or_never().flatten().is_some() {} cursor diff --git a/rs/moq-net/benches/synced.rs b/rs/moq-net/benches/synced.rs index 1716a716c37..e30640b2fa2 100644 --- a/rs/moq-net/benches/synced.rs +++ b/rs/moq-net/benches/synced.rs @@ -14,7 +14,7 @@ //! Run with `cargo bench -p moq-net --features fuzz --bench synced`. use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; -use moq_net::{Hop, Pattern, Patterns, announce, fuzz, origin}; +use moq_net::{Hop, Pattern, Patterns, fuzz, origin}; /// `(streams, consumers)` shapes. const SHAPES: [(usize, usize); 4] = [(10, 10), (1_000, 10), (10, 1_000), (1_000, 1_000)]; @@ -25,7 +25,7 @@ struct Table { producer: origin::Producer, _driver: origin::Driver, _streams: Vec, - _consumers: Vec, + _consumers: Vec, } fn table(streams: usize, consumers: usize) -> Table { @@ -34,7 +34,7 @@ fn table(streams: usize, consumers: usize) -> Table { .map(|i| fuzz::snapshot(&producer, &format!("room/{i}"))) .collect(); let consumers = (0..consumers) - .map(|i| scoped(&producer, &format!("other/{i}")).announced()) + .map(|i| scoped(&producer, &format!("other/{i}")).routes()) .collect(); Table { producer, @@ -57,7 +57,7 @@ fn bench_land(c: &mut Criterion) { let id = BenchmarkId::from_parameter(format!("{streams}st_{consumers}c")); group.bench_function(id, |b| { let table = table(streams, consumers); - let watcher = scoped(&table.producer, "room/incoming").announced(); + let watcher = scoped(&table.producer, "room/incoming").routes(); b.iter(|| { let snapshot = fuzz::snapshot(&table.producer, "room/incoming"); assert!(!watcher.poll_synced(&moq_net::kio::Waiter::noop()).is_ready()); @@ -78,7 +78,7 @@ fn bench_register(c: &mut Criterion) { let incoming = scoped(&table.producer, "room/incoming"); let _held = fuzz::snapshot(&table.producer, "room/incoming"); b.iter(|| { - let watcher = incoming.announced(); + let watcher = incoming.routes(); assert!(!watcher.poll_synced(&moq_net::kio::Waiter::noop()).is_ready()); }); }); diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index 9039c685aaa..4c5744cdf03 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -2711,7 +2711,7 @@ impl Enforce { pub(crate) fn poll( &mut self, handle: &Link, - announced: &mut crate::announce::Consumer, + announced: &mut crate::origin::Routes, waiter: &kio::Waiter, ) -> Poll> { if !self.setup { @@ -2733,7 +2733,7 @@ impl Enforce { return Poll::Ready(None); }; match event { - crate::announce::Event::Start(update) | crate::announce::Event::Update(update) + crate::origin::RouteEvent::Start(update) | crate::origin::RouteEvent::Update(update) if !self.live.contains(&update.prefix) => { if self.pending.iter().any(|gate| gate.path == update.prefix) { @@ -2749,7 +2749,7 @@ impl Enforce { } self.live.insert(gate.path); } - crate::announce::Event::End(update) => { + crate::origin::RouteEvent::End(update) => { self.live.remove(&update.prefix); self.pending.retain(|gate| gate.path != update.prefix); } diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 4734539e11e..9fa773e6548 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1860,7 +1860,7 @@ mod tests { let tier = crate::stats::Tier::new(crate::Path::from_static("t")); let session = registry.tier(tier.clone()).session(crate::Path::from_static("root")); let _broadcast = origin.publish("room/1", crate::origin::Route::default()).unwrap(); - let mut cursor = origin.consume().with_stats(session).announced(); + let mut cursor = origin.consume().with_stats(session).routes(); while cursor.next().now_or_never().flatten().is_some() {} bump_publisher_bytes(®istry, crate::Path::from_static("room/1"), &tier, 5); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index de3f3987f99..10d1d790f23 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -4136,7 +4136,7 @@ impl Publisher { .discovery(!self.peer_setup.get().await.hidden || self.origin.includes_hidden()); let ns = Namespaces::new(peer, Target::Requests); - self.run_namespaces(origin.announced(), crate::Path::empty(), ns, Vec::new()) + self.run_namespaces(origin.routes(), crate::Path::empty(), ns, Vec::new()) .await } @@ -4230,7 +4230,7 @@ impl Publisher { // A NAMESPACE is discovery, not a route, so a peer that also hears the unsolicited // PUBLISH_NAMESPACE still gets the match here. - let mut announced = origin.announced(); + let mut announced = origin.routes(); // MoQ Active Count: take what is advertised now, so the OK can say how many // NAMESPACE messages carry it. They go out first, ahead of any change, so with @@ -4287,17 +4287,17 @@ impl Publisher { /// /// A route announced and retracted within the snapshot is left out, and a repeat /// keeps its latest metadata, so each path appears once. - fn snapshot(announced: &mut crate::announce::Consumer) -> Vec { + fn snapshot(announced: &mut crate::origin::Routes) -> Vec { let mut initial = std::collections::BTreeMap::new(); while let Some(event) = announced.try_next() { match event { - crate::announce::Event::Start(update) - | crate::announce::Event::Update(update) - | crate::announce::Event::Restart(update) - | crate::announce::Event::Takeover { announce: update, .. } => { + crate::origin::RouteEvent::Start(update) + | crate::origin::RouteEvent::Update(update) + | crate::origin::RouteEvent::Restart(update) + | crate::origin::RouteEvent::Takeover { announce: update, .. } => { initial.insert(update.prefix.clone(), update); } - crate::announce::Event::End(update) => { + crate::origin::RouteEvent::End(update) => { initial.remove(&update.prefix); } } @@ -4312,7 +4312,7 @@ impl Publisher { /// for a subset). The token that admitted the request, if one did, guards the loop. async fn run_namespaces( &self, - announced: crate::announce::Consumer, + announced: crate::origin::Routes, prefix: crate::Path, mut ns: Namespaces, initial: Vec, @@ -4332,7 +4332,7 @@ impl Publisher { /// [`Self::run_namespaces`]'s loop, leaving a token still being verified in `ns`. async fn forward_namespaces( &self, - mut announced: crate::announce::Consumer, + mut announced: crate::origin::Routes, mut prefix: crate::Path, ns: &mut Namespaces, initial: Vec, @@ -4454,16 +4454,16 @@ impl Publisher { } if let Poll::Ready(next) = announced.poll_next(waiter) { return Poll::Ready(NamespaceEvent::Update(next.map(|event| match event { - crate::announce::Event::Start(update) | crate::announce::Event::Update(update) => { + crate::origin::RouteEvent::Start(update) | crate::origin::RouteEvent::Update(update) => { (update, true, false) } - crate::announce::Event::Restart(update) => (update, true, true), + crate::origin::RouteEvent::Restart(update) => (update, true, true), // moq-transport has no takeover: another instance, in place or not. - crate::announce::Event::Takeover { announce, predecessor } => { + crate::origin::RouteEvent::Takeover { announce, predecessor } => { let restart = predecessor == announce.prefix; (announce, true, restart) } - crate::announce::Event::End(update) => (update, false, false), + crate::origin::RouteEvent::End(update) => (update, false, false), }))); } if retry.poll(waiter).is_ready() { @@ -4648,7 +4648,7 @@ impl Publisher { &self, ns: &mut Namespaces, prefix: &mut crate::Path, - announced: &mut crate::announce::Consumer, + announced: &mut crate::origin::Routes, next: Option, ) -> Result { let moved = match next.filter(|next| next != prefix) { @@ -4674,7 +4674,7 @@ impl Publisher { .origin .scope(&*prefix, &scope) .unwrap_or_else(|_| self.origin.empty()); - *announced = self.advertising(origin, &ns.peer).discovery(ns.hidden).announced(); + *announced = self.advertising(origin, &ns.peer).discovery(ns.hidden).routes(); true } None => false, @@ -15722,7 +15722,7 @@ mod tests { let peer = cluster::Peer::default(); // The cursor is what filters: an excluded consumer never sees the echoed route. - let mut announced = consumer.excluding(assigned).announced(); + let mut announced = consumer.excluding(assigned).routes(); let local = announced.assert_next_active("from/us"); announced.assert_next_wait(); @@ -15762,7 +15762,7 @@ mod tests { hop: Some(r1), cost: None, }; - let mut announced = consumer.excluding(publisher.exclude(&peer)).announced(); + let mut announced = consumer.excluding(publisher.exclude(&peer)).routes(); announced.assert_next_wait(); let forwarded = cluster::Advert::forward(&hops, 0, crate::Hop::new(1).unwrap()).unwrap(); @@ -15834,7 +15834,7 @@ mod tests { hop: Some(crate::Hop::UNKNOWN), cost: None, }; - let mut announced = consumer.excluding(publisher.exclude(&peer)).announced(); + let mut announced = consumer.excluding(publisher.exclude(&peer)).routes(); announced.assert_next_wait(); } diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index d51bd80fb7c..9f0b120df75 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -1362,7 +1362,7 @@ impl EnforceGrant { fn poll(&mut self, waiter: &kio::Waiter) -> std::task::Poll> { let origin: origin::Consumer = wait!(Origin => self.publisher.poll_serving_origin(waiter)); // Checking what we publish is not a local reader asking to discover anything. - let mut announced: crate::announce::Consumer = origin.clone().serving().announced(); + let mut announced: crate::origin::Routes = origin.clone().serving().routes(); let mut check: crate::auth::Enforce = crate::auth::Enforce::default(); let path = wait!(Check => check.poll(&self.auth, announced, waiter)); let Some(path) = path else { @@ -1406,7 +1406,7 @@ mod tests { async fn announce_interest_waits_for_credit() { for version in [Version::Draft18] { let origin = origin::Config::new(crate::Hop::new(1).unwrap()).produce(); - let _interest = origin.consume().announced(); + let _interest = origin.consume().routes(); let scripted = crate::lite::test_transport::ScriptedSession::new(Vec::new()).without_credit(); let log = scripted.log.clone(); let session = crate::driver::Transport::new(scripted.clone()); @@ -1804,7 +1804,7 @@ mod tests { let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); // A client asks only about what is watched. - let _watch = origin.consume().announced(); + let _watch = origin.consume().routes(); let session = crate::lite::test_transport::ScriptedSession::new(namespace_without_hop_path(VERSION).await); let log = session.log.clone(); @@ -1868,7 +1868,7 @@ mod tests { let scoped = origin .scope("rootns", &scope) .expect("scope the origin to two prefixes"); - let _watch = scoped.consume().announced(); + let _watch = scoped.consume().routes(); let gate = kio::Producer::new(true); let session = crate::lite::test_transport::SinkSession::gated_bi(gate.consume()); @@ -1932,12 +1932,12 @@ mod tests { } /// Watch everything under `prefix`. - fn watch(origin: &crate::origin::Producer, prefix: &str) -> crate::announce::Consumer { + fn watch(origin: &crate::origin::Producer, prefix: &str) -> crate::origin::Routes { origin .consume() .scope(prefix, &crate::Patterns::from(crate::Pattern::all())) .unwrap() - .announced() + .routes() } /// Start a client feeding `subscribe` against a peer that answers nothing and declared diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 5f3c4298d5a..12b19e6d989 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -7148,7 +7148,7 @@ mod tests { let session = crate::lite::test_transport::ScriptedSession::new(counted_response(VERSION, Some(2), entries).await); let (mut subscriber, consumer, snapshot) = counted_subscriber(VERSION, true, session.clone()); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let (_live, live) = live(); let stream = Stream::open(&crate::driver::Transport::new(session.clone()), VERSION) .await @@ -7176,7 +7176,7 @@ mod tests { const VERSION: Version = Version::Draft18; let session = crate::lite::test_transport::ScriptedSession::new(counted_response(VERSION, None, &[]).await); let (mut subscriber, consumer, snapshot) = counted_subscriber(VERSION, false, session.clone()); - let cursor = consumer.announced(); + let cursor = consumer.routes(); assert!(!cursor.is_synced()); let (_live, live) = live(); let stream = Stream::open(&crate::driver::Transport::new(session.clone()), VERSION) @@ -8907,13 +8907,13 @@ mod tests { .start_announce(crate::Path::from_static("room/host"), advert, Admit::Session(None)) .unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let route = announced.expect_next_active("room/host").await; let hops: Vec<_> = route.hops.iter().copied().collect(); assert_eq!(hops, vec![crate::Hop::UNKNOWN]); assert!(route.is_anonymous()); - let mut hidden = consumer.excluding(assigned).announced(); + let mut hidden = consumer.excluding(assigned).routes(); hidden.assert_next_wait(); } @@ -8931,12 +8931,12 @@ mod tests { let origin = crate::origin::Config::new(self_origin).produce(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); // The publish direction: an origin handle scoped to the peer, which is what // `Client::with_peer_hop` hands the publisher. Holding its announce stream // is what records that the peer has been offered these paths. - let mut publishing = consumer.clone().excluding(peer).announced(); + let mut publishing = consumer.clone().excluding(peer).routes(); // What we are publishing to the peer: a real upstream route. let upstream = crate::Hops::try_from(vec![crate::Hop::new(7).unwrap()]).unwrap(); @@ -8984,7 +8984,7 @@ mod tests { let origin = crate::origin::Config::new(self_origin).produce(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let connect = || { let (tasks, task_set) = crate::util::TaskSet::new(); @@ -9021,7 +9021,7 @@ mod tests { announced.assert_next_wait(); // Neither route is offered back to the peer they both came from. - consumer.clone().excluding(peer).announced().assert_next_wait(); + consumer.clone().excluding(peer).routes().assert_next_wait(); // The stale session finally retracting leaves the fresh route standing. drop(first); @@ -9057,12 +9057,12 @@ mod tests { /// The current best route covering `path`, if any (synchronous peek). fn routed_now(consumer: &crate::origin::Consumer, path: &str) -> Option { use futures::FutureExt; - match consumer.routes(path).ok()?.next().now_or_never()?? { - crate::announce::Event::End(_) => None, - crate::announce::Event::Start(announce) - | crate::announce::Event::Update(announce) - | crate::announce::Event::Restart(announce) - | crate::announce::Event::Takeover { announce, .. } => Some(announce.route), + match consumer.path_routes(path).ok()?.next().now_or_never()?? { + crate::origin::RouteEvent::End(_) => None, + crate::origin::RouteEvent::Start(announce) + | crate::origin::RouteEvent::Update(announce) + | crate::origin::RouteEvent::Restart(announce) + | crate::origin::RouteEvent::Takeover { announce, .. } => Some(announce.route), } } @@ -10977,7 +10977,7 @@ mod tests { assert!(futures::poll!(run.as_mut()).is_pending(), "the announce ended early"); let _ = task_set.poll(&kio::Waiter::noop()); if let Poll::Ready(routed) = futures::poll!(routing.as_mut()) { - broadcast = Some(routed.expect("the announce routes the broadcast")); + broadcast = routed.expect("the announce routes the broadcast"); break; } settle().await; diff --git a/rs/moq-net/src/lite/ietf.rs b/rs/moq-net/src/lite/ietf.rs index b6ca86f1d6e..967c96f239b 100644 --- a/rs/moq-net/src/lite/ietf.rs +++ b/rs/moq-net/src/lite/ietf.rs @@ -766,7 +766,7 @@ pub(crate) async fn test_hop( ); downstream .consume() - .routes(broadcast) + .path_routes(broadcast) .unwrap() .next() .await diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 426bf81a649..2a4fbdfcb07 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1,7 +1,7 @@ use crate::driver::Transport; use crate::transport::poll::Session as _; -use crate::{SessionError, announce, datagram, frame, group, origin, track}; +use crate::{SessionError, datagram, frame, group, origin, track}; use rangemap::{RangeMap, RangeSet}; use std::{ borrow::Cow, @@ -326,7 +326,7 @@ impl Publisher { async fn run_announce( stream: &mut Stream, origin: &origin::Consumer, - announced: &mut announce::Consumer, + announced: &mut origin::Routes, self_origin: Hop, version: Version, auth: Option, @@ -674,7 +674,7 @@ impl AuthServe { #[derive(Default)] struct Enforce { check: crate::auth::Enforce, - announced: Option, + announced: Option, } impl Enforce { @@ -683,7 +683,7 @@ impl Enforce { Some(announced) => announced, None => { let origin = ready!(shared.poll_serving_origin(waiter)); - self.announced.insert(origin.announced()) + self.announced.insert(origin.routes()) } }; let Some(path) = ready!(self.check.poll(&shared.auth, announced, waiter)) else { @@ -966,7 +966,7 @@ impl AnnounceServe { Some(hop) => Poll::Ready(*hop), None => self.shared.peer_setup.poll_hop(waiter).map(|hop| hop.unwrap_or(*assigned)), }); - let (origin, announced, run): (origin::Consumer, announce::Consumer, AnnounceRun) = + let (origin, announced, run): (origin::Consumer, origin::Routes, AnnounceRun) = self.start(prefix, exclude_hop, hidden); // Streaming announce updates. @@ -1001,7 +1001,7 @@ impl AnnounceServe { prefix: crate::Path, exclude_hop: Hop, hidden: bool, - ) -> (origin::Consumer, announce::Consumer, AnnounceRun) { + ) -> (origin::Consumer, origin::Routes, AnnounceRun) { // If the requested prefix is outside our scope (an empty origin, or a token // that doesn't grant it), we simply have nothing to announce. Respond with an // empty set and keep the stream open (the subscriber treats a FIN here as a @@ -1035,7 +1035,7 @@ impl AnnounceServe { true => origin.with_hidden(true), false => origin, }; - let announced = origin.announced(); + let announced = origin.routes(); let mut run = AnnounceRun::new(prefix, self.shared.self_origin, self.shared.version); run.auth = Some(crate::auth::Gate::new( self.shared.auth.clone(), @@ -1169,17 +1169,17 @@ impl AnnounceRun { // state from a fresh one and start what the grant now covers. The cursor itself stays: // whatever it has pending for the routes the peer holds (an update, a restart, an end // still waiting out its hold) arrives through the loop as usual. - let mut snapshot = origin.announced(); + let mut snapshot = origin.routes(); let mut current: HashMap = HashMap::new(); while let Some(event) = snapshot.try_next() { match event { - announce::Event::Start(update) - | announce::Event::Update(update) - | announce::Event::Restart(update) - | announce::Event::Takeover { announce: update, .. } => { + origin::RouteEvent::Start(update) + | origin::RouteEvent::Update(update) + | origin::RouteEvent::Restart(update) + | origin::RouteEvent::Takeover { announce: update, .. } => { current.insert(update.prefix, update.route); } - announce::Event::End(update) => { + origin::RouteEvent::End(update) => { current.remove(&update.prefix); } } @@ -1423,7 +1423,7 @@ impl AnnounceRun { &mut self, stream: &mut Stream, origin: &origin::Consumer, - announced: &mut announce::Consumer, + announced: &mut origin::Routes, ) -> Result<(), Error> { if !matches!(self.version, Version::Lite01 | Version::Lite02) && !self.version.has_announce_ok() { return Ok(()); @@ -1485,7 +1485,7 @@ impl AnnounceRun { &mut self, stream: &mut Stream, origin: &origin::Consumer, - announced: &mut announce::Consumer, + announced: &mut origin::Routes, ) -> Result<(), Error> { match self.phase { AnnouncePhase::Withdrawing | AnnouncePhase::Closing => return Ok(()), @@ -1508,7 +1508,7 @@ impl AnnounceRun { &mut self, stream: &mut Stream, origin: &origin::Consumer, - announced: &mut announce::Consumer, + announced: &mut origin::Routes, waiter: &kio::Waiter, ) -> Poll> { if matches!(self.phase, AnnouncePhase::Init) { @@ -1575,10 +1575,14 @@ impl AnnounceRun { }; let (update, active, restart, predecessor) = match next { - Some(announce::Event::Start(update) | announce::Event::Update(update)) => (update, true, false, None), - Some(announce::Event::Restart(update)) => (update, true, true, None), - Some(announce::Event::Takeover { announce, predecessor }) => (announce, true, false, Some(predecessor)), - Some(announce::Event::End(update)) => (update, false, false, None), + Some(origin::RouteEvent::Start(update) | origin::RouteEvent::Update(update)) => { + (update, true, false, None) + } + Some(origin::RouteEvent::Restart(update)) => (update, true, true, None), + Some(origin::RouteEvent::Takeover { announce, predecessor }) => { + (announce, true, false, Some(predecessor)) + } + Some(origin::RouteEvent::End(update)) => (update, false, false, None), None => { if self.initial.take().is_some() { stream.writer.buffer(&lite::AnnounceBroadcast::Synced)?; @@ -3458,20 +3462,20 @@ mod announce_test { #[test] fn initial_replay_keeps_the_last_event_and_its_order() { - let update = |prefix: &'static str, cost| announce::Announce { + let update = |prefix: &'static str, cost| crate::announce::Announce { prefix: crate::Path::try_from(prefix).unwrap(), captures: None, route: origin::Route::default().with_cost(cost), }; let events = [ - announce::Event::Start(update("a", 1)), - announce::Event::Start(update("b", 2)), - announce::Event::Update(update("a", 3)), - announce::Event::Start(update("c", 4)), - announce::Event::End(update("b", 2)), - announce::Event::Restart(update("c", 5)), - announce::Event::End(update("c", 5)), - announce::Event::Start(update("b", 6)), + origin::RouteEvent::Start(update("a", 1)), + origin::RouteEvent::Start(update("b", 2)), + origin::RouteEvent::Update(update("a", 3)), + origin::RouteEvent::Start(update("c", 4)), + origin::RouteEvent::End(update("b", 2)), + origin::RouteEvent::Restart(update("c", 5)), + origin::RouteEvent::End(update("c", 5)), + origin::RouteEvent::Start(update("b", 6)), ]; let initial: Vec<_> = super::super::replay::initial(events) .into_iter() @@ -3492,7 +3496,7 @@ mod announce_test { let removed = origin.announce("z/removed", origin::Route::default()).unwrap(); let _initial = origin.announce("z/retained", origin::Route::default()).unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let budget = kio::Producer::new(0usize); let log = Log::default(); let mut wire = Wire { @@ -3534,7 +3538,7 @@ mod announce_test { let _a = origin.announce("a", origin::Route::default()).unwrap(); let _b = origin.announce("b", origin::Route::default()).unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let budget = kio::Producer::new(1usize); let log = Log::default(); let mut wire = Wire { @@ -3576,7 +3580,7 @@ mod announce_test { let _a = origin.announce("a", origin::Route::default()).unwrap(); let _b = origin.announce("b", origin::Route::default()).unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let budget = kio::Producer::new(0usize); let log = Log::default(); let mut wire = Wire { @@ -3632,7 +3636,7 @@ mod announce_test { let first = origin.announce("a", origin::Route::default()).unwrap(); let unseen = origin.announce("b", origin::Route::default()).unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let budget = kio::Producer::new(1usize); let log = Log::default(); let mut wire = Wire { @@ -3672,7 +3676,7 @@ mod announce_test { let origin = Hop::new(1).unwrap().produce(); let _upstream = origin.snapshot(""); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); assert!(!announced.is_synced()); let log = Log::default(); let mut wire = Wire { @@ -3792,13 +3796,13 @@ mod announce_test { let log = Log::default(); let writes = log.writes.clone(); let consumer = origin.consume(); - assert!(!consumer.announced().is_synced(), "the relay itself is still syncing"); + assert!(!consumer.routes().is_synced(), "the relay itself is still syncing"); let mut stream = Stream:: { writer: Writer::new(crate::transport::boxed::SendStream::new(SinkSend::new(log)), VERSION), reader: Reader::new(crate::transport::boxed::RecvStream::new(PendingRecv), VERSION), }; let _task = kio::clock::sim::spawn(async move { - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let self_origin = consumer.hop(); TestPublisher::run_announce(&mut stream, &consumer, &mut announced, self_origin, VERSION, None).await }); @@ -3831,7 +3835,7 @@ mod announce_test { reader: Reader::new(crate::transport::boxed::RecvStream::new(PendingRecv), version), }; let task = kio::clock::sim::spawn(async move { - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let self_origin = consumer.hop(); TestPublisher::run_announce(&mut stream, &consumer, &mut announced, self_origin, version, auth).await }); @@ -4088,7 +4092,7 @@ mod announce_test { reader: Reader::new(crate::transport::boxed::RecvStream::new(PendingRecv), VERSION), }; let task = kio::clock::sim::spawn(async move { - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let self_origin = consumer.hop(); TestPublisher::run_announce(&mut stream, &consumer, &mut announced, self_origin, VERSION, None).await }); @@ -7753,7 +7757,14 @@ mod serve_group_test { broadcast.announce(Default::default()).unwrap(); let relay = origin(2); let _upstream = connect(version, &publisher, &relay).await; - let remote = relay.consume().follow("room", None).unwrap().available().await.unwrap(); + let remote = relay + .consume() + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut keeper = match warm { true => Some( remote @@ -7784,6 +7795,7 @@ mod serve_group_test { .unwrap() .available() .await + .unwrap() .unwrap(); let mut live = remote .track("test") @@ -8607,7 +8619,7 @@ mod tests { .unwrap(); let consumer = origin.consume().excluding(assigned); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let mut run = std::pin::pin!(Publisher::run_announce( &mut stream, &consumer, @@ -8652,7 +8664,7 @@ mod tests { .await .unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let err = Publisher::run_announce( &mut stream, &consumer, @@ -8681,7 +8693,7 @@ mod tests { .await .unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let mut run = std::pin::pin!(Publisher::run_announce( &mut stream, &consumer, @@ -8717,7 +8729,7 @@ mod tests { .await .unwrap(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let mut run = AnnounceRun::new(crate::Path::default(), self_origin, Version::Lite01); run.withdraw(&mut stream, &consumer, &mut announced).unwrap(); diff --git a/rs/moq-net/src/lite/replay.rs b/rs/moq-net/src/lite/replay.rs index cef480f5663..1d5549d6689 100644 --- a/rs/moq-net/src/lite/replay.rs +++ b/rs/moq-net/src/lite/replay.rs @@ -2,19 +2,19 @@ use std::collections::HashMap; -use crate::announce; +use crate::{announce, origin}; /// Keep each prefix's last active event in its original event order. -pub(super) fn initial(events: impl IntoIterator) -> Vec { +pub(super) fn initial(events: impl IntoIterator) -> Vec { let mut pending: Vec> = Vec::new(); let mut positions = HashMap::new(); for event in events { let (update, active) = match event { - announce::Event::Start(update) - | announce::Event::Update(update) - | announce::Event::Restart(update) - | announce::Event::Takeover { announce: update, .. } => (update, true), - announce::Event::End(update) => (update, false), + origin::RouteEvent::Start(update) + | origin::RouteEvent::Update(update) + | origin::RouteEvent::Restart(update) + | origin::RouteEvent::Takeover { announce: update, .. } => (update, true), + origin::RouteEvent::End(update) => (update, false), }; if let Some(index) = positions.remove(&update.prefix) { pending[index] = None; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 58f76ed899d..c1641266fc5 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -5253,7 +5253,7 @@ mod tests { async fn announce_interest_waits_for_credit() { for version in [Version::Lite04, Version::Lite07] { let origin = origin::Config::new(crate::Hop::new(1).unwrap()).produce(); - let _interest = origin.consume().announced(); + let _interest = origin.consume().routes(); let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()).without_credit(); let log = session.log.clone(); let subscriber = Subscriber::new(SubscriberConfig { @@ -7251,13 +7251,13 @@ mod tests { assert!(accepted); // The route is announced synchronously: hop 0 on the wire, assigned id local. - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let route = cursor.expect_next_active("room/host").await; let hops: Vec<_> = route.hops.iter().copied().collect(); assert_eq!(hops, vec![crate::Hop::UNKNOWN]); assert!(route.is_anonymous()); - let mut hidden = consumer.excluding(assigned).announced(); + let mut hidden = consumer.excluding(assigned).routes(); hidden.assert_next_wait(); } @@ -7297,7 +7297,7 @@ mod tests { .unwrap() ); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let route = cursor.expect_next_active("room/host").await; let hops: Vec<_> = route.hops.iter().copied().collect(); assert_eq!(hops, vec![crate::Hop::UNKNOWN, crate::Hop::UNKNOWN]); @@ -7324,7 +7324,7 @@ mod tests { ) .unwrap(); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); kio::clock::sim::yield_now().await; let route = cursor.expect_next_active("room/host").await; let hops: Vec<_> = route.hops.iter().copied().collect(); @@ -7432,7 +7432,7 @@ mod tests { ) .unwrap(); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); kio::clock::sim::yield_now().await; cursor.expect_next_active("room/host").await; @@ -7472,7 +7472,7 @@ mod tests { &mut announced, ) .unwrap(); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); cursor.expect_next_active("room/host").await; auth.handle().authorize(&crate::auth::Grant { @@ -7533,7 +7533,7 @@ mod tests { &mut announced, ) .unwrap(); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); cursor.expect_next_active("room/host").await; // The stream ends without retracting anything: the map dies with it and the @@ -7608,7 +7608,7 @@ mod tests { Path::from_static(""), subscriber.origin.snapshot(""), ); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); @@ -7697,7 +7697,7 @@ mod tests { } script.extend(announce_script(VERSION, &[&announce_start(VERSION, "a")])); let (mut prefix, consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(script)); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let mut polls = 0; kio::wait(|waiter| { @@ -7726,7 +7726,7 @@ mod tests { ], ); let (mut prefix, consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(script)); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); assert!(!cursor.is_synced(), "held from the request on"); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); @@ -7737,7 +7737,7 @@ mod tests { cursor.assert_next_active("b"); assert!(cursor.is_synced()); // A consumer created after the boundary syncs on its replay. - let mut late = consumer.announced(); + let mut late = consumer.routes(); late.assert_next_active("a"); late.assert_next_active("b"); assert!(late.is_synced()); @@ -7749,7 +7749,7 @@ mod tests { const VERSION: Version = Version::Lite06; let script = announce_script(VERSION, &[&announce_ok(VERSION, 2), &announce_start(VERSION, "a")]); let (mut prefix, consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(script)); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); @@ -7764,7 +7764,7 @@ mod tests { let synced = |w: &mut crate::coding::Encoder<'_>| lite::AnnounceBroadcast::Synced.encode(w, VERSION).unwrap(); let script = announce_script(VERSION, &[&announce_ok(VERSION, 0), &synced]); let (mut prefix, consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(script)); - let cursor = consumer.announced(); + let cursor = consumer.routes(); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); assert!(cursor.is_synced()); @@ -7801,7 +7801,7 @@ mod tests { let session = crate::lite::test_transport::ScriptedSession::new(script); let log = session.log.clone(); let (mut prefix, consumer) = root_prefix(version, session.clone()); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); assert!(prefix.poll(&kio::Waiter::noop()).is_pending()); assert!(log.closes().is_empty()); session.push(&[last]); @@ -7854,7 +7854,7 @@ mod tests { const VERSION: Version = Version::Lite04; let (mut prefix, consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(Vec::new())); - let cursor = consumer.announced(); + let cursor = consumer.routes(); assert!(!cursor.is_synced()); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); @@ -7876,7 +7876,7 @@ mod tests { VERSION, crate::lite::test_transport::ScriptedSession::new(announce_script(VERSION, &[&init])), ); - let mut cursor = consumer.announced(); + let mut cursor = consumer.routes(); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); assert!(!cursor.is_synced()); diff --git a/rs/moq-net/src/model/announced.rs b/rs/moq-net/src/model/announced.rs new file mode 100644 index 00000000000..a01fb82a1f4 --- /dev/null +++ b/rs/moq-net/src/model/announced.rs @@ -0,0 +1,187 @@ +//! The paths announced under a scope, each present or absent. + +use std::task::Poll; + +use super::origin_impl::{Announce, RouteEvent, Routes}; +use crate::{Error, Path}; + +/// What an announcement listing reports for a path. +#[derive(Clone, Debug)] +pub enum AnnounceEvent { + /// A route now covers the path, which was absent. + Present(Announce), + /// No route covers the path any more. Carries its last advertised route. + Absent(Announce), +} + +/// The paths announced in a scope, each reported as it becomes present or absent. +/// +/// A path that another instance takes over stays present, and a route's metadata +/// changing is not reported: hold a [`follow`](crate::origin::Consumer::follow) to +/// see the broadcast change. Created by +/// [`Consumer::announced`](crate::origin::Consumer::announced). Drop to unregister. +pub struct AnnounceConsumer { + routes: Routes, +} + +impl AnnounceConsumer { + pub(super) fn new(routes: Routes) -> Self { + Self { routes } + } + + /// What a route change means for the listing, if anything. + fn list(event: RouteEvent) -> Option { + match event { + RouteEvent::Start(announce) => Some(AnnounceEvent::Present(announce)), + RouteEvent::End(announce) => Some(AnnounceEvent::Absent(announce)), + // A takeover elsewhere starts its own prefix; at its predecessor's it replaces it. + RouteEvent::Takeover { announce, predecessor } if predecessor != announce.prefix => { + Some(AnnounceEvent::Present(announce)) + } + RouteEvent::Update(_) | RouteEvent::Restart(_) | RouteEvent::Takeover { .. } => None, + } + } + + /// Returns the next path that became present or absent, relative to this listing's root. + /// + /// Returns None once the origin closes. The consumer is also a [`futures::Stream`] + /// of the same events. + #[cfg(feature = "async")] + pub async fn next(&mut self) -> Option { + kio::wait(|waiter| self.poll_next(waiter)).await + } + + /// Poll for the next event, registering `waiter` when there is none yet. + /// + /// Returns `Poll::Ready(None)` once the origin closes. + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { + loop { + let Some(event) = std::task::ready!(self.routes.poll_next(waiter)) else { + return Poll::Ready(None); + }; + if let Some(event) = Self::list(event) { + return Poll::Ready(Some(event)); + } + } + } + + /// Returns the next event without blocking. + /// + /// Returns None if there is no event available; NOT because the listing is closed. + /// Use [`Self::is_closed`] to check if the listing is closed. + pub fn try_next(&mut self) -> Option { + loop { + if let Some(event) = Self::list(self.routes.try_next()?) { + return Some(event); + } + } + } + + /// Returns true if the listing is closed (no more events will arrive). + pub fn is_closed(&self) -> bool { + self.routes.is_closed() + } + + /// Wait until this listing has synced with what its sources announced. + /// + /// Synced means every session feeding this scope has delivered its peer's + /// announcements as of when it answered, and this listing has read every event + /// queued before then. A path not present by then is absent, rather than not heard + /// yet. A session joining the scope later, such as a reconnect, takes this back + /// until it syncs too; routes published on this origin itself never wait. + /// + /// Level-triggered: ready whenever synced, and once the listing is closed. + pub fn poll_synced(&self, waiter: &kio::Waiter) -> Poll<()> { + self.routes.poll_synced(waiter) + } + + /// Returns the root that is automatically stripped from reported paths. + pub fn root(&self) -> &Path { + self.routes.root() + } + + /// Converts a reported path back to one rooted at the origin. + pub fn absolute(&self, prefix: impl TryInto>) -> Result { + self.routes.absolute(prefix) + } +} + +#[cfg(feature = "async")] +impl futures::Stream for AnnounceConsumer { + type Item = AnnounceEvent; + + fn poll_next(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll> { + let this = self.get_mut(); + loop { + let Some(event) = std::task::ready!(futures::Stream::poll_next(std::pin::Pin::new(&mut this.routes), cx)) + else { + return Poll::Ready(None); + }; + if let Some(event) = Self::list(event) { + return Poll::Ready(Some(event)); + } + } + } +} + +#[cfg(all(test, feature = "async"))] +mod tests { + use std::time::Duration; + + use super::super::ProduceTest; + use super::*; + use crate::{ + Hop, Instance, + origin::{Cost, Route}, + }; + + /// The listing's next event as `present path` or `absent path`, within the update hold. + async fn listed(announced: &mut AnnounceConsumer) -> String { + let event = kio::clock::timeout(Duration::from_secs(1), announced.next()) + .await + .expect("no event") + .expect("the origin closed"); + match event { + AnnounceEvent::Present(announce) => format!("present {}", announce.prefix), + AnnounceEvent::Absent(announce) => format!("absent {}", announce.prefix), + } + } + + /// The listing stays quiet past the origin's update hold. + async fn quiet(announced: &mut AnnounceConsumer) { + if let Ok(event) = kio::clock::timeout(Duration::from_secs(1), announced.next()).await { + panic!("expected nothing, got {event:?}"); + } + } + + fn route(instance: &Instance) -> Route { + Route::default().with_instance(instance.clone(), 0) + } + + /// A path stays present while another instance takes it over or its route changes, and is + /// absent only once nothing covers it. + #[kio::test] + async fn a_path_stays_present_across_instances() { + let origin = Hop::new(1).unwrap().produce(); + let mut announced = origin.consume().announced(); + let mut routes = origin.consume().routes(); + + let first = origin.publish("live", route(&Instance::random())).unwrap(); + assert_eq!(listed(&mut announced).await, "present live"); + assert!(matches!(routes.next().await, Some(RouteEvent::Start(_)))); + + first + .announce(route(&Instance::random()).with_cost(Cost::new(5))) + .unwrap(); + quiet(&mut announced).await; + // The relay-level stream still reports the replacement. + assert!(matches!(routes.next().await, Some(RouteEvent::Restart(_)))); + + let second = origin.publish("live", route(&Instance::random())).unwrap(); + drop(first); + quiet(&mut announced).await; + + drop(second); + assert_eq!(listed(&mut announced).await, "absent live"); + } +} diff --git a/rs/moq-net/src/model/follow.rs b/rs/moq-net/src/model/follow.rs index e4b1b7058d5..78d90882a38 100644 --- a/rs/moq-net/src/model/follow.rs +++ b/rs/moq-net/src/model/follow.rs @@ -1,7 +1,7 @@ use std::task::{Poll, ready}; use super::WeakEntry; -use super::origin_impl::{Announce, AnnounceConsumer, AnnounceEvent, Consumer, Moved, Requesting}; +use super::origin_impl::{Announce, Consumer, Moved, Requesting, RouteEvent, Routes}; use crate::{Error, Instance, Path, broadcast}; /// The announcements covering one path, as one broadcast coming online, being replaced, and @@ -9,13 +9,13 @@ use crate::{Error, Instance, Path, broadcast}; /// /// Several routes can cover the path at once, such as the path itself and a prefix above it, /// and a request resolves through the most specific one, so only that route's events come -/// through. Another route taking over is a [`Restart`](AnnounceEvent::Restart), unless both +/// through. Another route taking over is a [`Restart`](RouteEvent::Restart), unless both /// carry the same instance: those serve one continuous stream, so it is an -/// [`Update`](AnnounceEvent::Update). One announced as a -/// [`Takeover`](AnnounceEvent::Takeover) of the serving route continues it, and comes through +/// [`Update`](RouteEvent::Update). One announced as a +/// [`Takeover`](RouteEvent::Takeover) of the serving route continues it, and comes through /// as one. Routes beneath the path serve other broadcasts and are ignored. pub(crate) struct Follow { - announced: AnnounceConsumer, + announced: Routes, path: Path, /// Every route standing over the path. covering: Vec, @@ -41,7 +41,7 @@ struct Standing { } impl Follow { - pub(super) fn new(announced: AnnounceConsumer, path: Path) -> Self { + pub(super) fn new(announced: Routes, path: Path) -> Self { Self { announced, path, @@ -54,7 +54,7 @@ impl Follow { /// The next change to the route serving the path, or `None` once the origin closes. #[cfg(all(test, feature = "async"))] - pub async fn next(&mut self) -> Option { + pub async fn next(&mut self) -> Option { kio::wait(|waiter| self.poll_next(waiter)).await } @@ -68,7 +68,7 @@ impl Follow { /// instance. A route whose start came after the serving route's last event, with the cursor /// never running dry in between, may have arrived after its end, so taking over from it is /// reported as that gap. - pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { if self.idle { let ended = loop { match self.poll_announced(waiter) { @@ -82,7 +82,7 @@ impl Follow { return match (self.serving().map(|standing| standing.announce.clone()), ended) { (Some(serving), _) => { self.idle = false; - Poll::Ready(Some(AnnounceEvent::Start(serving))) + Poll::Ready(Some(RouteEvent::Start(serving))) } (None, true) => Poll::Ready(None), (None, false) => Poll::Pending, @@ -94,7 +94,7 @@ impl Follow { return Poll::Ready(None); }; if let Some(event) = self.fold(event) { - self.idle = matches!(event, AnnounceEvent::End(_)); + self.idle = matches!(event, RouteEvent::End(_)); return Poll::Ready(Some(event)); } } @@ -102,13 +102,13 @@ impl Follow { /// Poll until every source feeding this follower has synced, registering `waiter` until /// then. A path [`poll_next`](Self::poll_next) has not started by then is not announced, - /// rather than not heard yet. See [`AnnounceConsumer::poll_synced`]. + /// rather than not heard yet. See [`Routes::poll_synced`]. pub fn poll_synced(&self, waiter: &kio::Waiter) -> Poll<()> { self.announced.poll_synced(waiter) } /// The cursor's next event, counting what it delivers and when it runs dry. - fn poll_announced(&mut self, waiter: &kio::Waiter) -> Poll> { + fn poll_announced(&mut self, waiter: &kio::Waiter) -> Poll> { let poll = self.announced.poll_next(waiter); match poll { Poll::Ready(Some(_)) => self.taken += 1, @@ -126,12 +126,12 @@ impl Follow { } /// Apply one route's event, returning what it means for the path, if anything. - fn fold(&mut self, event: AnnounceEvent) -> Option { - let (AnnounceEvent::Start(announce) - | AnnounceEvent::Update(announce) - | AnnounceEvent::Restart(announce) - | AnnounceEvent::Takeover { announce, .. } - | AnnounceEvent::End(announce)) = &event; + fn fold(&mut self, event: RouteEvent) -> Option { + let (RouteEvent::Start(announce) + | RouteEvent::Update(announce) + | RouteEvent::Restart(announce) + | RouteEvent::Takeover { announce, .. } + | RouteEvent::End(announce)) = &event; if !self.path.has_prefix(&announce.prefix) { return None; } @@ -146,11 +146,11 @@ impl Follow { .position(|standing| standing.announce.prefix == prefix) .map(|index| self.covering.swap_remove(index).since); let (announce, since, restart, predecessor) = match event { - AnnounceEvent::End(_) => (None, None, false, None), - AnnounceEvent::Start(announce) => (Some(announce), None, false, None), - AnnounceEvent::Update(announce) => (Some(announce), standing, false, None), - AnnounceEvent::Restart(announce) => (Some(announce), standing, true, None), - AnnounceEvent::Takeover { announce, predecessor } => (Some(announce), None, true, Some(predecessor)), + RouteEvent::End(_) => (None, None, false, None), + RouteEvent::Start(announce) => (Some(announce), None, false, None), + RouteEvent::Update(announce) => (Some(announce), standing, false, None), + RouteEvent::Restart(announce) => (Some(announce), standing, true, None), + RouteEvent::Takeover { announce, predecessor } => (Some(announce), None, true, Some(predecessor)), }; if let Some(announce) = announce { self.covering.push(Standing { @@ -165,19 +165,19 @@ impl Follow { .map(|standing| (standing.announce.clone(), standing.since)); match (before, after) { (None, None) => None, - (None, Some((after, _))) => Some(AnnounceEvent::Start(after)), - (Some((before, _)), None) => Some(AnnounceEvent::End(before)), + (None, Some((after, _))) => Some(RouteEvent::Start(after)), + (Some((before, _)), None) => Some(RouteEvent::End(before)), // The serving route ended, and the one left may have started after it did. (Some((before, seen)), Some((after, since))) if before.prefix == prefix && before.prefix != after.prefix && since > self.dry.max(seen) => { - Some(AnnounceEvent::End(before)) + Some(RouteEvent::End(before)) } // The serving route continues as this one. (Some((before, _)), Some((after, _))) if after.prefix == prefix && predecessor.as_ref() == Some(&before.prefix) => { - Some(AnnounceEvent::Takeover { + Some(RouteEvent::Takeover { announce: after, predecessor: before.prefix, }) @@ -185,14 +185,14 @@ impl Follow { (Some((before, _)), Some((after, _))) if before.prefix != after.prefix => { let same = before.route.instance == after.route.instance; Some(match same { - true => AnnounceEvent::Update(after), - false => AnnounceEvent::Restart(after), + true => RouteEvent::Update(after), + false => RouteEvent::Restart(after), }) } // A route less specific than the serving one changed, which no request sees. (Some(_), Some((after, _))) if after.prefix != prefix => None, - (Some(_), Some((after, _))) if restart => Some(AnnounceEvent::Restart(after)), - (Some(_), Some((after, _))) => Some(AnnounceEvent::Update(after)), + (Some(_), Some((after, _))) if restart => Some(RouteEvent::Restart(after)), + (Some(_), Some((after, _))) => Some(RouteEvent::Update(after)), } } } @@ -218,6 +218,10 @@ pub struct Following { /// A change resolved to the broadcast handed out last while it was still open, so it counts /// as delivered only until that broadcast closes. absorbed: bool, + /// A broadcast was handed out and its end not reported yet. + live: bool, + /// The broadcast handed out last stopped serving the path, and its end is owed. + ended: bool, /// The last resolution was refused: any change to the serving route, even the same /// instance over another route, is worth asking again. refused: bool, @@ -235,25 +239,30 @@ impl Following { last: None, moved: None, absorbed: false, + live: false, + ended: false, refused: false, } } - /// Wait for the next instance serving the path; see + /// Wait for the next change to the instance serving the path; see /// [`Consumer::follow`](crate::origin::Consumer::follow). /// - /// Returns [`Error::Closed`] once the origin closes, and any other resolution failure - /// as-is. A later call then waits for the serving route to change, even to another route of - /// the same instance, instead of asking the refusing route again. Dropping the future keeps the wait's progress for the + /// Returns the broadcast of each instance as it comes online or takes over, and `None` + /// once the path the one handed out last served goes offline, or, when pinned, another + /// instance takes it over. Returns [`Error::Closed`] once the origin closes, and any other + /// resolution failure as-is, such as a handler refusing the path; the next call then waits + /// for the serving route to change, even to another route of the same instance, instead of + /// asking the refusing route again. Dropping the future keeps the wait's progress for the /// next call. #[cfg(feature = "async")] - pub async fn available(&mut self) -> Result { + pub async fn available(&mut self) -> Result, Error> { kio::wait(|waiter| self.poll_available(waiter)).await } - /// Poll for the next instance serving the path, registering `waiter` when there - /// is none yet. - pub fn poll_available(&mut self, waiter: &kio::Waiter) -> Poll> { + /// Poll for the next change to the instance serving the path, registering `waiter` when + /// there is none yet; see [`Self::available`]. + pub fn poll_available(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { loop { // Apply every change on hand before reading an answer, so a burst of them hands out // only the instance serving the path now. A resolution in flight answers for the @@ -262,13 +271,18 @@ impl Following { if self.settle(waiter)? { self.resolving = None; } + // Report an end before whatever serves next, so a reader drops the old broadcast first. + if self.ended { + self.ended = false; + self.live = false; + return Poll::Ready(Ok(None)); + } // The broadcast handed out ended while the path is still served: its instance's // routes may have gone, and another instance's, beneath the same announcement, // taken the path over. It also ends once the caller lets go of it, so only its // instance no longer serving the path makes whatever serves it now next, unless a - // change it absorbed is owed. After a refusal, only a route change asks again. + // change it absorbed is owed. A refusal forgets it, so only a route change asks again. if !self.unseen - && !self.refused && self.follow.serving().is_some() && let Some(last) = self.last.as_ref().map(broadcast::WeakConsumer::consume) && last.poll_closed(waiter).is_ready() @@ -300,10 +314,21 @@ impl Following { let resolved = ready!(kio::Task::poll(resolving, waiter)); self.resolving = None; // Handed out or refused, this announcement is settled: after a refusal, the next - // call waits for the serving route to change rather than asking it again. + // call waits for the serving route to change rather than asking it again. That + // includes the broadcast handed out last moving off the path, which led here. self.unseen = false; self.refused = resolved.is_err(); - let broadcast = resolved?; + let broadcast = match resolved { + Ok(broadcast) => broadcast, + // `live` stays: the end of what was handed out is still owed once the path goes + // offline. + Err(err) => { + self.last = None; + self.moved = None; + self.absorbed = false; + return Poll::Ready(Err(err)); + } + }; // Resolving can run ahead of the follower: a takeover resolves at once while the // origin holds its restart, and that restart then names the instance handed out. @@ -316,7 +341,8 @@ impl Following { self.last = Some(weak); self.moved = None; self.absorbed = false; - return Poll::Ready(Ok(broadcast)); + self.live = true; + return Poll::Ready(Ok(Some(broadcast))); } } @@ -329,33 +355,42 @@ impl Following { Ok(changed) } + /// The broadcast handed out last stopped serving the path: owe its end, and hand out + /// whatever serves next, even the same instance coming back. + fn end(&mut self) { + self.ended |= self.live; + self.last = None; + self.moved = None; + self.absorbed = false; + } + /// Apply one change to the path, returning whether it is one. - fn apply(&mut self, event: AnnounceEvent) -> bool { + fn apply(&mut self, event: RouteEvent) -> bool { match event { - // Another instance taking over leaves a pinned follower's own stream as it was. - AnnounceEvent::Restart(announce) + // Another instance taking over ends a pinned follower's own stream, which is handed + // out again once it serves the path again. + RouteEvent::Restart(announce) if self .instance .as_ref() .is_some_and(|pin| announce.route.instance.as_ref() != Some(pin)) => { + self.end(); return false; } // A pinned resolution refuses any other instance, waiting until its own serves again. - AnnounceEvent::Start(_) | AnnounceEvent::Restart(_) => self.unseen = true, + RouteEvent::Start(_) | RouteEvent::Restart(_) => self.unseen = true, // An end is a gap that ended every request on the old route, so whatever serves the // path next is handed out, even the same instance coming back. - AnnounceEvent::End(_) => { + RouteEvent::End(_) => { self.unseen = false; - self.last = None; - self.moved = None; - self.absorbed = false; + self.end(); } // After a refusal, another route may answer what the last one refused. - AnnounceEvent::Update(_) | AnnounceEvent::Takeover { .. } if self.refused => self.unseen = true, + RouteEvent::Update(_) | RouteEvent::Takeover { .. } if self.refused => self.unseen = true, // The same instance over another route, which its subscriptions ride out, or one // continuing what was handed out. - AnnounceEvent::Update(_) | AnnounceEvent::Takeover { .. } => return false, + RouteEvent::Update(_) | RouteEvent::Takeover { .. } => return false, } true } @@ -379,11 +414,11 @@ mod tests { .expect("no event") .expect("the origin closed"); match event { - AnnounceEvent::Start(announce) => ("start", announce.prefix.to_string()), - AnnounceEvent::Update(announce) => ("update", announce.prefix.to_string()), - AnnounceEvent::Restart(announce) => ("restart", announce.prefix.to_string()), - AnnounceEvent::Takeover { announce, .. } => ("takeover", announce.prefix.to_string()), - AnnounceEvent::End(announce) => ("end", announce.prefix.to_string()), + RouteEvent::Start(announce) => ("start", announce.prefix.to_string()), + RouteEvent::Update(announce) => ("update", announce.prefix.to_string()), + RouteEvent::Restart(announce) => ("restart", announce.prefix.to_string()), + RouteEvent::Takeover { announce, .. } => ("takeover", announce.prefix.to_string()), + RouteEvent::End(announce) => ("end", announce.prefix.to_string()), } } @@ -401,7 +436,7 @@ mod tests { #[kio::test] async fn follow_reports_the_route_serving_the_path() { let origin = Hop::new(1).unwrap().produce(); - let mut follow = origin.consume().routes("pool/job").unwrap(); + let mut follow = origin.consume().path_routes("pool/job").unwrap(); let instance = Instance::random(); // A prefix above the path covers it. @@ -443,7 +478,7 @@ mod tests { let _pool = origin.requests_route("pool", Route::default(), None).unwrap(); let _exact = origin.publish("pool/job", Route::default()).unwrap(); - let mut follow = origin.consume().routes("pool/job").unwrap(); + let mut follow = origin.consume().path_routes("pool/job").unwrap(); assert_eq!(followed(&mut follow).await, ("start", "pool/job".into())); quiet(&mut follow).await; } @@ -455,7 +490,7 @@ mod tests { let origin = Hop::new(1).unwrap().produce(); let instance = Instance::random(); let exact = origin.publish("pool/job", route(&instance)).unwrap(); - let mut follow = origin.consume().routes("pool/job").unwrap(); + let mut follow = origin.consume().path_routes("pool/job").unwrap(); assert_eq!(followed(&mut follow).await, ("start", "pool/job".into())); // Both land before the follower reads again, and the cursor delivers the prefix's start @@ -478,7 +513,7 @@ mod tests { async fn follow_hands_over_to_a_prefix_that_started_before_the_last_change() { let origin = Hop::new(1).unwrap().produce(); let exact = origin.publish("pool/job", route(&Instance::random())).unwrap(); - let mut follow = origin.consume().routes("pool/job").unwrap(); + let mut follow = origin.consume().path_routes("pool/job").unwrap(); assert_eq!(followed(&mut follow).await, ("start", "pool/job".into())); let instance = Instance::random(); @@ -494,7 +529,7 @@ mod tests { #[kio::test] async fn follow_restarts_onto_a_more_specific_route_under_another_instance() { let origin = Hop::new(1).unwrap().produce(); - let mut follow = origin.consume().routes("pool/job").unwrap(); + let mut follow = origin.consume().path_routes("pool/job").unwrap(); let _pool = origin.requests_route("pool", Route::default(), None).unwrap(); assert_eq!(followed(&mut follow).await, ("start", "pool".into())); @@ -514,7 +549,7 @@ mod tests { .unwrap() .requests_route("room/alice", Route::default(), None) .unwrap(); - let mut follow = origin.consume().routes("room/alice").unwrap(); + let mut follow = origin.consume().path_routes("room/alice").unwrap(); let first = origin.publish("room/alice", Route::default().with_cost(5)).unwrap(); let event = kio::clock::timeout(Duration::from_secs(1), follow.next()) @@ -522,7 +557,7 @@ mod tests { .unwrap() .unwrap(); assert!( - matches!(&event, AnnounceEvent::Start(announce) if announce.route.cost == Cost::new(5)), + matches!(&event, RouteEvent::Start(announce) if announce.route.cost == Cost::new(5)), "{event:?}" ); @@ -534,26 +569,38 @@ mod tests { .unwrap() .unwrap(); assert!( - matches!(&event, AnnounceEvent::Restart(announce) if announce.route.cost == Cost::new(5)), + matches!(&event, RouteEvent::Restart(announce) if announce.route.cost == Cost::new(5)), "{event:?}" ); } - /// The next instance handed out, which must arrive within the origin's update hold. - async fn available(announced: &mut Following) -> broadcast::Consumer { + /// The next change handed out, which must arrive within the origin's update hold. + async fn changed(announced: &mut Following) -> Option { kio::clock::timeout(Duration::from_secs(1), announced.available()) .await - .expect("no instance") + .expect("no change") .expect("the wait failed") } + /// The next instance handed out, which must arrive within the origin's update hold. + async fn available(announced: &mut Following) -> broadcast::Consumer { + changed(announced).await.expect("an end, not an instance") + } + + /// The broadcast handed out last stops serving, with nothing in its place. + async fn ended(announced: &mut Following) { + if let Some(broadcast) = changed(announced).await { + panic!("expected an end, got {:?}", broadcast.info().instance); + } + } + /// Nothing more is handed out past the origin's update hold. async fn none(announced: &mut Following) { let hold = super::super::origin_impl::DEFAULT_UPDATE_HOLD * 2; if let Ok(result) = kio::clock::timeout(hold, announced.available()).await { panic!( "expected nothing, got {:?}", - result.map(|broadcast| broadcast.info().instance.clone()) + result.map(|broadcast| broadcast.map(|broadcast| broadcast.info().instance.clone())) ); } } @@ -613,6 +660,7 @@ mod tests { let dynamic = origin.requests_route("", route(&instance), None).unwrap(); exact.unannounce(); + ended(&mut announced).await; let served = broadcast::Info::new().produce(); let (again, ()) = futures::join!(available(&mut announced), async { requested(&dynamic).await.serve(&served); @@ -620,7 +668,7 @@ mod tests { assert!(again.is_clone(&first), "the route above did not carry the instance on"); } - /// An announced request that pins an instance refuses another instance taking over instead of + /// An announced request that pins an instance ends when another instance takes over instead of /// following it. #[kio::test] async fn announced_pinned_refuses_another_instance() { @@ -631,13 +679,40 @@ mod tests { let pinned = available(&mut announced).await; assert_eq!(pinned.info().instance.as_ref(), Some(&instance)); - let _second = origin.publish("live", route(&Instance::random())).unwrap(); + let second = origin.publish("live", route(&Instance::random())).unwrap(); + ended(&mut announced).await; none(&mut announced).await; // Unpinned, the same takeover is followed. let mut unpinned = origin.consume().follow("live", None).unwrap(); let current = available(&mut unpinned).await; assert_ne!(current.info().instance.as_ref(), Some(&instance)); + + // The pinned instance serves again once the other goes, and is handed out again. + drop(second); + assert_eq!( + available(&mut announced).await.info().instance.as_ref(), + Some(&instance) + ); + assert_eq!(available(&mut unpinned).await.info().instance.as_ref(), Some(&instance)); + } + + /// A follower started before anything serves the path waits for it, reports its end, and + /// hands out what serves it next. + #[kio::test] + async fn announced_reports_the_path_going_offline() { + let origin = Hop::new(1).unwrap().produce(); + let mut announced = origin.consume().follow("live", None).unwrap(); + none(&mut announced).await; + + let first = origin.publish("live", Route::default()).unwrap(); + let started = available(&mut announced).await; + drop(first); + ended(&mut announced).await; + none(&mut announced).await; + + let _second = origin.publish("live", Route::default()).unwrap(); + assert!(!available(&mut announced).await.is_clone(&started)); } #[kio::test] @@ -651,6 +726,7 @@ mod tests { Some(&instance) ); first.unannounce(); + ended(&mut announced).await; none(&mut announced).await; first.announce(route(&instance)).unwrap(); assert_eq!( @@ -714,31 +790,52 @@ mod tests { ); } - /// A refusal after the instance handed out closed is not asked again on every call: the - /// closed instance's handover is settled until the serving route changes. + /// A refusal is reported once, and the follower then waits for the serving route to change + /// rather than asking the refusing handler again. + #[kio::test] + async fn announced_waits_for_a_route_change_after_a_refusal() { + let origin = Hop::new(1).unwrap().produce(); + let claim = origin.requests("live").unwrap(); + let mut announced = origin.consume().follow("live", None).unwrap(); + let (result, ()) = futures::join!( + kio::clock::timeout(Duration::from_secs(1), announced.available()), + async { requested(&claim).await.reject(Error::NotFound) } + ); + assert!(matches!(result.expect("the refusal answers"), Err(Error::NotFound))); + none(&mut announced).await; + let again = kio::clock::timeout(Duration::from_secs(1), claim.requested_broadcast()).await; + assert!(again.is_err(), "the refusing handler was asked again"); + + // A publisher takes the path over from the claim, and is handed out. + let _publisher = origin.publish("live", route(&Instance::random())).unwrap(); + let _served = available(&mut announced).await; + } + + /// A refusal by the route that took over from a closed broadcast is not retried when that + /// broadcast's movement is seen: the follower waits for the serving route to change, and + /// still reports the end of what it handed out once the path goes offline. #[kio::test] - async fn announced_waits_after_refusing_a_closed_instances_successor() { + async fn announced_does_not_retry_a_refusal_after_a_takeover() { let origin = Hop::new(1).unwrap().produce(); - let claim = origin.requests_route("", route(&Instance::random()), None).unwrap(); + let dynamic = origin.requests_route("", route(&Instance::random()), None).unwrap(); let exact = origin.publish("live", route(&Instance::random())).unwrap(); let mut announced = origin.consume().follow("live", None).unwrap(); let _first = available(&mut announced).await; + drop(exact); let (result, ()) = futures::join!( kio::clock::timeout(Duration::from_secs(1), announced.available()), - async { - requested(&claim).await.reject(Error::Unroutable); - } - ); - assert!(matches!( - result.expect("the refusal must answer"), - Err(Error::Unroutable) - )); - let ((), again) = futures::join!( - none(&mut announced), - kio::clock::timeout(Duration::from_secs(1), claim.requested_broadcast()) + async { requested(&dynamic).await.reject(Error::NotFound) } ); - assert!(again.is_err(), "the refusing route was asked again"); + assert!(matches!(result.expect("the refusal answers"), Err(Error::NotFound))); + none(&mut announced).await; + let again = kio::clock::timeout(Duration::from_secs(1), dynamic.requested_broadcast()).await; + assert!(again.is_err(), "the refusing handler was asked again"); + + // The broadcast handed out before the refusal is still owed its end once the path goes + // offline. + drop(dynamic); + ended(&mut announced).await; } /// A path no pattern can spell is refused rather than followed through the whole scope, @@ -747,7 +844,7 @@ mod tests { fn follow_refuses_a_path_no_pattern_can_spell() { let origin = Hop::new(1).unwrap().produce(); assert!(matches!( - origin.consume().routes("camera*main"), + origin.consume().path_routes("camera*main"), Err(Error::InvalidPath(_)) )); } @@ -758,6 +855,6 @@ mod tests { let origin = Hop::new(1).unwrap().produce(); let scope = Patterns::from(Pattern::subtree("pool/job/cam").unwrap()); let scoped = origin.consume().scope("", &scope).unwrap(); - assert!(matches!(scoped.routes("pool/job"), Err(Error::Unauthorized))); + assert!(matches!(scoped.path_routes("pool/job"), Err(Error::Unauthorized))); } } diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index 2d20e881904..c2eb91bd28a 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -12,6 +12,8 @@ pub mod track; // modules so neither leaks the other's plumbing. #[path = "origin.rs"] mod origin_impl; +// The app-level announcement listing over a route stream. +mod announced; // Reduces the routes covering one path to the one serving it. mod follow; // The failover state machine origin fronts run; pure, so its transitions are @@ -63,11 +65,16 @@ pub mod origin { Config, Consumer, Cost, Driver, HopLimit, Link, Preference, Prepared, Producer, Publication, Request, Requesting, Requests, Route, Source, }; + // Relay-level: every route change, for forwarding and stats. Apps read + // `announce` and `follow` instead. + #[doc(hidden)] + pub use super::origin_impl::{RouteEvent, Routes}; } -/// Subscribing to route (un)announcements from an origin. +/// Listing the paths announced on an origin. pub mod announce { - pub use super::origin_impl::{Announce, AnnounceConsumer as Consumer, AnnounceEvent as Event}; + pub use super::announced::{AnnounceConsumer as Consumer, AnnounceEvent as Event}; + pub use super::origin_impl::Announce; } // Hop identity and the `Consume` conversion trait aren't part of a role diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index b08a1aaec0a..d29439757d2 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -14,6 +14,7 @@ use rand::RngExt; use super::{ RequestQueue, WeakCache, WeakEntry, + announced::AnnounceConsumer, follow::{Follow, Following}, front::{Action, Candidate, Event, Front, Refusal}, }; @@ -475,7 +476,7 @@ impl From for Preference { pub struct Route { /// The stream the route serves: routes with one instance serve one continuous /// sequence space, so a subscription resumes from one to another. Changing it on a - /// standing advertisement is an [`AnnounceEvent::Restart`]. + /// standing advertisement is an [`RouteEvent::Restart`]. /// /// Always set on a route the origin delivers. Given to an announcing handle, `None` /// keeps the instance its route already has: a fresh random one, minted when the @@ -738,7 +739,7 @@ fn earliest(a: Option, b: Option) -> Option { } } -/// A retraction's quarantine, keyed by prefix in its [`AnnounceConsumer`]: the +/// A retraction's quarantine, keyed by prefix in its [`Routes`]: the /// retracted route's publisher (none for an anonymous route, which holds /// nothing), instance, epoch, and rank, and when the quarantine ends. #[derive(Clone)] @@ -761,7 +762,7 @@ impl Quarantine { } } -/// A pending update's hold, kept with it in its [`AnnounceConsumer`]'s queue. +/// A pending update's hold, kept with it in its [`Routes`]'s queue. /// /// A consumer reads the origin's clock outside the driver, where it holds the /// driver's last reading, not the time the update arrived. So a hold first waits @@ -773,7 +774,7 @@ enum Held { Until(Instant), } -/// One coalesced update queued for an `AnnounceConsumer`. +/// One coalesced update queued for an `Routes`. /// /// At most one entry exists per prefix, so a slow consumer's pending set is /// bounded by the number of distinct prefixes. A metadata change on a live route @@ -842,7 +843,7 @@ struct OriginConsumerState { /// the end instead of parking forever on a table that can never fire again. ended: bool, /// Snapshots in flight that can present on this consumer's cursors, counted - /// once per cursor they touch. See [`AnnounceConsumer::poll_synced`]. + /// once per cursor they touch. See [`Routes::poll_synced`]. /// /// Changed through a read guard, so a session joining wakes none of the cursors it /// holds back (nothing waits on losing sync), except the last landing, which @@ -1121,13 +1122,13 @@ impl OriginConsumerState { } /// Take the pending update at `prefix` for delivery. - fn take_prefix(&mut self, prefix: Path) -> AnnounceEvent { + fn take_prefix(&mut self, prefix: Path) -> RouteEvent { let (event, owed) = self.take_update(prefix); - let (AnnounceEvent::Start(announce) - | AnnounceEvent::Update(announce) - | AnnounceEvent::Restart(announce) - | AnnounceEvent::Takeover { announce, .. } - | AnnounceEvent::End(announce)) = &event; + let (RouteEvent::Start(announce) + | RouteEvent::Update(announce) + | RouteEvent::Restart(announce) + | RouteEvent::Takeover { announce, .. } + | RouteEvent::End(announce)) = &event; // A trailing announce left pending is still owed. if owed && !self.pending.contains_key(&announce.prefix) { self.owed -= 1; @@ -1135,7 +1136,7 @@ impl OriginConsumerState { event } - fn take_update(&mut self, prefix: Path) -> (AnnounceEvent, bool) { + fn take_update(&mut self, prefix: Path) -> (RouteEvent, bool) { /// What the consumer learns at the prefix. enum Kind { Start, @@ -1210,11 +1211,11 @@ impl OriginConsumerState { }, }; let event = match kind { - Kind::Start => AnnounceEvent::Start(announce), - Kind::Update => AnnounceEvent::Update(announce), - Kind::Restart => AnnounceEvent::Restart(announce), - Kind::Takeover(predecessor) => AnnounceEvent::Takeover { announce, predecessor }, - Kind::End => AnnounceEvent::End(announce), + Kind::Start => RouteEvent::Start(announce), + Kind::Update => RouteEvent::Update(announce), + Kind::Restart => RouteEvent::Restart(announce), + Kind::Takeover(predecessor) => RouteEvent::Takeover { announce, predecessor }, + Kind::End => RouteEvent::End(announce), }; (event, owed) } @@ -2018,9 +2019,9 @@ impl Drop for HiddenWithholding { } } -/// What an [`AnnounceConsumer`] yields. +/// What a [`Routes`] yields. #[derive(Clone, Debug)] -pub enum AnnounceEvent { +pub enum RouteEvent { /// A route now covers the prefix; the cursor had none there. Start(Announce), /// The route covering the prefix changed hops, cost or epoch, with the same instance; @@ -2050,7 +2051,7 @@ pub enum AnnounceEvent { }, } -/// A route over a prefix, delivered by [`AnnounceConsumer`] inside an [`AnnounceEvent`]. +/// A route over a prefix, as an announcement listing or a [`Routes`] stream reports it. /// /// An announcement is always a prefix, never a broadcast: it advertises that /// [`prefix`](Self::prefix) and every path beneath it are servable. A broadcast @@ -2887,7 +2888,7 @@ impl Announcer { /// [`Pattern`] can be served. /// /// Held by a [`Requests`] and by a broadcast's [`Announcer`]; dropping it -/// retracts the route, which [`AnnounceConsumer`]s observe and sessions withdraw +/// retracts the route, which [`Routes`]s observe and sessions withdraw /// from their peers. #[must_use = "dropping an announcement retracts the route"] pub(crate) struct AnnounceProducer { @@ -3090,7 +3091,7 @@ impl Drop for AnnounceProducer { /// /// It holds no [`Producer`] clone, so it never keeps the origin alive. It /// finishes once every owner is gone: each [`Producer`] clone, published -/// broadcast, and [`Requests`]. Read handles ([`Consumer`], [`AnnounceConsumer`]) +/// broadcast, and [`Requests`]. Read handles ([`Consumer`], [`Routes`]) /// are not owners. Dropping it aborts active fronts, rejects pending requests, /// ends announcements, and makes subsequent producer mutations fail with /// [`Error::Closed`]. @@ -4613,7 +4614,7 @@ struct SnapshotEntry { /// A session still delivering its peer's initial announcements under a prefix. /// /// Every announce consumer whose scope the prefix overlaps is unsynced -/// (see [`AnnounceConsumer::poll_synced`]) until this drops: once the session +/// (see [`Routes::poll_synced`]) until this drops: once the session /// has inserted the announcements its peer counted, or it gave up on them. pub(crate) struct Snapshot { shared: Option>>, @@ -5838,7 +5839,7 @@ impl Requests { /// /// A route naming no [`Route::instance`], or the same one, re-prices: consumers observe /// an update and every handle survives. Another instance is another stream: consumers - /// see an [`AnnounceEvent::Restart`], and a re-request never joins what was resolved + /// see an [`RouteEvent::Restart`], and a re-request never joins what was resolved /// under the old one. Requests still waiting on this handle carry over: the handler is /// asked again under the new instance, and its answer to a request asked before the /// change is dropped, never served under the new one. A request pinned to the old @@ -6258,12 +6259,12 @@ impl Requesting { if let Some(discovery) = &mut self.discovery { loop { match discovery.poll_next(waiter) { - Poll::Ready(Some(AnnounceEvent::End(_))) => self.routed = false, + Poll::Ready(Some(RouteEvent::End(_))) => self.routed = false, Poll::Ready(Some( - AnnounceEvent::Start(announce) - | AnnounceEvent::Restart(announce) - | AnnounceEvent::Update(announce) - | AnnounceEvent::Takeover { announce, .. }, + RouteEvent::Start(announce) + | RouteEvent::Restart(announce) + | RouteEvent::Update(announce) + | RouteEvent::Takeover { announce, .. }, )) => { self.routed = self.instance.is_none() || announce.route.instance == self.instance; } @@ -6675,7 +6676,18 @@ impl Consumer { } } - /// Subscribe to route announcements for this consumer's scope. + /// List the paths announced in this consumer's scope, each present or absent. + /// + /// A path stays present while any route covers it, whichever instance serves it: + /// hold a [`follow`](Self::follow) to see the broadcast change. Scope, hiding and + /// sync behave as for [`routes`](Self::routes). Drop the returned + /// [`announce::Consumer`](crate::announce::Consumer) to unregister. + pub fn announced(&self) -> AnnounceConsumer { + AnnounceConsumer::new(self.routes()) + } + + /// Every change to the routes announced in this consumer's scope, for relays and + /// stats: route metadata, instance replacements and takeovers included. /// /// Allocates a per-cursor coalescing buffer and replays the currently /// announced routes as initial updates. Routes stay prefixes and are named @@ -6686,8 +6698,9 @@ impl Consumer { /// patterns are hidden unless [`with_hidden`](Self::with_hidden) opted in. /// The stream also watches its scope: a client session fed by this origin asks /// its peer about the scope while any stream watches it. - /// Drop the returned [`AnnounceConsumer`] to unregister. - pub fn announced(&self) -> AnnounceConsumer { + /// Drop the returned [`Routes`] to unregister. + #[doc(hidden)] + pub fn routes(&self) -> Routes { let state = kio::Producer::::default(); let cursor = |root: Path, allowed: Patterns, hidden: Hidden, mount: Option<&Mount>, under: Path, holes: Vec| { @@ -6713,7 +6726,7 @@ impl Consumer { // A root the mount resolves past the depth limit, or an excluded one, has // nothing to announce. let (Some(root), Some(holes)) = (mount.resolve(&self.root), self.scope.holes(Some(mount))) else { - return AnnounceConsumer::new( + return Routes::new( self.root.clone(), Vec::new(), state, @@ -6730,7 +6743,7 @@ impl Consumer { Path::default(), holes, )]; - return AnnounceConsumer::new( + return Routes::new( self.root.clone(), cursors, state, @@ -6784,7 +6797,7 @@ impl Consumer { holes, ), ); - AnnounceConsumer::new( + Routes::new( self.root.clone(), cursors, state, @@ -6823,7 +6836,7 @@ impl Consumer { /// absence from its sync. Fails with [`Error::Unauthorized`] when this consumer's scope can /// never cover the path, and with [`Error::InvalidPath`] when no pattern can spell it (a /// segment containing `*`). - pub(crate) fn routes(&self, path: impl TryInto>) -> Result { + pub(crate) fn path_routes(&self, path: impl TryInto>) -> Result { let path = path::convert(path)?; // Scope down to the path itself, which still sees every route whose claim covers it: @@ -6841,7 +6854,7 @@ impl Consumer { // Untagged: this is a lookup, not egress announce forwarding, so it must not drive // the announce guards. Hiding narrows discovery, not lookup, so a hidden path // follows like any other. - let announced = consumer.untagged().with_hidden(true).covering().announced(); + let announced = consumer.untagged().with_hidden(true).covering().routes(); Ok(Follow::new(announced, path)) } @@ -6887,23 +6900,25 @@ impl Consumer { /// Follow the broadcast at `path` across the instances that serve it. /// - /// The first [`available`](Following::poll_available) resolves the instance serving the - /// path once one is announced, retrying route churn but returning a selected handler's - /// refusal; the call after a refusal waits for the path's next announcement change. Each later call waits for another instance to take over (an - /// [`AnnounceEvent::Restart`]), or for the path to be served again after an - /// [`AnnounceEvent::End`], even by the same instance. Drop what was read from the - /// previous broadcast then: its tracks end with an error once its routes go. + /// The first [`available`](Following::available) resolves the instance serving the path + /// once one is announced, retrying route churn but returning a selected handler's refusal; + /// the call after a refusal waits for the serving route to change. Each later call returns + /// another instance taking over, or `None` once the path goes offline, then whatever serves + /// it next, even the same instance. Drop what was read from the previous broadcast then: + /// its tracks end with an error once its routes go. Another route of the same instance, + /// or another epoch of it, is not a change. /// - /// Pass an owned [`Instance`] to follow only that stream: another one taking over is - /// never handed out, and the instance is handed out again if it returns after an end. Fails with [`Error::Unauthorized`] when this consumer's scope can never - /// cover the path, and with [`Error::InvalidPath`] when no pattern can spell it. + /// Pass an owned [`Instance`] to follow only that stream: another one taking over ends + /// it, and the instance is handed out again when it serves the path again. Fails with + /// [`Error::Unauthorized`] when this consumer's scope can never cover the path, and with + /// [`Error::InvalidPath`] when no pattern can spell it. pub fn follow( &self, path: impl TryInto>, instance: impl Into>, ) -> Result { let path = path::convert(path)?; - let routes = self.routes(&path)?; + let routes = self.path_routes(&path)?; Ok(Following::new(self.clone(), path, instance.into(), routes)) } @@ -6948,7 +6963,7 @@ impl Consumer { /// /// Another instance winning the path leaves the broadcast to its readers until its /// routes go, while the next request resolves the winner on a fresh broadcast. - /// [`Consumer::announced`] reports that as [`AnnounceEvent::Restart`], and + /// [`Consumer::announced`] reports that as [`RouteEvent::Restart`], and /// [`Self::follow`] yields the next broadcast. /// /// Pass an owned [`Instance`] to refuse a different stream, both when requesting @@ -6972,7 +6987,7 @@ impl Consumer { Err(err) => return kio::Pending::new(self.failed(err)), }; let mut routing = self.routing(path.clone(), instance.into()); - match self.routes(&path) { + match self.path_routes(&path) { Ok(discovery) => { routing.discovery = Some(discovery); if let Some(resolved) = self.root.join(&path).ok().and_then(|named| self.scope.resolve(&named)) { @@ -7163,11 +7178,11 @@ impl Consumer { } } -/// Receives route announcements for a scope. +/// Receives every route change for a scope, for relays and stats. /// -/// Created by [`Consumer::announced`]. +/// Created by [`Consumer::routes`]. /// Drop to unregister. -pub struct AnnounceConsumer { +pub struct Routes { // One registration per table cursor feeding `state`: more than one when the // consumer reads through a mount. ids: Vec, @@ -7204,9 +7219,9 @@ pub struct AnnounceConsumer { hold: crate::time::Deadline, } -impl AnnounceConsumer { +impl Routes { #[cfg(test)] - async fn expect_next(&mut self) -> AnnounceEvent { + async fn expect_next(&mut self) -> RouteEvent { #[cfg(feature = "async")] { return kio::wait(|waiter| self.poll_next(waiter)).await.expect("no next"); @@ -7232,7 +7247,7 @@ impl AnnounceConsumer { expected: impl TryInto, ) -> Route { match self.expect_next().await { - AnnounceEvent::Restart(announce) => { + RouteEvent::Restart(announce) => { assert_eq!( announce.prefix, expected.try_into().expect("expected path"), @@ -7247,7 +7262,7 @@ impl AnnounceConsumer { #[cfg(test)] pub(crate) async fn expect_next_ended(&mut self, expected: impl TryInto) { match self.expect_next().await { - AnnounceEvent::End(announce) => assert_eq!( + RouteEvent::End(announce) => assert_eq!( announce.prefix, expected.try_into().expect("expected path"), "wrong prefix" @@ -7309,8 +7324,8 @@ impl AnnounceConsumer { /// Start a retraction's quarantine. A delivered announcement leaves it to /// expire: later ones for the prefix are updates, which it never holds. - fn quarantine(&mut self, event: &AnnounceEvent, now: Instant) { - let AnnounceEvent::End(update) = event else { + fn quarantine(&mut self, event: &RouteEvent, now: Instant) { + let RouteEvent::End(update) = event else { return; }; if self.update_hold.is_zero() { @@ -7381,18 +7396,18 @@ impl AnnounceConsumer { } /// Drive the egress announce guards for one update. - fn hand_out(&mut self, event: AnnounceEvent) -> AnnounceEvent { + fn hand_out(&mut self, event: RouteEvent) -> RouteEvent { match &event { - AnnounceEvent::Start(announce) - | AnnounceEvent::Update(announce) - | AnnounceEvent::Restart(announce) - | AnnounceEvent::Takeover { announce, .. } => { + RouteEvent::Start(announce) + | RouteEvent::Update(announce) + | RouteEvent::Restart(announce) + | RouteEvent::Takeover { announce, .. } => { let scope = self.stats.egress(self.root.rejoin(&announce.prefix)); self.guards .entry(announce.prefix.clone()) .or_insert_with(|| scope.announce()); } - AnnounceEvent::End(announce) => { + RouteEvent::End(announce) => { self.guards.remove(&announce.prefix); } } @@ -7407,7 +7422,7 @@ impl AnnounceConsumer { /// None if the cursor is closed. The consumer is also a [`futures::Stream`] /// of the same events. #[cfg(feature = "async")] - pub async fn next(&mut self) -> Option { + pub async fn next(&mut self) -> Option { kio::wait(|waiter| self.poll_next(waiter)).await } @@ -7416,7 +7431,7 @@ impl AnnounceConsumer { /// Returns `Poll::Ready(Some(_))` for an event, `Poll::Ready(None)` if the /// cursor is closed, or `Poll::Pending` after registering `waiter` to be /// notified when the next event arrives. - pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { loop { let now = self.timers.now(); let settling = self.settle(now); @@ -7460,7 +7475,7 @@ impl AnnounceConsumer { /// /// Returns None if there is no event available; NOT because the cursor is closed. /// Use [`Self::is_closed`] to check if the cursor is closed. - pub fn try_next(&mut self) -> Option { + pub fn try_next(&mut self) -> Option { let now = self.timers.now(); let settling = self.settle(now); // Read first, so a call with nothing due writes nothing; see `poll_next`. @@ -7582,8 +7597,8 @@ impl AnnounceConsumer { } #[cfg(feature = "async")] -impl futures::Stream for AnnounceConsumer { - type Item = AnnounceEvent; +impl futures::Stream for Routes { + type Item = RouteEvent; fn poll_next(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll> { let this = self.get_mut(); @@ -7592,7 +7607,7 @@ impl futures::Stream for AnnounceConsumer { } } -impl Drop for AnnounceConsumer { +impl Drop for Routes { fn drop(&mut self) { let mut state = self.shared.lock(); for id in &self.ids { @@ -7613,11 +7628,11 @@ use futures::FutureExt; #[cfg(test)] #[expect(missing_docs, reason = "test-only assertion helpers")] -impl AnnounceConsumer { +impl Routes { /// The route of an active event at `expected`. - fn active(event: AnnounceEvent, expected: &Path) -> Route { + fn active(event: RouteEvent, expected: &Path) -> Route { match event { - AnnounceEvent::Start(announce) | AnnounceEvent::Update(announce) | AnnounceEvent::Restart(announce) => { + RouteEvent::Start(announce) | RouteEvent::Update(announce) | RouteEvent::Restart(announce) => { assert_eq!(announce.prefix, *expected, "wrong prefix"); announce.route } @@ -7647,7 +7662,7 @@ impl AnnounceConsumer { .expect("next blocked") .expect("no next") { - AnnounceEvent::Restart(announce) => { + RouteEvent::Restart(announce) => { assert_eq!( announce.prefix, expected.try_into().expect("expected path"), @@ -7666,7 +7681,7 @@ impl AnnounceConsumer { .expect("next blocked") .expect("no next") { - AnnounceEvent::End(announce) => assert_eq!( + RouteEvent::End(announce) => assert_eq!( announce.prefix, expected.try_into().expect("expected path"), "wrong prefix" @@ -7924,7 +7939,7 @@ mod tests { async fn announce_and_retract() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); announced.assert_next_wait(); let announcement = producer.announce("room/alice", Route::default()).unwrap(); @@ -7943,7 +7958,7 @@ mod tests { fn synced_after_the_snapshot_and_its_events() { let producer = origin(1).produce(); let snapshot = producer.snapshot("room"); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); assert!(!announced.is_synced(), "a snapshot in flight holds it"); let _alice = producer.announce("room/alice", Route::default()).unwrap(); @@ -7962,12 +7977,12 @@ mod tests { #[test] fn a_local_origin_catches_up_on_its_replay() { let producer = origin(1).produce(); - let mut empty = producer.consume().announced(); + let mut empty = producer.consume().routes(); assert!(empty.is_synced(), "nothing to wait on"); empty.assert_next_wait(); let _alice = producer.announce("room/alice", Route::default()).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); assert!(!announced.is_synced(), "the replay is owed"); announced.assert_next_active("room/alice"); assert!(announced.is_synced()); @@ -7977,7 +7992,7 @@ mod tests { fn initial_routes_precede_live_prefixes() { let producer = origin(1).produce(); let _initial = producer.announce("z/initial", Route::default()).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let _live = producer.announce("a/live", Route::default()).unwrap(); announced.assert_next_active("z/initial"); assert!(announced.is_synced()); @@ -7987,7 +8002,7 @@ mod tests { fn stalled_initial_replay_collapses_churn_without_a_backlog() { let producer = origin(1).produce(); let initial = producer.announce("z/initial", Route::default()).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); for i in 0..1_000 { let transient = producer.announce(format!("a/{i}"), Route::default()).unwrap(); initial.update(Route::default().with_cost(i)).unwrap(); @@ -8004,7 +8019,7 @@ mod tests { fn a_cancelled_start_still_catches_up() { let producer = origin(1).produce(); let snapshot = producer.snapshot(""); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let alice = producer.announce("room/alice", Route::default()).unwrap(); drop(snapshot); @@ -8018,7 +8033,7 @@ mod tests { #[test] fn an_update_is_not_owed() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let alice = producer.announce("room/alice", Route::default()).unwrap(); announced.assert_next_active("room/alice"); @@ -8032,7 +8047,7 @@ mod tests { #[test] fn a_new_snapshot_takes_sync_back() { let producer = origin(1).produce(); - let announced = producer.consume().announced(); + let announced = producer.consume().routes(); assert!(announced.is_synced()); let snapshot = producer.snapshot("room/alice"); @@ -8054,10 +8069,10 @@ mod tests { .unwrap(); let other = producer.snapshot("other"); - assert!(room.announced().is_synced(), "a disjoint prefix"); + assert!(room.routes().is_synced(), "a disjoint prefix"); let above = producer.snapshot(""); - let announced = room.announced(); + let announced = room.routes(); assert!(!announced.is_synced(), "a snapshot above the root covers it"); drop(above); assert!(announced.is_synced()); @@ -8066,8 +8081,8 @@ mod tests { // A local view never waits on a peer's routes. let _peer = producer.clone().with_link(Link::Peer).snapshot("room"); - assert!(producer.consume().local().announced().is_synced()); - assert!(!producer.consume().announced().is_synced()); + assert!(producer.consume().local().routes().is_synced()); + assert!(!producer.consume().routes().is_synced()); } /// A source shadowed by a mount can present nothing on the handle, so it never holds it. @@ -8075,12 +8090,12 @@ mod tests { fn a_shadowed_snapshot_never_holds() { let producer = origin(1).produce(); let project = mounted(&producer, "p1", &["**"]).with_hidden(true); - let announced = project.announced(); + let announced = project.routes(); assert!(announced.is_synced()); let shadowed = producer.snapshot("p1/.svc"); assert!(announced.is_synced(), "a snapshot in flight under the hole"); - assert!(project.announced().is_synced(), "a reader registering past one"); + assert!(project.routes().is_synced(), "a reader registering past one"); drop(shadowed); let _target = producer.snapshot(".svc/p1"); @@ -8091,14 +8106,11 @@ mod tests { #[test] fn a_hidden_snapshot_holds_only_hidden_readers() { let producer = origin(1).produce(); - let announced = producer.consume().announced(); + let announced = producer.consume().routes(); let _stats = producer.snapshot(".stats"); assert!(announced.is_synced(), "a snapshot in flight under a hidden segment"); - assert!( - producer.consume().announced().is_synced(), - "a reader registering past one" - ); - assert!(!producer.consume().with_hidden(true).announced().is_synced()); + assert!(producer.consume().routes().is_synced(), "a reader registering past one"); + assert!(!producer.consume().with_hidden(true).routes().is_synced()); } /// A waiter parked on syncing is woken when it does. @@ -8113,7 +8125,7 @@ mod tests { let producer = origin(1).produce(); let snapshot = producer.snapshot(""); - let announced = producer.consume().announced(); + let announced = producer.consume().routes(); let woken = Arc::new(Woken(AtomicBool::new(false))); let waiter = kio::Waiter::new(std::task::Waker::from(woken.clone())); assert!(announced.poll_synced(&waiter).is_pending()); @@ -8134,7 +8146,7 @@ mod tests { } let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let woken = Arc::new(Woken(AtomicBool::new(false))); let waiter = kio::Waiter::new(std::task::Waker::from(woken.clone())); assert!(announced.poll_next(&waiter).is_pending()); @@ -8160,12 +8172,12 @@ mod tests { // Only a leading dot hides: a suffix is part of the name. let _suffix = producer.announce("room/catalog.pro", Route::default()).unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); announced.expect_next_active("room/alice").await; announced.expect_next_active("room/catalog.pro").await; announced.assert_next_wait(); - let mut announced = consumer.clone().with_hidden(true).announced(); + let mut announced = consumer.clone().with_hidden(true).routes(); announced.expect_next_active(".stats/node").await; announced.expect_next_active("room/.internal").await; announced.expect_next_active("room/alice").await; @@ -8176,10 +8188,10 @@ mod tests { let mut announced = consumer .scope(".stats", &Patterns::from(Pattern::all())) .unwrap() - .announced(); + .routes(); announced.expect_next_active("node").await; announced.assert_next_wait(); - let mut announced = consumer.scope("", &scopes(&["room/.internal"])).unwrap().announced(); + let mut announced = consumer.scope("", &scopes(&["room/.internal"])).unwrap().routes(); announced.expect_next_active("room/.internal").await; announced.assert_next_wait(); } @@ -8193,7 +8205,7 @@ mod tests { let broadcast = producer.create_broadcast(".stats/node").unwrap(); broadcast.announce(Route::default()).unwrap(); - consumer.announced().assert_next_wait(); + consumer.routes().assert_next_wait(); let resolved = consumer.request(".stats/node", None).await.expect("resolves"); assert_eq!(resolved.info().path.as_str(), ".stats/node"); } @@ -8216,7 +8228,7 @@ mod tests { .exclude(".pro") .unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); announced.expect_next_active("").await; announced.expect_next_active(".private/cam").await; announced.expect_next_active("cam").await; @@ -8246,12 +8258,12 @@ mod tests { let _cam = producer.publish("p1/cam", Route::default()).unwrap(); let project = mounted(&producer, "p1", &["**"]).with_hidden(true); - let mut announced = project.exclude(".svc/foo").unwrap().announced(); + let mut announced = project.exclude(".svc/foo").unwrap().routes(); announced.expect_next_active("cam").await; announced.expect_next_active(".svc/bar").await; announced.assert_next_wait(); - let mut announced = project.exclude(".svc").unwrap().announced(); + let mut announced = project.exclude(".svc").unwrap().routes(); announced.expect_next_active("cam").await; announced.assert_next_wait(); @@ -8261,7 +8273,7 @@ mod tests { .unwrap() .exclude("foo") .unwrap() - .announced(); + .routes(); inside.expect_next_active("bar").await; inside.assert_next_wait(); } @@ -8316,11 +8328,11 @@ mod tests { let project = mounted(&producer, "p1", &["**"]); // A dot segment hides the mount like any other. - let mut announced = project.announced(); + let mut announced = project.routes(); announced.expect_next_active("cam").await; announced.assert_next_wait(); - let mut announced = project.clone().with_hidden(true).covering().announced(); + let mut announced = project.clone().with_hidden(true).covering().routes(); announced.expect_next_active("cam").await; announced.expect_next_active(".svc").await; announced.expect_next_active(".svc/foo").await; @@ -8331,7 +8343,7 @@ mod tests { .scope(".svc", &Patterns::from(Pattern::all())) .unwrap() .covering() - .announced(); + .routes(); inside.expect_next_active("").await; inside.expect_next_active("foo").await; inside.assert_next_wait(); @@ -8362,7 +8374,7 @@ mod tests { .now_or_never() .expect("refused at once"); assert!(matches!(refused, Err(Error::Unauthorized))); - let mut announced = granted.clone().with_hidden(true).announced(); + let mut announced = granted.clone().with_hidden(true).routes(); announced.expect_next_active(".svc/foo").await; announced.assert_next_wait(); @@ -8372,11 +8384,11 @@ mod tests { .now_or_never() .expect("refused at once"); assert!(matches!(refused, Err(Error::Unauthorized))); - narrower.clone().with_hidden(true).announced().assert_next_wait(); + narrower.clone().with_hidden(true).routes().assert_next_wait(); // Another project's mount reaches its own target, never this one's. let other = mounted(&producer, "p2", &["**"]); - other.clone().with_hidden(true).announced().assert_next_wait(); + other.clone().with_hidden(true).routes().assert_next_wait(); let err = other.request(".svc/foo", None).await.err().unwrap(); assert!(matches!(err, Error::Unroutable)); } @@ -8389,14 +8401,14 @@ mod tests { let _foo = producer.publish(".svc/p1/foo", Route::default()).unwrap(); let project = mounted(&producer, "p1", &["**"]).with_hidden(true); - let Some(AnnounceEvent::Start(update)) = project.announced().try_next() else { + let Some(RouteEvent::Start(update)) = project.routes().try_next() else { panic!("expected foo to start"); }; assert_eq!(update.prefix.as_str(), ".svc/foo"); assert_eq!(update.captures, Some(vec![".svc/foo".parse::().unwrap()])); let inside = project.scope(".svc", &Patterns::from(Pattern::all())).unwrap(); - let Some(AnnounceEvent::Start(update)) = inside.announced().try_next() else { + let Some(RouteEvent::Start(update)) = inside.routes().try_next() else { panic!("expected foo to start"); }; assert_eq!(update.prefix.as_str(), "foo"); @@ -8419,7 +8431,7 @@ mod tests { .consume() .with_hidden(true); - let mut announced = project.announced(); + let mut announced = project.routes(); announced.expect_next_active(".svc").await; announced.assert_next_wait(); @@ -8427,7 +8439,7 @@ mod tests { let err = project.request(".svc/x", None).await.err().unwrap(); assert!(matches!(err, Error::BoundsExceeded(_)), "{err:?}"); let inside = project.scope(".svc/x", &Patterns::from(Pattern::all())).unwrap(); - inside.announced().assert_next_wait(); + inside.routes().assert_next_wait(); } /// A mount point deeper than its target never presents a route whose name @@ -8445,7 +8457,7 @@ mod tests { .unwrap() .consume(); - let mut announced = project.announced(); + let mut announced = project.routes(); announced.expect_next_active("a/b/x").await; announced.assert_next_wait(); @@ -8533,7 +8545,7 @@ mod tests { ) .with_hidden(true); - let mut announced = project.announced(); + let mut announced = project.routes(); announced.expect_next_active(".svc/foo").await; project.request(".svc/foo", None).await.expect("resolves"); @@ -8552,8 +8564,8 @@ mod tests { async fn hidden_route_announced_later_stays_hidden() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); - let mut opted = consumer.clone().with_hidden(true).announced(); + let mut announced = consumer.routes(); + let mut opted = consumer.clone().with_hidden(true).routes(); let hidden = producer.announce(".stats/node", Route::default()).unwrap(); announced.assert_next_wait(); @@ -8568,7 +8580,7 @@ mod tests { async fn a_replayed_route_retracted_before_delivery_cancels() { let producer = origin(1).produce(); let alice = producer.announce("alice", Route::default()).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); drop(alice); announced.assert_next_wait(); } @@ -8578,8 +8590,8 @@ mod tests { async fn broadcast_announces_its_own_path() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); - let mut peer = consumer.clone().excluding(Hop::UNKNOWN).announced(); + let mut announced = consumer.routes(); + let mut peer = consumer.clone().excluding(Hop::UNKNOWN).routes(); // Created but not announced: invisible to local and peer cursors alike. let broadcast = producer.create_broadcast("room/alice").unwrap(); @@ -8619,7 +8631,7 @@ mod tests { async fn broadcast_announcement_retracts_with_the_last_producer() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let broadcast = producer.create_broadcast("room/alice").unwrap(); let clone = broadcast.clone(); @@ -8636,7 +8648,7 @@ mod tests { #[kio::test] async fn publish_creates_and_announces_together() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let _broadcast = producer.publish("room/alice", Route::default()).unwrap(); announced.expect_next_active("room/alice").await; } @@ -8655,7 +8667,7 @@ mod tests { let _a = producer.announce("room/alice", Route::default()).unwrap(); let _b = producer.announce("room/bob", Route::default()).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); // Replay follows the table traversal order. announced.expect_next_active("room/alice").await; announced.expect_next_active("room/bob").await; @@ -8669,7 +8681,7 @@ mod tests { // Prefix advertisements stay prefixes. The scope filters requests locally. let _a = scoped.announce("", Route::default()).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); announced.expect_next_active("").await; // Disjoint prefixes cannot be claimed at all. @@ -8685,9 +8697,9 @@ mod tests { let _a = producer.announce("", Route::default()).unwrap(); let consumer = producer.consume().scope("", &scopes(&["room"])).unwrap(); - let mut announced = consumer.clone().covering().announced(); + let mut announced = consumer.clone().covering().routes(); announced.expect_next_active("").await; - consumer.announced().assert_next_wait(); + consumer.routes().assert_next_wait(); } #[kio::test] @@ -8699,14 +8711,14 @@ mod tests { .consume() .scope("room", &Patterns::from(Pattern::all())) .unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); announced.expect_next_active("alice").await; } #[kio::test] async fn best_route_wins_and_fails_over() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let expensive = producer .announce("room", Route::default().with_hops(hops(&[10])).with_cost(5)) @@ -8808,7 +8820,7 @@ mod tests { async fn identical_reannounce_restarts_under_another_instance() { for named in [None, Some(instance())] { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let mut route = Route::default().with_hops(hops(&[10])); route.instance = named.clone(); @@ -8832,7 +8844,7 @@ mod tests { #[cfg(feature = "async")] async fn a_reprice_is_an_update() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let route = producer .requests_route("room", Route::default().with_cost(3), None) .unwrap(); @@ -8840,7 +8852,7 @@ mod tests { route.update_route(Route::default().with_cost(5)).unwrap(); match announced.next().await.expect("no next") { - AnnounceEvent::Update(update) => assert_eq!(update.route.cost, Cost::new(5)), + RouteEvent::Update(update) => assert_eq!(update.route.cost, Cost::new(5)), other => panic!("should be an update: got {other:?}"), } } @@ -8854,7 +8866,7 @@ mod tests { let first = handle.present(bytes::Bytes::from_static(b"first"), false).unwrap(); let second = handle.present(bytes::Bytes::from_static(b"second"), false).unwrap(); let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let route = producer .requests_route("room", Route::default().with_auth(first.clone()), None) .unwrap(); @@ -8862,7 +8874,7 @@ mod tests { route.update_route(Route::default().with_auth(second.clone())).unwrap(); match announced.next().await.expect("no next") { - AnnounceEvent::Update(update) => assert_eq!(update.route.auth, Some(second)), + RouteEvent::Update(update) => assert_eq!(update.route.auth, Some(second)), other => panic!("should be an update: got {other:?}"), } } @@ -8882,7 +8894,7 @@ mod tests { } .produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let peer = producer.clone().with_link(Link::Peer); let old = peer .requests_route( @@ -8937,7 +8949,7 @@ mod tests { } .produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let _old = producer .requests_route("pool/job-0", Route::default().with_hops(hops(&[old_hop])), None) .unwrap(); @@ -8989,7 +9001,7 @@ mod tests { /// Let the fronts react, then expect nothing more on `announced`. #[cfg(feature = "async")] - async fn quiet(announced: &mut AnnounceConsumer) { + async fn quiet(announced: &mut Routes) { for _ in 0..20 { kio::clock::sim::yield_now().await; } @@ -9005,7 +9017,7 @@ mod tests { let consumer = producer.consume(); let a = claim("room", "room", 10, 5); let b = claim("room", "room", 11, 9); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); assert_eq!(announced.expect_next_active("room").await.cost, Cost::new(5)); let _sticky = hold(&consumer, "room/job", &a).await; @@ -9022,10 +9034,10 @@ mod tests { .announce("room", Route::default().with_hops(hops(&[7]))) .unwrap(); - let mut hidden = producer.consume().excluding(origin(7)).announced(); + let mut hidden = producer.consume().excluding(origin(7)).routes(); hidden.assert_next_wait(); - let mut visible = producer.consume().excluding(origin(8)).announced(); + let mut visible = producer.consume().excluding(origin(8)).routes(); visible.expect_next_active("room").await; } @@ -9040,7 +9052,7 @@ mod tests { .announce("local", Route::default().with_hops(hops(&[10]))) .unwrap(); - let mut hidden = producer.consume().excluding(assigned).announced(); + let mut hidden = producer.consume().excluding(assigned).routes(); hidden.expect_next_active("local").await; hidden.assert_next_wait(); } @@ -9048,7 +9060,7 @@ mod tests { #[kio::test] async fn anonymous_route_loses_to_identified_at_any_cost() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let _anonymous = producer .announce("room", Route::default().with_hops(hops(&[0])).with_cost(1)) @@ -9068,7 +9080,7 @@ mod tests { #[kio::test] async fn anonymous_routes_order_by_cost() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let expensive = producer .announce("room", Route::default().with_hops(hops(&[0])).with_cost(5)) @@ -9090,7 +9102,7 @@ mod tests { #[kio::test] async fn anonymous_chain_from_identified_peer_still_ranks_last() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let _anonymous = producer .announce( @@ -9160,7 +9172,7 @@ mod tests { #[kio::test] async fn update_reprices_in_place() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let announcement = producer.announce("room", Route::default()).unwrap(); announced.expect_next_active("room").await; @@ -9174,7 +9186,7 @@ mod tests { #[kio::test] async fn reprice_from_the_current_route_keeps_the_instance() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let dynamic = producer .requests_route("room", Route::default().with_instance(instance(), 0).with_cost(5), None) @@ -9201,7 +9213,7 @@ mod tests { #[kio::test] async fn retract_after_undelivered_reprice_still_delivered() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let announcement = producer.announce("room", Route::default()).unwrap(); announced.expect_next_active("room").await; @@ -9227,7 +9239,7 @@ mod tests { .consume() .scope("room/alice", &Patterns::from(Pattern::all())) .unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let route = announced.expect_next_active("").await; assert_eq!(route.cost, Cost::new(9)); announced.assert_next_wait(); @@ -9246,21 +9258,21 @@ mod tests { .scope("room/alice", &Patterns::from(Pattern::all())) .unwrap() .covering(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); - let Some(Some(AnnounceEvent::Start(first))) = announced.next().now_or_never() else { + let Some(Some(RouteEvent::Start(first))) = announced.next().now_or_never() else { panic!("expected the announcement"); }; assert_eq!(first.prefix.as_str(), ""); assert_eq!(first.captures, Some(Vec::new())); drop(exact); - let Some(Some(AnnounceEvent::End(retracted))) = announced.next().now_or_never() else { + let Some(Some(RouteEvent::End(retracted))) = announced.next().now_or_never() else { panic!("expected the retraction"); }; assert_eq!(retracted.prefix.as_str(), ""); assert_eq!(retracted.captures, Some(Vec::new())); - let Some(Some(AnnounceEvent::Start(replacement))) = announced.next().now_or_never() else { + let Some(Some(RouteEvent::Start(replacement))) = announced.next().now_or_never() else { panic!("expected the replacement"); }; assert_eq!(replacement.prefix.as_str(), ""); @@ -9294,7 +9306,7 @@ mod tests { assert!((&mut resolving).now_or_never().is_none()); broadcast.announce(Route::default()).unwrap(); - let resolved = resolving.await.expect("resolves once announced"); + let resolved = resolving.await.expect("resolves once announced").unwrap(); assert_eq!(resolved.info().path.as_str(), "room/alice"); drop(broadcast); } @@ -9306,7 +9318,7 @@ mod tests { async fn cheaper_remote_route_beats_a_local_broadcast() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let _local = producer .publish("room/alice", Route::default().with_instance(instance(), 0).with_cost(5)) @@ -9718,7 +9730,7 @@ mod tests { async fn local_broadcast_wins_a_cost_tie() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let server = producer .requests_route( @@ -10171,7 +10183,7 @@ mod tests { async fn a_hairpin_through_the_plain_front_is_withdrawn() { let producer = origin(1).produce(); let peer = producer.consume().excluding(origin(7)); - let mut view = peer.announced(); + let mut view = peer.routes(); let incumbent = producer .requests_route( "room", @@ -10530,8 +10542,8 @@ mod tests { .requests_route("live", Route::default().with_cost(1), None) .unwrap(); - let mut announced = producer.consume().announced(); - let Some(Some(AnnounceEvent::Start(update))) = announced.next().now_or_never() else { + let mut announced = producer.consume().routes(); + let Some(Some(RouteEvent::Start(update))) = announced.next().now_or_never() else { panic!("expected the announcement"); }; assert_eq!(update.prefix.as_str(), "live"); @@ -10577,14 +10589,14 @@ mod tests { .scope("", &scopes(&["room/chat"])) .unwrap() .covering() - .announced(); + .routes(); matching.expect_next_active("").await; let mut outside = producer .consume() .scope("", &scopes(&["room/video"])) .unwrap() .covering() - .announced(); + .routes(); outside.assert_next_wait(); let refused = producer @@ -10620,7 +10632,7 @@ mod tests { .scope("", &scopes(&["room/video"])) .unwrap() .covering() - .announced(); + .routes(); assert_eq!(video.expect_next_active("").await.cost, Cost::new(5)); } @@ -10629,18 +10641,18 @@ mod tests { async fn route_changes_preserve_each_cursors_visible_winner() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut all = consumer.clone().announced(); - let mut excluded = consumer.clone().excluding(origin(7)).announced(); + let mut all = consumer.clone().routes(); + let mut excluded = consumer.clone().excluding(origin(7)).routes(); let mut video = consumer .clone() .scope("", &scopes(&["room/live/video"])) .unwrap() .covering() - .announced(); + .routes(); let mut rooted = consumer .scope("room/live", &Patterns::from(Pattern::all())) .unwrap() - .announced(); + .routes(); let chat = producer .scope("", &scopes(&["room/live/chat"])) @@ -10691,7 +10703,7 @@ mod tests { .scope("room/live/video", &Patterns::from(Pattern::all())) .unwrap() .covering() - .announced(); + .routes(); assert_eq!(announced.expect_next_active("").await.cost, Cost::new(9)); broad.update_route(Route::default().with_cost(0)).unwrap(); @@ -10710,7 +10722,7 @@ mod tests { .map(|i| format!("s{i}")) .collect::>() .join("/"); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let dynamic = producer .requests_route(&path, Route::default(), None) @@ -10729,10 +10741,10 @@ mod tests { .requests_route("live", Route::default().with_hops(hops(&[7])), None) .unwrap(); - let mut excluded = producer.consume().excluding(origin(7)).announced(); + let mut excluded = producer.consume().excluding(origin(7)).routes(); excluded.assert_next_wait(); - let mut clean = producer.consume().excluding(origin(8)).announced(); + let mut clean = producer.consume().excluding(origin(8)).routes(); clean.expect_next_active("live").await; } @@ -10743,22 +10755,22 @@ mod tests { use futures::StreamExt; let producer = origin(1).produce(); let server = producer.requests_route("live", Route::default(), None).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let mut next = || StreamExt::next(&mut announced).now_or_never(); - let Some(Some(AnnounceEvent::Start(update))) = next() else { + let Some(Some(RouteEvent::Start(update))) = next() else { panic!("expected the replayed route"); }; assert_eq!(update.prefix.as_str(), "live"); assert!(next().is_none()); drop(server); - assert!(matches!(next(), Some(Some(AnnounceEvent::End(_))))); + assert!(matches!(next(), Some(Some(RouteEvent::End(_))))); } #[kio::test] async fn dynamic_retracts() { let producer = origin(1).produce(); let server = producer.requests_route("live", Route::default(), None).unwrap(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); announced.expect_next_active("live").await; drop(server); @@ -10768,8 +10780,8 @@ mod tests { #[kio::test] async fn local_broadcast_is_invisible_until_announced() { let producer = origin(1).produce(); - let mut local = producer.consume().announced(); - let mut peer = producer.consume().excluding(Hop::UNKNOWN).announced(); + let mut local = producer.consume().routes(); + let mut peer = producer.consume().excluding(Hop::UNKNOWN).routes(); let broadcast = producer.create_broadcast("room/alice").unwrap(); local.assert_next_wait(); peer.assert_next_wait(); @@ -10882,7 +10894,7 @@ mod tests { let source = broadcast::Info::new().produce(); request.serve(&source); - let resolved = resolving.await.expect("resolves via the standby"); + let resolved = resolving.await.expect("resolves via the standby").unwrap(); assert_eq!(resolved.info().path.as_str(), "room/alice"); } @@ -10913,7 +10925,7 @@ mod tests { async fn routes_report_where_they_entered() { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let _ingest = producer .requests_route( @@ -10966,7 +10978,7 @@ mod tests { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); step(Duration::from_millis(1)).await; // One instance, so the winner moving between routes is an update, not a restart. @@ -10986,7 +10998,7 @@ mod tests { assert!(announced.try_next().is_none(), "the update is still held"); step(DEFAULT_UPDATE_HOLD).await; match announced.try_next().expect("the hold passed") { - AnnounceEvent::Update(update) => assert_eq!(update.route.hops, hops(&[9, 3, 4])), + RouteEvent::Update(update) => assert_eq!(update.route.hops, hops(&[9, 3, 4])), other => panic!("should be an update: got {other:?}"), } } @@ -11023,9 +11035,9 @@ mod tests { .unwrap(), ); } - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); for _ in 0..k { - assert!(matches!(announced.try_next(), Some(AnnounceEvent::Start(_)))); + assert!(matches!(announced.try_next(), Some(RouteEvent::Start(_)))); } let before = announced.state.read().steps; @@ -11036,7 +11048,7 @@ mod tests { for (i, route) in far.into_iter().enumerate().rev() { drop(route); match announced.try_next() { - Some(AnnounceEvent::End(end)) => assert_eq!(end.prefix, Path::new(&format!("room/{i}")).unwrap()), + Some(RouteEvent::End(end)) => assert_eq!(end.prefix, Path::new(&format!("room/{i}")).unwrap()), other => panic!("should be a retraction: got {other:?}"), } } @@ -11055,7 +11067,7 @@ mod tests { async fn a_changed_route_waits_out_the_hold() { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); // Holds count on the driver's clock, which starts at its first poll. kio::clock::sleep(Duration::from_millis(1)).await; @@ -11076,7 +11088,7 @@ mod tests { drop(near); drop(far); match announced.next().await.unwrap() { - AnnounceEvent::Update(update) => assert_eq!(update.route.hops, hops(&[9, 5, 6, 7])), + RouteEvent::Update(update) => assert_eq!(update.route.hops, hops(&[9, 5, 6, 7])), other => panic!("should be an update: got {other:?}"), } assert_eq!(kio::clock::now() - start, DEFAULT_UPDATE_HOLD); @@ -11094,7 +11106,7 @@ mod tests { async fn a_retraction_quarantines_a_named_publisher() { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); kio::clock::sleep(Duration::from_millis(1)).await; // Every path is to one publisher, so to one instance. @@ -11112,7 +11124,7 @@ mod tests { let start = kio::clock::now(); let _stale = peer.requests_route("named", route(&[9, 3, 4], 4), None).unwrap(); match announced.next().await.unwrap() { - AnnounceEvent::Start(announce) => assert_eq!(announce.route.hops, hops(&[9, 3, 4])), + RouteEvent::Start(announce) => assert_eq!(announce.route.hops, hops(&[9, 3, 4])), other => panic!("should be a start: got {other:?}"), } assert!(kio::clock::now() - start >= RETRACT_QUARANTINE); @@ -11157,7 +11169,7 @@ mod tests { async fn withdrawn_peer_hides_routes_through_it() { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); // Publisher 9 ingests at relay 2; relay 3 relays 2's route. let direct = || Route::default().with_hops(hops(&[9, 2])).with_via(origin(2)); @@ -11196,7 +11208,7 @@ mod tests { for old_first in [false, true] { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let route = Route::default().with_instance(instance(), 0).with_hops(hops(&[9, 2])); let first = peer.requests_route("room", route.clone(), None).unwrap(); let second = peer.requests_route("room", route.clone(), None).unwrap(); @@ -11249,7 +11261,7 @@ mod tests { async fn source_change_is_an_update() { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let route = Route::default() .with_instance(instance(), 0) @@ -11264,7 +11276,7 @@ mod tests { // The newest identical route wins, so the local twin takes over. let local = producer.requests_route("room", route, None).unwrap(); match announced.next().await.expect("no next") { - AnnounceEvent::Update(announce) => assert_eq!(announce.route.source(), Source::Local), + RouteEvent::Update(announce) => assert_eq!(announce.route.source(), Source::Local), other => panic!("should be an update: got {other:?}"), } @@ -11279,7 +11291,7 @@ mod tests { async fn local_view_hides_peer_routes() { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); - let mut local = producer.consume().local().announced(); + let mut local = producer.consume().local().routes(); let _forwarded = peer .requests_route( @@ -11335,8 +11347,8 @@ mod tests { let producer = origin(1).produce(); let peer = producer.clone().with_link(Link::Peer); let core = producer.clone().with_link(Link::Upstream); - let mut toward_core = core.consume().announced(); - let mut everyone = producer.consume().announced(); + let mut toward_core = core.consume().routes(); + let mut everyone = producer.consume().routes(); let _core = core .requests_route("core", Route::default().with_hops(hops(&[7])).with_via(origin(7)), None) @@ -11448,7 +11460,7 @@ mod tests { #[cfg(feature = "async")] async fn a_prepared_answer_cannot_announce_itself() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let claim = producer.requests("room").unwrap(); announced.assert_next_active("room"); let pending = producer.consume().request("room/alice", None); @@ -11472,7 +11484,7 @@ mod tests { async fn publication_clones_share_control_and_fill_retention() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut events = consumer.announced(); + let mut events = consumer.routes(); let publication = producer.create_broadcast("room/alice").unwrap(); let fill = publication.broadcast().clone(); let control = publication.clone(); @@ -11556,7 +11568,7 @@ mod tests { #[cfg(feature = "async")] async fn a_prepared_answer_for_an_abandoned_claim_is_never_announced() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let claim = producer.requests("room").unwrap(); announced.assert_next_active("room"); let _pending = producer.consume().request("room/alice", None); @@ -11573,7 +11585,7 @@ mod tests { #[cfg(feature = "async")] async fn a_prepared_answer_for_a_released_request_is_never_announced() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let claim = producer.requests("room").unwrap(); announced.assert_next_active("room"); let _pending = producer.consume().request("room/alice", None); @@ -11618,7 +11630,7 @@ mod tests { async fn a_withdrawn_takeover_ends_the_claims_continuity() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let claim = producer.requests("room").unwrap(); announced.assert_next_active("room"); @@ -11631,7 +11643,7 @@ mod tests { let _alice = answer("room/alice").await; match announced.expect_next().await { - AnnounceEvent::Takeover { announce, predecessor } => { + RouteEvent::Takeover { announce, predecessor } => { assert_eq!(announce.prefix.as_str(), "room/alice"); assert_eq!(predecessor.as_str(), "room"); } @@ -11641,7 +11653,7 @@ mod tests { answer("room/bob").await.unannounce(); let _carol = answer("room/carol").await; match announced.expect_next().await { - AnnounceEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "room/carol"), + RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "room/carol"), other => panic!("should be a start: got {other:?}"), } announced.assert_next_wait(); @@ -11867,7 +11879,7 @@ mod tests { async fn root_dynamic_serves_any_path() { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let dynamic = producer.requests_route("", Route::default(), None).unwrap(); // The root claim is advertised like any other prefix. announced.expect_next_active("").await; @@ -11922,9 +11934,9 @@ mod tests { /// The route a follower's first event starts on. #[cfg(feature = "async")] - fn started(event: Option) -> Route { + fn started(event: Option) -> Route { match event.expect("routed") { - AnnounceEvent::Start(announce) => announce.route, + RouteEvent::Start(announce) => announce.route, other => panic!("should start: got {other:?}"), } } @@ -11935,7 +11947,7 @@ mod tests { let producer = origin(1).produce(); let consumer = producer.consume(); - let mut routes = consumer.routes("room/alice").unwrap(); + let mut routes = consumer.path_routes("room/alice").unwrap(); let mut fut = routes.next().boxed(); assert!((&mut fut).now_or_never().is_none()); @@ -11945,7 +11957,7 @@ mod tests { assert_eq!(route.cost, Cost::new(3)); // Already covered: resolves immediately. - let mut routes = consumer.routes("room/alice/cam").unwrap(); + let mut routes = consumer.path_routes("room/alice/cam").unwrap(); started(routes.next().now_or_never().expect("covered")); } @@ -11957,7 +11969,7 @@ mod tests { // A deeper route does not cover the shorter path. let _deep = producer.announce("room/alice/cam", Route::default()).unwrap(); - let mut routes = consumer.routes("room/alice").unwrap(); + let mut routes = consumer.path_routes("room/alice").unwrap(); let mut fut = routes.next().boxed(); assert!((&mut fut).now_or_never().is_none()); @@ -11978,7 +11990,7 @@ mod tests { assert!(consumer.allowed().matches(&path)); - let mut routes = consumer.routes(&path).unwrap(); + let mut routes = consumer.path_routes(&path).unwrap(); let mut fut = routes.next().boxed(); assert!((&mut fut).now_or_never().is_none()); @@ -11993,7 +12005,7 @@ mod tests { let (producer, driver) = Producer::new(Config::new(origin(1))); let consumer = producer.consume(); let _announcement = producer.announce("room", Route::default()).unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); announced.expect_next_active("room").await; let _server = producer.requests_route("served", Route::default(), None).unwrap(); @@ -12018,7 +12030,7 @@ mod tests { assert!(matches!(err, Error::Closed)); // A cursor born after the teardown is born ended. - let mut late = consumer.announced(); + let mut late = consumer.routes(); assert!(late.next().now_or_never().expect("ended").is_none()); } @@ -12028,7 +12040,7 @@ mod tests { fn teardown_owes_what_a_snapshot_held() { let (producer, driver) = Producer::new(Config::new(origin(1))); let snapshot = producer.snapshot("room"); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let _alice = producer.announce("room/alice", Route::default()).unwrap(); drop(driver); @@ -12264,14 +12276,14 @@ mod tests { let (first, ()) = futures::join!(follow.available(), async { queued(&a).await.serve(broadcast::Info::new().produce()); }); - let first = first.unwrap(); + let first = first.unwrap().unwrap(); assert_eq!(first.info().instance, Some("a".parse().unwrap())); drop(a); let (second, ()) = futures::join!(follow.available(), async { queued(&b).await.serve(broadcast::Info::new().produce()); }); - let second = second.unwrap(); + let second = second.unwrap().unwrap(); assert_eq!(second.info().instance, Some("b".parse().unwrap())); assert!(!second.is_clone(&first)); } @@ -12303,7 +12315,7 @@ mod tests { let (first, ()) = futures::join!(follow.available(), async { queued(&a).await.serve(broadcast::Info::new().produce()); }); - let first = first.unwrap(); + let first = first.unwrap().unwrap(); let weak = first.weak(); drop(first); settle(|| weak.consume().poll_closed(&kio::Waiter::noop()).is_ready()).await; @@ -12325,7 +12337,7 @@ mod tests { let (second, ()) = futures::join!(follow.available(), async { queued(&b).await.serve(broadcast::Info::new().produce()); }); - assert_eq!(second.unwrap().info().instance, Some("b".parse().unwrap())); + assert_eq!(second.unwrap().unwrap().info().instance, Some("b".parse().unwrap())); } /// Write one single-frame group and read it back through `subscription`. @@ -12633,7 +12645,7 @@ mod tests { #[cfg(feature = "async")] async fn the_newest_epoch_is_announced_in_place() { let producer = origin(1).produce(); - let mut announced = producer.consume().announced(); + let mut announced = producer.consume().routes(); let instance = crate::Instance::random(); let _old = producer .publish( @@ -13867,7 +13879,7 @@ mod tests { .scope("", &scopes(&["alpha", "beta"])) .unwrap() .covering(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); announced.expect_next_active("").await; announced.assert_next_wait(); } @@ -13918,7 +13930,7 @@ mod tests { let cursor = consumer .scope("", &scopes(&["room/a", "other/deep/head"])) .unwrap() - .announced(); + .routes(); let route = producer.announce("room/a/b/c", Route::default()).unwrap(); { let table = producer.shared.lock(); @@ -14324,12 +14336,12 @@ mod tests { .consume() .scope("", &Patterns::from("room/*/chat".parse::().unwrap())) .unwrap(); - let mut listed = consumer.announced(); - let mut announced = consumer.covering().announced(); + let mut listed = consumer.routes(); + let mut announced = consumer.covering().routes(); let alice = producer.create_broadcast("room/alice/chat").unwrap(); alice.announce(Route::default()).unwrap(); - let Some(AnnounceEvent::Start(update)) = announced.try_next() else { + let Some(RouteEvent::Start(update)) = announced.try_next() else { panic!("expected alice's chat"); }; assert_eq!(update.prefix.as_str(), "room/alice/chat"); @@ -14341,7 +14353,7 @@ mod tests { announced.assert_next_wait(); let broad = producer.announce("room", Route::default()).unwrap(); - let Some(AnnounceEvent::Start(update)) = announced.try_next() else { + let Some(RouteEvent::Start(update)) = announced.try_next() else { panic!("expected the overlapping broad route"); }; assert_eq!(update.prefix.as_str(), "room"); @@ -14364,11 +14376,11 @@ mod tests { let cam = producer.announce("project/cam.hang", Route::default()).unwrap(); let overlap = producer.announce("project/room/mic.hang", Route::default()).unwrap(); let chat = producer.announce("project/lobby.chat", Route::default()).unwrap(); - let mut routing = consumer.clone().covering().announced(); + let mut routing = consumer.clone().covering().routes(); routing.expect_next_active("cam.hang").await; routing.expect_next_active("lobby.chat").await; routing.expect_next_active("room/mic.hang").await; - let mut selected = consumer.announced(); + let mut selected = consumer.routes(); selected.expect_next_active("cam.hang").await; selected.expect_next_active("lobby.chat").await; selected.assert_next_wait(); @@ -14392,7 +14404,7 @@ mod tests { .scope("room/alice", &Patterns::from(Pattern::all())) .unwrap(); let broad = producer.announce("room", Route::default()).unwrap(); - let mut selected = consumer.announced(); + let mut selected = consumer.routes(); selected.assert_next_wait(); let first_instance = crate::Instance::random(); @@ -14400,21 +14412,21 @@ mod tests { .announce("room/alice", Route::default().with_instance(first_instance.clone(), 0)) .unwrap(); assert!( - matches!(selected.expect_next().await, AnnounceEvent::Start(a) if a.prefix.is_empty() && a.captures.is_some()) + matches!(selected.expect_next().await, RouteEvent::Start(a) if a.prefix.is_empty() && a.captures.is_some()) ); exact .update(Route::default().with_instance(first_instance, 0).with_cost(2)) .unwrap(); - assert!(matches!(selected.expect_next().await, AnnounceEvent::Update(a) if a.route.cost == Cost::new(2))); + assert!(matches!(selected.expect_next().await, RouteEvent::Update(a) if a.route.cost == Cost::new(2))); exact .update(Route::default().with_instance(crate::Instance::random(), 0)) .unwrap(); - assert!(matches!(selected.expect_next().await, AnnounceEvent::Restart(_))); + assert!(matches!(selected.expect_next().await, RouteEvent::Restart(_))); drop(exact); selected.expect_next_ended("").await; selected.assert_next_wait(); let _again = producer.announce("room/alice", Route::default()).unwrap(); - assert!(matches!(selected.expect_next().await, AnnounceEvent::Start(_))); + assert!(matches!(selected.expect_next().await, RouteEvent::Start(_))); drop(broad); selected.assert_next_wait(); drop(selected); @@ -14438,13 +14450,10 @@ mod tests { .with_hidden(true) .exclude(".svc/private") .unwrap(); - let mut selected = project.announced(); + let mut selected = project.routes(); selected.expect_next_active(".svc/.private/lobby.chat").await; selected.assert_next_wait(); - let mut inside = project - .scope(".svc", &Patterns::from(Pattern::all())) - .unwrap() - .announced(); + let mut inside = project.scope(".svc", &Patterns::from(Pattern::all())).unwrap().routes(); inside.expect_next_active(".private/lobby.chat").await; inside.assert_next_wait(); let live = producer.announce(".svc/p1/lobby.chat", Route::default()).unwrap(); @@ -14472,7 +14481,7 @@ mod tests { .tier(stats::Tier::default()) .session(Path::from_static("project")), ); - let mut selected = consumer.announced(); + let mut selected = consumer.routes(); selected.expect_next_active("cam.chat").await; selected.assert_next_wait(); let resolved = consumer.request("cam.chat", None).await.unwrap(); @@ -14505,7 +14514,7 @@ mod tests { assert_eq!(report.traffic[0].publisher.announces_ended, 1); // Discovery of a request still sees a covering route that enumeration omits. - let mut routes = consumer.routes("room/lobby.chat").unwrap(); + let mut routes = consumer.path_routes("room/lobby.chat").unwrap(); assert!(routes.next().await.is_some()); drop(routes); drop(overlap); @@ -14523,8 +14532,8 @@ mod tests { let local = producer.create_broadcast("room/alice").unwrap(); local.announce(Route::default()).unwrap(); - let mut announced = producer.consume().announced(); - let Some(AnnounceEvent::Start(update)) = announced.try_next() else { + let mut announced = producer.consume().routes(); + let Some(RouteEvent::Start(update)) = announced.try_next() else { panic!("expected one winning route"); }; assert_eq!(update.prefix.as_str(), "room/alice"); diff --git a/rs/moq-net/src/model/socket.rs b/rs/moq-net/src/model/socket.rs index 3870d01f825..76df834ffaf 100644 --- a/rs/moq-net/src/model/socket.rs +++ b/rs/moq-net/src/model/socket.rs @@ -840,7 +840,8 @@ impl Step { loop { match self { Self::Resolving(following) => { - let broadcast = ready!(following.poll_available(waiter))?; + // Only the first call is made here, and it never reports the path offline. + let broadcast = ready!(following.poll_available(waiter))?.expect("the first call is never offline"); let subscribing = broadcast.track(track)?.subscribe(reliable()).into_inner(); *self = Self::Subscribing(subscribing); } diff --git a/rs/moq-net/tests/integration/announce_covering_route.rs b/rs/moq-net/tests/integration/announce_covering_route.rs index 9b1db074a8b..c88a1524e01 100644 --- a/rs/moq-net/tests/integration/announce_covering_route.rs +++ b/rs/moq-net/tests/integration/announce_covering_route.rs @@ -27,8 +27,8 @@ fn produce_origin(hop: u64) -> origin::Producer { } /// Drain every event the cursor has pending, as `kind prefix` lines. -fn drain(announced: &mut moq_net::announce::Consumer) -> Vec { - use moq_net::announce::Event; +fn drain(announced: &mut moq_net::origin::Routes) -> Vec { + use moq_net::origin::RouteEvent as Event; let mut seen = Vec::new(); while let Some(event) = announced.try_next() { let (kind, announce) = match event { @@ -62,7 +62,7 @@ async fn covering(version: Option<&str>) -> Vec { (subscriber.consume(), Some(connect_mock(options).await)) } }; - let mut announced = observer.scope(REQUESTED, &everything).unwrap().covering().announced(); + let mut announced = observer.scope(REQUESTED, &everything).unwrap().covering().routes(); let mut log = Vec::new(); let served = publisher.create_broadcast(SERVED).unwrap(); @@ -146,8 +146,8 @@ async fn relays_route_a_covering_claim_that_listings_omit() { let _downstream = connect_mock(options).await; let scoped = client.consume().scope("", &chat).unwrap(); - let mut listed = scoped.announced(); - let mut covering = scoped.clone().covering().announced(); + let mut listed = scoped.routes(); + let mut covering = scoped.clone().covering().routes(); kio::clock::sleep(SETTLE).await; assert_eq!(drain(&mut listed), ["Start lobby.chat"], "{version}"); let mut routes = drain(&mut covering); diff --git a/rs/moq-net/tests/integration/announce_takeover.rs b/rs/moq-net/tests/integration/announce_takeover.rs index bf28bbf2ae3..f3c08ef14c0 100644 --- a/rs/moq-net/tests/integration/announce_takeover.rs +++ b/rs/moq-net/tests/integration/announce_takeover.rs @@ -9,7 +9,7 @@ use crate::support; use std::{cell::RefCell, rc::Rc, time::Duration}; -use moq_net::{Hop, Timestamp, Version, announce, origin, track}; +use moq_net::{Hop, Timestamp, Version, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; const TEST_TIMEOUT: Duration = Duration::from_secs(600); @@ -73,17 +73,17 @@ impl Worker { } /// Every announce event already delivered, as `(kind, prefix, predecessor)`. -async fn drain(announced: &mut announce::Consumer) -> Vec<(&'static str, String, Option)> { +async fn drain(announced: &mut origin::Routes) -> Vec<(&'static str, String, Option)> { let mut events = Vec::new(); while let Ok(event) = kio::clock::timeout(Duration::from_secs(1), announced.next()).await { events.push(match event.expect("announce ended") { - announce::Event::Start(a) => ("start", a.prefix.to_string(), None), - announce::Event::Update(a) => ("update", a.prefix.to_string(), None), - announce::Event::Restart(a) => ("restart", a.prefix.to_string(), None), - announce::Event::Takeover { announce, predecessor } => { + origin::RouteEvent::Start(a) => ("start", a.prefix.to_string(), None), + origin::RouteEvent::Update(a) => ("update", a.prefix.to_string(), None), + origin::RouteEvent::Restart(a) => ("restart", a.prefix.to_string(), None), + origin::RouteEvent::Takeover { announce, predecessor } => { ("takeover", announce.prefix.to_string(), Some(predecessor.to_string())) } - announce::Event::End(a) => ("end", a.prefix.to_string(), None), + origin::RouteEvent::End(a) => ("end", a.prefix.to_string(), None), }); } events @@ -117,8 +117,8 @@ async fn chain(version: &str, claim: &str) { let _downstream = link(version, &relay, &edge, 1).await; settle().await; - let mut at_relay = relay.consume().announced(); - let mut at_edge = edge.consume().announced(); + let mut at_relay = relay.consume().routes(); + let mut at_edge = edge.consume().routes(); let claimed = [("start", claim.to_string(), None)]; assert_eq!(drain(&mut at_relay).await, claimed, "{version}"); assert_eq!(drain(&mut at_edge).await, claimed, "{version}"); @@ -149,7 +149,7 @@ async fn chain(version: &str, claim: &str) { assert_eq!(drain(&mut at_edge).await, answered, "{version}: at the edge"); // A late snapshot starts what stands, replaying no takeover. - let mut late = edge.consume().announced(); + let mut late = edge.consume().routes(); let mut snapshot = vec![("start", "pool/p".to_string(), None)]; if claim != "pool/p" { snapshot.insert(0, ("start", claim.to_string(), None)); @@ -191,7 +191,7 @@ async fn two_workers_announce_a_start() { let _b = link(version, &b.origin, &relay, 10).await; settle().await; - let mut announced = relay.consume().announced(); + let mut announced = relay.consume().routes(); assert_eq!(drain(&mut announced).await, [("start", "pool".to_string(), None)]); assert_eq!(watch(&relay, "pool/p").await, "a"); @@ -217,7 +217,7 @@ async fn an_unrequested_publish_is_a_start() { let _link = link(version, &worker.origin, &relay, 1).await; settle().await; - let mut announced = relay.consume().announced(); + let mut announced = relay.consume().routes(); assert_eq!(drain(&mut announced).await, [("start", "pool".to_string(), None)]); let _direct = worker.origin.publish("pool/q", origin::Route::default()).unwrap(); @@ -240,8 +240,8 @@ async fn an_older_downstream_gets_a_start() { let _downstream = link("moq-lite-06".parse().unwrap(), &relay, &edge, 1).await; settle().await; - let mut at_relay = relay.consume().announced(); - let mut at_edge = edge.consume().announced(); + let mut at_relay = relay.consume().routes(); + let mut at_edge = edge.consume().routes(); drain(&mut at_relay).await; drain(&mut at_edge).await; diff --git a/rs/moq-net/tests/integration/announce_to_serve.rs b/rs/moq-net/tests/integration/announce_to_serve.rs index 35895427298..112ac37fba1 100644 --- a/rs/moq-net/tests/integration/announce_to_serve.rs +++ b/rs/moq-net/tests/integration/announce_to_serve.rs @@ -43,8 +43,8 @@ enum Observer { } /// Drain every route event the cursor has pending, as `kind prefix` lines. -fn drain(announced: &mut moq_net::announce::Consumer) -> Vec { - use moq_net::announce::Event; +fn drain(announced: &mut moq_net::origin::Routes) -> Vec { + use moq_net::origin::RouteEvent as Event; let mut seen = Vec::new(); while let Some(event) = announced.try_next() { @@ -141,7 +141,7 @@ async fn lifecycle(observer: Observer) -> Vec { (subscriber.consume(), Some(pair)) } }; - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let mut log = Vec::new(); // Created, not yet announced: nobody can see or reach it. diff --git a/rs/moq-net/tests/integration/auth.rs b/rs/moq-net/tests/integration/auth.rs index a6a38b67c17..57fdedb60b2 100644 --- a/rs/moq-net/tests/integration/auth.rs +++ b/rs/moq-net/tests/integration/auth.rs @@ -396,16 +396,16 @@ async fn granted(session: &Session) -> Grant { /// Wait until `path` is announced (`true`) or retracted (`false`) in `origin`. async fn wait_announced(origin: &origin::Consumer, path: &str, active: bool) { - let mut announced = origin.announced(); + let mut announced = origin.routes(); let mut live = std::collections::HashSet::new(); - let apply = |live: &mut std::collections::HashSet, event: moq_net::announce::Event| match event { - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) - | moq_net::announce::Event::Takeover { announce: update, .. } => { + let apply = |live: &mut std::collections::HashSet, event: moq_net::origin::RouteEvent| match event { + moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Update(update) + | moq_net::origin::RouteEvent::Restart(update) + | moq_net::origin::RouteEvent::Takeover { announce: update, .. } => { live.insert(update.prefix.to_string()); } - moq_net::announce::Event::End(update) => { + moq_net::origin::RouteEvent::End(update) => { live.remove(update.prefix.as_str()); } }; @@ -1104,6 +1104,7 @@ async fn exact_and_subtree_grants_admit_their_broadcast(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let prefs = moq_net::track::Subscription::default() .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(10))); @@ -1166,6 +1167,7 @@ async fn a_live_subscription_survives_union_renewal(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut group = published.append_group().unwrap(); group @@ -1246,6 +1248,7 @@ async fn a_revoked_grant_cancels_its_subscriptions(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut down_sub = remote.track("video").unwrap().subscribe(prefs()).await.unwrap(); down_sub.recv_group().await.unwrap().unwrap(); @@ -1256,6 +1259,7 @@ async fn a_revoked_grant_cancels_its_subscriptions(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut up_sub = remote.track("video").unwrap().subscribe(prefs()).await.unwrap(); up_sub.recv_group().await.unwrap().unwrap(); @@ -1333,6 +1337,7 @@ async fn lite_07_a_revoked_grant_cancels_its_datagram_subscriptions() { .unwrap() .available() .await + .unwrap() .unwrap(); let mut reader = remote.datagrams("audio").unwrap(); reader.info().await.unwrap(); @@ -1707,6 +1712,7 @@ async fn a_narrowing_deafens_one_path(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut audio_sub = remote.track("opus").unwrap().subscribe(prefs()).await.unwrap(); audio_sub.recv_group().await.unwrap().unwrap(); @@ -1716,6 +1722,7 @@ async fn a_narrowing_deafens_one_path(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut video_sub = remote.track("h264").unwrap().subscribe(prefs()).await.unwrap(); video_sub.recv_group().await.unwrap().unwrap(); @@ -1795,6 +1802,7 @@ async fn a_narrowing_aborts_what_the_peer_published(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut mic_sub = remote.track("opus").unwrap().subscribe(prefs()).await.unwrap(); mic_sub.recv_group().await.unwrap().unwrap(); @@ -1804,6 +1812,7 @@ async fn a_narrowing_aborts_what_the_peer_published(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut cam_sub = remote.track("h264").unwrap().subscribe(prefs()).await.unwrap(); cam_sub.recv_group().await.unwrap().unwrap(); @@ -1855,6 +1864,7 @@ async fn a_narrowing_resets_a_fetch_in_flight(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut fetched = remote.track("video").unwrap().fetch_group(0, None).await.unwrap(); let frame = fetched.read_frame().await.unwrap().expect("first frame"); @@ -1926,6 +1936,7 @@ async fn a_widening_brings_back_a_deafened_path(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = remote.track("opus").unwrap().subscribe(prefs()).await.unwrap(); sub.recv_group().await.unwrap().unwrap(); @@ -1944,6 +1955,7 @@ async fn a_widening_brings_back_a_deafened_path(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = remote.track("opus").unwrap().subscribe(prefs()).await.unwrap(); let mut group = audio_track.append_group().unwrap(); @@ -2002,6 +2014,7 @@ async fn a_widening_brings_back_what_the_peer_published(version: &'static str) { .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = remote.track("opus").unwrap().subscribe(prefs()).await.unwrap(); let mut group = mic_track.append_group().unwrap(); diff --git a/rs/moq-net/tests/integration/broadcast_close.rs b/rs/moq-net/tests/integration/broadcast_close.rs index cd65a8ca432..12b8087d2f1 100644 --- a/rs/moq-net/tests/integration/broadcast_close.rs +++ b/rs/moq-net/tests/integration/broadcast_close.rs @@ -30,7 +30,8 @@ async fn close_then_lookup(publisher: moq_net::origin::Producer, reader: moq_net let handle = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); handle.track("video").expect("a live broadcast serves its track"); broadcast.close(); diff --git a/rs/moq-net/tests/integration/catalog_acquisition.rs b/rs/moq-net/tests/integration/catalog_acquisition.rs index 2f694bb89ed..413282e2b71 100644 --- a/rs/moq-net/tests/integration/catalog_acquisition.rs +++ b/rs/moq-net/tests/integration/catalog_acquisition.rs @@ -38,10 +38,11 @@ async fn acquisition(version: &str, carries_instance: bool) { .unwrap() .available() .await + .unwrap() .unwrap(); - let mut announced = subscriber.consume().announced(); + let mut announced = subscriber.consume().routes(); let old_cost = match announced.next().await { - Some(moq_net::announce::Event::Start(announce)) => announce.route.cost, + Some(moq_net::origin::RouteEvent::Start(announce)) => announce.route.cost, other => panic!("expected the first route, got {other:?}"), }; let mut pending = Box::pin(remote.track("catalog.json").unwrap().subscribe(None)); @@ -55,8 +56,8 @@ async fn acquisition(version: &str, carries_instance: bool) { // A replacement under a minted instance restarts the advertisement; a carried one only // updates it. let new_cost = match announced.next().await { - Some(moq_net::announce::Event::Update(announce)) if carries_instance => announce.route.cost, - Some(moq_net::announce::Event::Restart(announce)) if !carries_instance => announce.route.cost, + Some(moq_net::origin::RouteEvent::Update(announce)) if carries_instance => announce.route.cost, + Some(moq_net::origin::RouteEvent::Restart(announce)) if !carries_instance => announce.route.cost, other => panic!("{version}: unexpected route change {other:?}"), }; assert!(new_cost < old_cost, "the cheaper session wins the route"); diff --git a/rs/moq-net/tests/integration/compress.rs b/rs/moq-net/tests/integration/compress.rs index f11f2ed7f55..140957d3803 100644 --- a/rs/moq-net/tests/integration/compress.rs +++ b/rs/moq-net/tests/integration/compress.rs @@ -101,7 +101,13 @@ async fn relay(published: Published, hops: [&str; 2], subscription: moq_net::tra let downstream = connect_mock(options).await; let consumer = origin.consume(); - let remote = consumer.follow("bcast", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let subscriber = remote.track("data").unwrap().subscribe(subscription).await.unwrap(); support::harness::subscribed(&track).await; @@ -286,6 +292,7 @@ async fn ietf_fetch_keeps_the_stored_form() { .unwrap() .available() .await + .unwrap() .unwrap(); let consumer = remote.track("data").unwrap(); assert!(consumer.info().await.unwrap().compress, "{version}"); diff --git a/rs/moq-net/tests/integration/control_stats.rs b/rs/moq-net/tests/integration/control_stats.rs index 4ad814fdbd9..fcf0ea05e5b 100644 --- a/rs/moq-net/tests/integration/control_stats.rs +++ b/rs/moq-net/tests/integration/control_stats.rs @@ -43,6 +43,7 @@ async fn control_classes_cross_lite_and_ietf_sessions_without_counting_payload() .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = remote.track("video").unwrap().subscribe(None).await.unwrap(); subscribed(&track).await; diff --git a/rs/moq-net/tests/integration/dial_split_horizon.rs b/rs/moq-net/tests/integration/dial_split_horizon.rs index d017fcbe397..6b3ebf74f1d 100644 --- a/rs/moq-net/tests/integration/dial_split_horizon.rs +++ b/rs/moq-net/tests/integration/dial_split_horizon.rs @@ -94,16 +94,16 @@ fn traffic(registry: &stats::Registry, role: stats::Role) -> stats::Traffic { /// The hop the dialer attributed `path` to, once that announce has arrived. async fn learned_hop(dialer: &origin::Producer, path: &str) -> Hop { - let mut announced = dialer.consume().announced(); + let mut announced = dialer.consume().routes(); let event = kio::clock::timeout(SETTLE, async { loop { let event = announced.next().await.expect("announce cursor closed"); let announce = match event { - moq_net::announce::Event::Start(announce) - | moq_net::announce::Event::Update(announce) - | moq_net::announce::Event::Restart(announce) - | moq_net::announce::Event::Takeover { announce, .. } => announce, - moq_net::announce::Event::End(_) => continue, + moq_net::origin::RouteEvent::Start(announce) + | moq_net::origin::RouteEvent::Update(announce) + | moq_net::origin::RouteEvent::Restart(announce) + | moq_net::origin::RouteEvent::Takeover { announce, .. } => announce, + moq_net::origin::RouteEvent::End(_) => continue, }; if announce.prefix.as_str() == path { return announce; @@ -160,12 +160,12 @@ async fn split_horizon(version: &str, [publisher_hop, subscriber_hop]: [Hop; 2]) // The other edge learns the namespace, attributed to a different hop, so the // two dials are not one endpoint. - let mut subscriber_announced = subscriber.consume().announced(); + let mut subscriber_announced = subscriber.consume().routes(); let subscriber_saw = kio::clock::timeout(SETTLE, subscriber_announced.next()) .await .expect("the other edge never learned the namespace") .expect("announce cursor closed"); - let moq_net::announce::Event::Start(subscriber_saw) = subscriber_saw else { + let moq_net::origin::RouteEvent::Start(subscriber_saw) = subscriber_saw else { panic!("the other edge's first event was not the namespace: {subscriber_saw:?}"); }; assert_eq!(subscriber_saw.prefix.as_str(), "room"); diff --git a/rs/moq-net/tests/integration/empty_group.rs b/rs/moq-net/tests/integration/empty_group.rs index 4fd5e61799f..e8939361e74 100644 --- a/rs/moq-net/tests/integration/empty_group.rs +++ b/rs/moq-net/tests/integration/empty_group.rs @@ -39,7 +39,8 @@ async fn an_empty_group_arrives_empty() { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("room", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut sub = kio::clock::timeout(TIMEOUT, remote.track("data").unwrap().subscribe(None)) .await .expect("subscribe timeout") diff --git a/rs/moq-net/tests/integration/errored_end.rs b/rs/moq-net/tests/integration/errored_end.rs index dbed5db770e..b8e01ef86bb 100644 --- a/rs/moq-net/tests/integration/errored_end.rs +++ b/rs/moq-net/tests/integration/errored_end.rs @@ -101,7 +101,8 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); session.server_transport.split_unis(); if subscribe_first { diff --git a/rs/moq-net/tests/integration/extensions.rs b/rs/moq-net/tests/integration/extensions.rs index a477925cf59..ecadc6b76a2 100644 --- a/rs/moq-net/tests/integration/extensions.rs +++ b/rs/moq-net/tests/integration/extensions.rs @@ -40,17 +40,17 @@ fn assert_open(pair: &MockPair, context: &str) { /// Whether `path` is announced in `origin` right now. fn announced(origin: &origin::Producer, path: &str) -> bool { - let mut announced = origin.consume().announced(); + let mut announced = origin.consume().routes(); let mut live = false; while let Some(event) = announced.try_next() { match event { - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) - | moq_net::announce::Event::Takeover { announce: update, .. } => { + moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Update(update) + | moq_net::origin::RouteEvent::Restart(update) + | moq_net::origin::RouteEvent::Takeover { announce: update, .. } => { live |= update.prefix.as_str() == path; } - moq_net::announce::Event::End(update) => { + moq_net::origin::RouteEvent::End(update) => { live &= update.prefix.as_str() != path; } } diff --git a/rs/moq-net/tests/integration/fastest_reader.rs b/rs/moq-net/tests/integration/fastest_reader.rs index 83cb18d0f01..ee34fabce4c 100644 --- a/rs/moq-net/tests/integration/fastest_reader.rs +++ b/rs/moq-net/tests/integration/fastest_reader.rs @@ -52,7 +52,8 @@ async fn relay(version: &str) -> Relay { let remote = kio::clock::timeout(TIMEOUT, relay.consume().follow("live", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let subscriber = kio::clock::timeout( TIMEOUT, remote.track("data").unwrap().subscribe( @@ -305,6 +306,7 @@ async fn two_relays_bound_their_cache_and_resume_without_a_hole() { .unwrap() .available() .await + .unwrap() .unwrap(); let mut subscriber = remote .track("data") diff --git a/rs/moq-net/tests/integration/fetch_fanout.rs b/rs/moq-net/tests/integration/fetch_fanout.rs index e35e461eb49..0288444bf41 100644 --- a/rs/moq-net/tests/integration/fetch_fanout.rs +++ b/rs/moq-net/tests/integration/fetch_fanout.rs @@ -44,6 +44,7 @@ async fn sequential_viewers_share_the_relays_fetched_groups() { .unwrap() .available() .await + .unwrap() .unwrap(); viewers.push((viewer, pair, remote)); } diff --git a/rs/moq-net/tests/integration/fetch_from_feed.rs b/rs/moq-net/tests/integration/fetch_from_feed.rs index abfc3f3ab52..e912b320558 100644 --- a/rs/moq-net/tests/integration/fetch_from_feed.rs +++ b/rs/moq-net/tests/integration/fetch_from_feed.rs @@ -49,7 +49,8 @@ async fn relay(version: &str) -> Relay { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); Relay { track, remote, diff --git a/rs/moq-net/tests/integration/fetch_only.rs b/rs/moq-net/tests/integration/fetch_only.rs index 7c5f461857a..28a9c6589a2 100644 --- a/rs/moq-net/tests/integration/fetch_only.rs +++ b/rs/moq-net/tests/integration/fetch_only.rs @@ -37,6 +37,7 @@ async fn resolve(origin: &moq_net::origin::Producer) -> moq_net::broadcast::Cons .await .expect("resolve timeout") .expect("broadcast resolves") + .unwrap() } /// Publish a finished track of three groups, and flag any subscription it ever gets. diff --git a/rs/moq-net/tests/integration/fetch_pipeline.rs b/rs/moq-net/tests/integration/fetch_pipeline.rs index 5cb165927ab..cc792354fb7 100644 --- a/rs/moq-net/tests/integration/fetch_pipeline.rs +++ b/rs/moq-net/tests/integration/fetch_pipeline.rs @@ -68,7 +68,8 @@ async fn fetch_ahead( let remote = kio::clock::timeout(TIMEOUT, upstream.consume().follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let fetching = kio::clock::sim::spawn(async move { remote.track("video").unwrap().fetch_group(0, None).await }); let request = kio::clock::timeout(TIMEOUT, dynamic.requested_track()) @@ -156,7 +157,8 @@ async fn first_fetch(version: &str, relays: u64, rtt: Duration) -> Duration { let remote = kio::clock::timeout(TIMEOUT, upstream.consume().follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let start = kio::clock::now(); let mut fetched = kio::clock::timeout(TIMEOUT, remote.track("video").unwrap().fetch_group(0, None)) .await diff --git a/rs/moq-net/tests/integration/finished_broadcast_mock.rs b/rs/moq-net/tests/integration/finished_broadcast_mock.rs index 2c80d58a072..5695bfba586 100644 --- a/rs/moq-net/tests/integration/finished_broadcast_mock.rs +++ b/rs/moq-net/tests/integration/finished_broadcast_mock.rs @@ -53,7 +53,8 @@ async fn round(finish_broadcast: bool) -> (Vec>, Option) let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); // Resolved before the ending starts: a SUBSCRIBE sent alongside its TRACK reaches the // publisher before the subscriber has the TRACK_INFO it accepts the track with. diff --git a/rs/moq-net/tests/integration/finite_relay.rs b/rs/moq-net/tests/integration/finite_relay.rs index 417c62d5750..c3a4ddb0c29 100644 --- a/rs/moq-net/tests/integration/finite_relay.rs +++ b/rs/moq-net/tests/integration/finite_relay.rs @@ -83,7 +83,8 @@ async fn round(name: &str, arrival: Arrival) -> (Vec, Result<(), moq_net::E let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let (mut heads, mut opened) = futures::channel::mpsc::unbounded(); let reader = kio::clock::sim::spawn(async move { diff --git a/rs/moq-net/tests/integration/goaway.rs b/rs/moq-net/tests/integration/goaway.rs index 4be7c5e2239..a8484e96229 100644 --- a/rs/moq-net/tests/integration/goaway.rs +++ b/rs/moq-net/tests/integration/goaway.rs @@ -395,7 +395,8 @@ async fn goaway_keeps_new_subscribes(version: Version) { .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut existing = bc.track("video").unwrap().subscribe(None).await.expect("subscribe"); let mut group = track.append_group().expect("append group"); @@ -507,9 +508,9 @@ async fn goaway_drains_routes(version: Version) { // Nothing else is sent on the announce stream, so this only resolves if the // subscriber acted on the GOAWAY rather than on another message. The drain // re-prices the route in place, which arrives as another active update. - let mut announced = sub.announced(); + let mut announced = sub.routes(); loop { - let (moq_net::announce::Event::Start(update) | moq_net::announce::Event::Update(update)) = + let (moq_net::origin::RouteEvent::Start(update) | moq_net::origin::RouteEvent::Update(update)) = announced.next().await.expect("update") else { continue; diff --git a/rs/moq-net/tests/integration/group_order.rs b/rs/moq-net/tests/integration/group_order.rs index 44ab3e2aa5d..dc372896fd3 100644 --- a/rs/moq-net/tests/integration/group_order.rs +++ b/rs/moq-net/tests/integration/group_order.rs @@ -53,7 +53,8 @@ fn read_arrivals(origin: moq_net::origin::Producer, order: Order) -> kio::clock: .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let subscription = Subscription::default() .with_delay(Delay::default().with_max(FOREVER)) .with_range(0..) diff --git a/rs/moq-net/tests/integration/history_groups.rs b/rs/moq-net/tests/integration/history_groups.rs index 78c7ac48ab6..be77e0f69c1 100644 --- a/rs/moq-net/tests/integration/history_groups.rs +++ b/rs/moq-net/tests/integration/history_groups.rs @@ -75,7 +75,8 @@ async fn round(version: &str, relay: bool, newest_first: bool) -> Vec<(u64, usiz let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let reader = kio::clock::sim::spawn(async move { let subscription = Subscription::default() @@ -219,7 +220,13 @@ async fn a_late_subscriber_receives_the_relays_cached_history() { options.client_subscribe = Some(first.clone()); let first_pair = connect_mock(options).await; let consumer = first.consume(); - let remote = consumer.follow("bcast", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let subscription = Subscription::default() .with_delay(Delay::default().with_max(FOREVER)) .with_range(0..); @@ -258,7 +265,13 @@ async fn a_late_subscriber_receives_the_relays_cached_history() { options.client_subscribe = Some(late.clone()); let late_pair = connect_mock(options).await; let consumer = late.consume(); - let remote = consumer.follow("bcast", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut late_sub = remote.track("history").unwrap().subscribe(subscription).await.unwrap(); kio::clock::timeout(TIMEOUT, read_history(&mut late_sub)) .await diff --git a/rs/moq-net/tests/integration/idle_cancel_refetch.rs b/rs/moq-net/tests/integration/idle_cancel_refetch.rs index 9b8128e03a3..04ff961ba2f 100644 --- a/rs/moq-net/tests/integration/idle_cancel_refetch.rs +++ b/rs/moq-net/tests/integration/idle_cancel_refetch.rs @@ -46,6 +46,7 @@ async fn a_group_cut_by_an_idle_cancel_is_refetched_whole() { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .unwrap() + .unwrap() .unwrap(); { diff --git a/rs/moq-net/tests/integration/instance.rs b/rs/moq-net/tests/integration/instance.rs index d1b3bb6ff7d..1210936a07d 100644 --- a/rs/moq-net/tests/integration/instance.rs +++ b/rs/moq-net/tests/integration/instance.rs @@ -7,7 +7,7 @@ use crate::support; use std::time::Duration; -use moq_net::{Hop, Instance, Version, announce, origin}; +use moq_net::{Hop, Instance, Version, origin}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; fn produce_origin(hop: u64) -> origin::Producer { @@ -24,24 +24,24 @@ async fn link(version: Version, from: &origin::Producer, to: &origin::Producer) } /// The instance of the next route `announced` starts, failing on any other event. -async fn started(announced: &mut announce::Consumer) -> Instance { +async fn started(announced: &mut origin::Routes) -> Instance { match kio::clock::timeout(Duration::from_secs(10), announced.next()) .await .expect("announce hung") .expect("announce ended") { - announce::Event::Start(announce) => announce.route.instance.expect("a delivered route has an instance"), + origin::RouteEvent::Start(announce) => announce.route.instance.expect("a delivered route has an instance"), other => panic!("expected a start, got {other:?}"), } } -async fn ended(announced: &mut announce::Consumer) { +async fn ended(announced: &mut origin::Routes) { match kio::clock::timeout(Duration::from_secs(10), announced.next()) .await .expect("announce hung") .expect("announce ended") { - announce::Event::End(_) => {} + origin::RouteEvent::End(_) => {} other => panic!("expected an end, got {other:?}"), } } @@ -59,7 +59,7 @@ async fn job_reannounced(version: &str) { .publish("job", origin::Route::default().with_instance(instance.clone(), 0)) .unwrap(); let _session = link(version, &publisher, &relay).await; - let mut announced = relay.consume().announced(); + let mut announced = relay.consume().routes(); let first = started(&mut announced).await; job.unannounce(); diff --git a/rs/moq-net/tests/integration/late_lower_group.rs b/rs/moq-net/tests/integration/late_lower_group.rs index 34fb1058b88..5973d5bd659 100644 --- a/rs/moq-net/tests/integration/late_lower_group.rs +++ b/rs/moq-net/tests/integration/late_lower_group.rs @@ -118,7 +118,8 @@ async fn session(version: &str, relay: bool, start: Option) -> Vec<(u64, us let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let subscription = Subscription::default() .with_delay(Delay::default().with_max(BUDGET)) @@ -235,7 +236,8 @@ async fn merged(version: &str) -> (Vec<(u64, usize)>, Vec<(u64, usize)>) { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let subscribe = |range: Range| { let track = remote.track("late").unwrap(); diff --git a/rs/moq-net/tests/integration/lazy_discovery.rs b/rs/moq-net/tests/integration/lazy_discovery.rs index 811f89b2f31..e5d65118542 100644 --- a/rs/moq-net/tests/integration/lazy_discovery.rs +++ b/rs/moq-net/tests/integration/lazy_discovery.rs @@ -6,7 +6,7 @@ use std::time::Duration; -use moq_net::{Error, Hop, Pattern, Patterns, Version, announce, origin}; +use moq_net::{Error, Hop, Pattern, Patterns, Version, origin}; use crate::support; use support::harness::{MockConnectOptions, MockPair, connect_mock}; @@ -77,12 +77,12 @@ impl Lazy { } /// Watch everything under `prefix`. - fn watch(&self, prefix: &str) -> announce::Consumer { + fn watch(&self, prefix: &str) -> origin::Routes { self.subscriber .consume() .scope(prefix, &Patterns::from(Pattern::all())) .unwrap() - .announced() + .routes() } /// Inspect the current table without running discovery for the observation itself. @@ -92,16 +92,16 @@ impl Lazy { .consume() .scope("", &Patterns::from(Pattern::literal(path).unwrap())) .unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); matches!( announced.poll_next(&kio::Waiter::noop()), - std::task::Poll::Ready(Some(announce::Event::Start(_))) + std::task::Poll::Ready(Some(origin::RouteEvent::Start(_))) ) } } /// Read until the stream syncs, returning the prefixes it then holds. -async fn synced(announced: &mut announce::Consumer) -> Vec { +async fn synced(announced: &mut origin::Routes) -> Vec { let mut seen = Vec::new(); while !announced.poll_synced(&moq_net::kio::Waiter::noop()).is_ready() { let event = kio::clock::timeout(SETTLE, announced.next()) @@ -109,11 +109,11 @@ async fn synced(announced: &mut announce::Consumer) -> Vec { .expect("synced in time") .expect("the stream stays open"); match event { - announce::Event::Start(announce) - | announce::Event::Restart(announce) - | announce::Event::Takeover { announce, .. } => seen.push(announce.prefix.to_string()), - announce::Event::End(announce) => seen.retain(|prefix| *prefix != announce.prefix.as_str()), - announce::Event::Update(_) => {} + origin::RouteEvent::Start(announce) + | origin::RouteEvent::Restart(announce) + | origin::RouteEvent::Takeover { announce, .. } => seen.push(announce.prefix.to_string()), + origin::RouteEvent::End(announce) => seen.retain(|prefix| *prefix != announce.prefix.as_str()), + origin::RouteEvent::Update(_) => {} } } seen.sort(); @@ -151,7 +151,7 @@ async fn a_new_session_asks_about_existing_watches() { .consume() .scope("b", &Patterns::from(Pattern::all())) .unwrap() - .announced(); + .routes(); let lazy = Lazy::with("moq-lite-07-wip", subscriber).await; assert_eq!(synced(&mut watch).await, ["y"]); assert_eq!(lazy.requests(), 1); @@ -283,11 +283,13 @@ async fn waiting_on_a_path_asks_about_it() { kio::clock::timeout(SETTLE, consumer.follow("a/x", None).unwrap().available()) .await .expect("routed in time") + .unwrap() .unwrap(); let mut followed = consumer.follow("b/y", None).unwrap(); kio::clock::timeout(SETTLE, followed.available()) .await .expect("announced in time") + .unwrap() .unwrap(); assert_eq!(lazy.requests(), 2, "{version}"); } @@ -416,19 +418,19 @@ const IETF: &[&str] = &["moq-transport-18", "moq-transport-22"]; /// Apply what `announced` reports once everything in flight has arrived to `held`, and /// say whether any of it ended a prefix. -async fn settled(announced: &mut announce::Consumer, held: &mut Vec) -> bool { +async fn settled(announced: &mut origin::Routes, held: &mut Vec) -> bool { kio::clock::sleep(SETTLE).await; let mut ended = false; while let Some(event) = announced.try_next() { match event { - announce::Event::Start(announce) - | announce::Event::Restart(announce) - | announce::Event::Takeover { announce, .. } => held.push(announce.prefix.to_string()), - announce::Event::End(announce) => { + origin::RouteEvent::Start(announce) + | origin::RouteEvent::Restart(announce) + | origin::RouteEvent::Takeover { announce, .. } => held.push(announce.prefix.to_string()), + origin::RouteEvent::End(announce) => { ended = true; held.retain(|prefix| *prefix != announce.prefix.as_str()) } - announce::Event::Update(_) => {} + origin::RouteEvent::Update(_) => {} } } held.sort(); @@ -526,6 +528,7 @@ async fn ietf_waiting_on_a_path_asks_about_it() { kio::clock::timeout(SETTLE, consumer.follow("a/x", None).unwrap().available()) .await .expect("routed in time") + .unwrap() .unwrap(); assert!(!lazy.routes("b/y").await, "{version}"); } diff --git a/rs/moq-net/tests/integration/mesh_withdraw.rs b/rs/moq-net/tests/integration/mesh_withdraw.rs index 8fdb0c95dae..d69f27178fb 100644 --- a/rs/moq-net/tests/integration/mesh_withdraw.rs +++ b/rs/moq-net/tests/integration/mesh_withdraw.rs @@ -13,7 +13,7 @@ use std::{collections::HashMap, time::Duration}; use futures::channel::mpsc; -use moq_net::{Hop, Version, announce, cache, origin}; +use moq_net::{Hop, Version, cache, origin}; use support::harness::{MockConnectOptions, MockPair, connect_mock, peer, peer_with_latency}; fn produce_origin(hop: u64) -> origin::Producer { @@ -32,7 +32,7 @@ fn produce(hop: u64, hold: Duration) -> origin::Producer { producer } -/// The kind of an [`announce::Event`] for a prefix. +/// The kind of an [`origin::RouteEvent`] for a prefix. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Kind { Start, @@ -55,16 +55,16 @@ fn drain(watched: &mut mpsc::UnboundedReceiver<(String, Kind)>) -> HashMap mpsc::UnboundedReceiver<(String, Kind)> { +fn watch(mut announced: origin::Routes) -> mpsc::UnboundedReceiver<(String, Kind)> { let (tx, rx) = mpsc::unbounded(); kio::clock::sim::spawn(async move { while let Some(event) = announced.next().await { let (kind, announce) = match event { - announce::Event::Start(announce) => (Kind::Start, announce), - announce::Event::Update(announce) => (Kind::Update, announce), - announce::Event::Restart(announce) => (Kind::Restart, announce), - announce::Event::Takeover { announce, .. } => (Kind::Takeover, announce), - announce::Event::End(announce) => (Kind::End, announce), + origin::RouteEvent::Start(announce) => (Kind::Start, announce), + origin::RouteEvent::Update(announce) => (Kind::Update, announce), + origin::RouteEvent::Restart(announce) => (Kind::Restart, announce), + origin::RouteEvent::Takeover { announce, .. } => (Kind::Takeover, announce), + origin::RouteEvent::End(announce) => (Kind::End, announce), }; if tx.unbounded_send((announce.prefix.to_string(), kind)).is_err() { break; @@ -89,7 +89,7 @@ impl Mesh { for &(a, b) in edges { pairs.push(peer(version, &nodes[a], &nodes[b]).await); } - let watched = watch(nodes.last().unwrap().consume().announced()); + let watched = watch(nodes.last().unwrap().consume().routes()); Self { nodes, _pairs: pairs, @@ -223,7 +223,7 @@ async fn partial_mesh_withdraw_retracts_once() { for (&(a, b), &ms) in PRODUCTION.iter().zip(PRODUCTION_LATENCY_MS) { pairs.push(peer_with_latency(version, &nodes[a], &nodes[b], Duration::from_millis(ms)).await); } - let mut watched: Vec<_> = nodes.iter().map(|node| watch(node.consume().announced())).collect(); + let mut watched: Vec<_> = nodes.iter().map(|node| watch(node.consume().routes())).collect(); for publisher in [5, 13, 22, 26, 32] { let broadcast = nodes[publisher].create_broadcast(format!("room/{publisher}")).unwrap(); @@ -278,7 +278,7 @@ async fn retracted_relay_ignores_a_stale_path() { for &((a, b), latency) in STALE_PATHS { pairs.push(peer_with_latency(version, &nodes[a], &nodes[b], latency).await); } - let mut watched: Vec<_> = nodes.iter().map(|node| watch(node.consume().announced())).collect(); + let mut watched: Vec<_> = nodes.iter().map(|node| watch(node.consume().routes())).collect(); let broadcast = nodes[7].create_broadcast("room/7").unwrap(); broadcast.announce(Default::default()).unwrap(); @@ -351,8 +351,8 @@ impl Cluster { let mut cluster = Self { _pairs: pairs, publisher: publishing, - client: watch(watching.consume().announced()), - relays: relays.iter().map(|relay| watch(relay.consume().announced())).collect(), + client: watch(watching.consume().routes()), + relays: relays.iter().map(|relay| watch(relay.consume().routes())).collect(), }; // Let every session's announce interest land before anything is published. kio::clock::sim::idle().await; @@ -470,7 +470,7 @@ async fn late_better_path(mesh: &str, client: &str) -> (Vec, Vec) { options.server_publish = Some(relays[3].consume()); options.client_subscribe = Some(watching.clone()); pairs.push(connect_mock(options).await); - let mut watcher = watch(watching.consume().announced()); + let mut watcher = watch(watching.consume().routes()); kio::clock::sim::idle().await; assert!(drain(&mut watcher).is_empty()); @@ -709,8 +709,8 @@ async fn hop_limit_bounds_receive_and_send() { let sink = produce_origin(3); let _upstream = peer(version, &source, &relay).await; let _downstream = peer(version, &relay, &sink).await; - let mut received = watch(relay.consume().announced()); - let mut sent = watch(sink.consume().announced()); + let mut received = watch(relay.consume().routes()); + let mut sent = watch(sink.consume().routes()); kio::clock::sim::idle().await; let route = |count: u64| { origin::Route::default().with_hops( @@ -774,7 +774,7 @@ async fn hop_limit_falls_back_to_a_forwardable_route() { let _near = peer(version, &near, &relay).await; let _far = peer(version, &far, &relay).await; let _downstream = peer(version, &relay, &sink).await; - let mut sent = watch(sink.consume().announced()); + let mut sent = watch(sink.consume().routes()); kio::clock::sim::idle().await; let route = |first: u64, count: u64, cost: u64| { diff --git a/rs/moq-net/tests/integration/model.rs b/rs/moq-net/tests/integration/model.rs index c4b1050a23e..d77efb0e9a0 100644 --- a/rs/moq-net/tests/integration/model.rs +++ b/rs/moq-net/tests/integration/model.rs @@ -64,7 +64,7 @@ fn follow_resolves_with_only_explicit_polls() { broadcast.announce(origin::Route::default()).unwrap(); assert!(following.poll_available(&waiter).is_pending()); driver.poll(now, &waiter).unwrap(); - let resolved = ready(following.poll_available(&waiter)).unwrap(); + let resolved = ready(following.poll_available(&waiter)).unwrap().unwrap(); assert_eq!(resolved.info().path.as_str(), "room/live"); // A closed origin ends a parked follower. diff --git a/rs/moq-net/tests/integration/multi_cdn.rs b/rs/moq-net/tests/integration/multi_cdn.rs index f2fa393dd3a..9dceb213ee0 100644 --- a/rs/moq-net/tests/integration/multi_cdn.rs +++ b/rs/moq-net/tests/integration/multi_cdn.rs @@ -15,7 +15,7 @@ use crate::support; use std::time::Duration; use futures::{StreamExt, channel::mpsc}; -use moq_net::{Error, Hop, Timestamp, Version, announce, origin, track}; +use moq_net::{Error, Hop, Timestamp, Version, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; /// A millisecond track: the frames written here carry timestamps. @@ -127,13 +127,13 @@ async fn next(rx: &mut mpsc::UnboundedReceiver) -> (u64, Option bool { +fn via_a(announce: &origin::RouteEvent) -> bool { let route = match announce { - announce::Event::Start(a) - | announce::Event::Update(a) - | announce::Event::Restart(a) - | announce::Event::Takeover { announce: a, .. } => &a.route, - announce::Event::End(_) => return false, + origin::RouteEvent::Start(a) + | origin::RouteEvent::Update(a) + | origin::RouteEvent::Restart(a) + | origin::RouteEvent::Takeover { announce: a, .. } => &a.route, + origin::RouteEvent::End(_) => return false, }; route.hops.iter().any(|hop| *hop == Hop::new(A).unwrap()) } @@ -275,10 +275,10 @@ fn assert_continuous(seen: &[(u64, Vec, Option)], groups: u64) { async fn fails_over_and_back(secondary: &str) { kio::clock::timeout(TEST_TIMEOUT, async { let mut topology = Topology::new(secondary.parse().unwrap()).await; - let mut announced = topology.subscriber.consume().announced(); + let mut announced = topology.subscriber.consume().routes(); let start = announced.next().await.expect("announced"); assert!( - matches!(start, announce::Event::Start(_)) && via_a(&start), + matches!(start, origin::RouteEvent::Start(_)) && via_a(&start), "the preferred CDN wins over the cheaper one: {start:?}" ); @@ -288,7 +288,7 @@ async fn fails_over_and_back(secondary: &str) { topology.lose_a().await; let update = announced.next().await.expect("announced"); assert!( - matches!(update, announce::Event::Update(_)) && !via_a(&update), + matches!(update, origin::RouteEvent::Update(_)) && !via_a(&update), "same instance over B: {update:?}" ); seen.extend(topology.group_anywhere(&mut rx).await); @@ -296,7 +296,7 @@ async fn fails_over_and_back(secondary: &str) { topology.restore_a().await; let update = announced.next().await.expect("announced"); assert!( - matches!(update, announce::Event::Update(_)) && via_a(&update), + matches!(update, origin::RouteEvent::Update(_)) && via_a(&update), "back on A: {update:?}" ); seen.extend(topology.group_through_a(&mut rx).await); @@ -320,7 +320,7 @@ async fn preferred_cdn_fails_over_and_back() { async fn restarts_across_protocols(secondary: &str) { kio::clock::timeout(TEST_TIMEOUT, async { let mut topology = Topology::new(secondary.parse().unwrap()).await; - let mut announced = topology.subscriber.consume().announced(); + let mut announced = topology.subscriber.consume().routes(); let start = announced.next().await.expect("announced"); assert!(via_a(&start), "{start:?}"); @@ -331,7 +331,7 @@ async fn restarts_across_protocols(secondary: &str) { topology.lose_a().await; let update = announced.next().await.expect("announced"); assert!( - matches!(update, announce::Event::Restart(_)) && !via_a(&update), + matches!(update, origin::RouteEvent::Restart(_)) && !via_a(&update), "another instance over B: {update:?}" ); topology.group(); diff --git a/rs/moq-net/tests/integration/partial_group.rs b/rs/moq-net/tests/integration/partial_group.rs index db8c6266368..05c35951762 100644 --- a/rs/moq-net/tests/integration/partial_group.rs +++ b/rs/moq-net/tests/integration/partial_group.rs @@ -44,7 +44,8 @@ async fn round(version: &str) -> Option, moq_net::Error>> { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("room", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut live = kio::clock::timeout( TIMEOUT, remote diff --git a/rs/moq-net/tests/integration/quiet_catalog.rs b/rs/moq-net/tests/integration/quiet_catalog.rs index 9855715de07..3ea0b0a0801 100644 --- a/rs/moq-net/tests/integration/quiet_catalog.rs +++ b/rs/moq-net/tests/integration/quiet_catalog.rs @@ -59,7 +59,13 @@ fn publish(origin: &origin::Producer) -> (moq_net::origin::Publication, track::P async fn request(origin: &origin::Producer) -> broadcast::Consumer { let consumer = origin.consume(); - consumer.follow("live", None).unwrap().available().await.unwrap() + consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap() } /// Subscribe without a floor and expect the snapshot as group 0. diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index 8f3ead08c41..1034de14399 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -74,6 +74,7 @@ async fn read(version: Option<&str>, range: Range) -> Result, Error> { .await .expect("resolve timeout") .expect("broadcast resolves") + .unwrap() } }; diff --git a/rs/moq-net/tests/integration/range_fill.rs b/rs/moq-net/tests/integration/range_fill.rs index b31ba7bf89c..6792429dbd2 100644 --- a/rs/moq-net/tests/integration/range_fill.rs +++ b/rs/moq-net/tests/integration/range_fill.rs @@ -63,7 +63,14 @@ impl Fixture { options.server_publish = Some(publisher.consume()); options.client_subscribe = Some(copy.clone()); let pair = connect_mock(options).await; - let remote = copy.consume().follow("bcast", None).unwrap().available().await.unwrap(); + let remote = copy + .consume() + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); Self { track, remote, diff --git a/rs/moq-net/tests/integration/range_read.rs b/rs/moq-net/tests/integration/range_read.rs index 57f3d0ce720..e1b7a0c121a 100644 --- a/rs/moq-net/tests/integration/range_read.rs +++ b/rs/moq-net/tests/integration/range_read.rs @@ -97,7 +97,8 @@ async fn fixture( let remote = kio::clock::timeout(TIMEOUT, client.consume().follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); (remote, track, (sessions, broadcast, publisher, middle, client)) } diff --git a/rs/moq-net/tests/integration/received_rewind.rs b/rs/moq-net/tests/integration/received_rewind.rs index 038839bb37b..41767d1abca 100644 --- a/rs/moq-net/tests/integration/received_rewind.rs +++ b/rs/moq-net/tests/integration/received_rewind.rs @@ -75,7 +75,8 @@ async fn relay( let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let subscription = Subscription::default() .with_delay(Delay::default().with_max(BUDGET)) @@ -195,7 +196,8 @@ async fn fetch_only(version: &str) -> Vec, moq let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut got = Vec::new(); for sequence in [1, 0] { diff --git a/rs/moq-net/tests/integration/rejoin.rs b/rs/moq-net/tests/integration/rejoin.rs index bfa52354067..a2f5e9d313d 100644 --- a/rs/moq-net/tests/integration/rejoin.rs +++ b/rs/moq-net/tests/integration/rejoin.rs @@ -6,7 +6,7 @@ use crate::support; use std::time::Duration; -use moq_net::{Hop, Timestamp, Version, announce, group, track}; +use moq_net::{Hop, Timestamp, Version, group, origin, track}; use support::harness::{MockConnectOptions, connect_mock}; /// A millisecond track with no publisher max age: the frames written here carry timestamps, @@ -58,7 +58,13 @@ async fn rejoin_recovers_the_group_reset_on_leave() { let _pair = connect_mock(options).await; let consumer = relay.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let ts = |ms| Timestamp::from_millis(ms).unwrap(); let prefs = || { @@ -152,7 +158,13 @@ async fn rejoin_skips_a_cache_kept_by_another_handle() { let _pair = connect_mock(options).await; let consumer = relay.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let ts = |ms| Timestamp::from_millis(ms).unwrap(); // Held for fetches only: it keeps the relay's copy without subscribing. @@ -210,7 +222,13 @@ async fn rejoin_keeps_the_cache_when_the_backlog_lands_newest_first() { let pair = connect_mock(options).await; let consumer = relay.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let ts = |ms| Timestamp::from_millis(ms).unwrap(); let prefs = || track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(10))); @@ -292,7 +310,13 @@ async fn rejoin_mid_group_keeps_the_head_for_later_readers() { let _downstream = connect_mock(options).await; let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut sub = remote.track("video").unwrap().subscribe(None).await.unwrap(); let mut group = sub.recv_group().await.unwrap().unwrap(); assert_eq!(group.read_frame().await.unwrap().unwrap().payload, b"a0".as_ref()); @@ -363,7 +387,13 @@ async fn rejoin_goes_live_without_the_join_head() { let _downstream = connect_mock(options).await; let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut sub = remote.track("video").unwrap().subscribe(None).await.unwrap(); let mut group = sub.recv_group().await.unwrap().unwrap(); assert_eq!(group.read_frame().await.unwrap().unwrap().payload, b"a0".as_ref()); @@ -426,7 +456,13 @@ async fn rejoin_during_the_cancel_skips_the_cache() { let _pair = connect_mock(options).await; let consumer = relay.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let ts = |ms| Timestamp::from_millis(ms).unwrap(); let mut group = track.append_group().unwrap(); @@ -488,7 +524,13 @@ async fn rejoin_waits_for_the_answers_first_frame() { let pair = connect_mock(options).await; let consumer = relay.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let ts = |ms| Timestamp::from_millis(ms).unwrap(); // Held for fetches only: it keeps the relay's copy without subscribing. @@ -572,7 +614,13 @@ async fn leaving_after_the_cache_window_keeps_the_latest_group() { options.client_subscribe = Some(client.clone()); let pair = connect_mock(options).await; let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); (remote, pair) } }; @@ -634,7 +682,13 @@ async fn rejoin_keeps_source_timestamps() { let _pair = connect_mock(options).await; let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); // Far from any arrival time, so a frame stamped on arrival cannot pass. let ts = |ms| Timestamp::from_millis(ms).unwrap(); @@ -736,7 +790,13 @@ async fn quiet_catalog_survives_cache_expiry(version: Version, takeover: bool) { options.client_subscribe = Some(client.clone()); let pair = connect_mock(options).await; let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let sub = remote.track("catalog").unwrap().subscribe(None).await.unwrap(); (sub, remote, pair) } @@ -748,9 +808,9 @@ async fn quiet_catalog_survives_cache_expiry(version: Version, takeover: bool) { let (mut holder, remote, pair) = session(3).await; assert_eq!(holder.recv_group().await.unwrap().unwrap().sequence, 0, "{version}"); // The new connection is cheaper, so the relay reports its route taking over. - let mut follow = relay.consume().announced(); + let mut follow = relay.consume().routes(); assert!( - matches!(follow.next().await, Some(announce::Event::Start(_))), + matches!(follow.next().await, Some(origin::RouteEvent::Start(_))), "{version}" ); let replacement = upstream(1).await; @@ -758,7 +818,11 @@ async fn quiet_catalog_survives_cache_expiry(version: Version, takeover: bool) { assert!( matches!( event, - Some(announce::Event::Update(_) | announce::Event::Restart(_) | announce::Event::Takeover { .. }) + Some( + origin::RouteEvent::Update(_) + | origin::RouteEvent::Restart(_) + | origin::RouteEvent::Takeover { .. } + ) ), "{version}: {event:?}" ); diff --git a/rs/moq-net/tests/integration/relay_upstream_close.rs b/rs/moq-net/tests/integration/relay_upstream_close.rs index 7d2785426dc..8ec827887b2 100644 --- a/rs/moq-net/tests/integration/relay_upstream_close.rs +++ b/rs/moq-net/tests/integration/relay_upstream_close.rs @@ -120,7 +120,8 @@ async fn round(name: &str, end: End) -> Vec { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut readers = Vec::new(); for (index, track) in tracks.iter().enumerate() { diff --git a/rs/moq-net/tests/integration/replacement.rs b/rs/moq-net/tests/integration/replacement.rs index 601628ee9b7..15006530fe8 100644 --- a/rs/moq-net/tests/integration/replacement.rs +++ b/rs/moq-net/tests/integration/replacement.rs @@ -14,7 +14,7 @@ use crate::support; use std::time::Duration; -use moq_net::{Error, Hop, Instance, Timestamp, Version, announce, broadcast, origin, track}; +use moq_net::{Error, Hop, Instance, Timestamp, Version, broadcast, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; /// A millisecond track: the frames written here carry timestamps. @@ -39,7 +39,7 @@ async fn link(version: Version, from: &origin::Producer, to: &origin::Producer) } /// The next announce event, failing on a hang. -async fn next_event(announced: &mut announce::Consumer) -> announce::Event { +async fn next_event(announced: &mut origin::Routes) -> origin::RouteEvent { kio::clock::timeout(Duration::from_secs(10), announced.next()) .await .expect("announce hung") @@ -90,9 +90,9 @@ async fn replace(version: &str, instance: Option) { let meta = cam.create_track("meta", timed()).unwrap(); let _first = link(version, &publisher, &viewer).await; - let mut announced = viewer.consume().announced(); + let mut announced = viewer.consume().routes(); match next_event(&mut announced).await { - announce::Event::Start(announce) => { + origin::RouteEvent::Start(announce) => { assert_eq!(announce.prefix.as_str(), "cam"); let carried = announce.route.instance.as_ref().expect("every route has an instance"); assert_eq!(carried.is_minted(), !carries, "{version}: minted {carried}"); @@ -131,7 +131,7 @@ async fn replace(version: &str, instance: Option) { // A minted instance makes the replacement a restart, delivered at once, and the old // handle refuses new requests from then on. match announced.try_next() { - Some(announce::Event::Restart(announce)) => assert_eq!(announce.prefix.as_str(), "cam"), + Some(origin::RouteEvent::Restart(announce)) => assert_eq!(announce.prefix.as_str(), "cam"), other => panic!("{version}: expected the restart, got {other:?}"), } let Err(err) = subscribe(&old, "meta").await else { @@ -199,7 +199,7 @@ async fn route_death(version: &str, relayed: bool, wait: Duration) { true => Some(link(version, &relay, &viewer).await), false => None, }; - let mut announced = viewer.consume().announced(); + let mut announced = viewer.consume().routes(); next_event(&mut announced).await; let old = viewer.consume().request("cam", None).await.unwrap(); @@ -276,7 +276,7 @@ async fn restart_while_the_old_session_lingers(version: &str, old_hop: u64, new_ let old_cam = old.publish("cam", origin::Route::default()).unwrap(); let _old_video = old_cam.create_track("video", timed()).unwrap(); let old_session = link(version, &old, &viewer).await; - let mut announced = viewer.consume().announced(); + let mut announced = viewer.consume().routes(); next_event(&mut announced).await; // The old process dies without a close: its session falls silent. diff --git a/rs/moq-net/tests/integration/request_caps.rs b/rs/moq-net/tests/integration/request_caps.rs index 165552593fa..03ff43f92e4 100644 --- a/rs/moq-net/tests/integration/request_caps.rs +++ b/rs/moq-net/tests/integration/request_caps.rs @@ -78,6 +78,7 @@ async fn closed_requests_are_granted_back() { ) .await .expect("announce timeout") + .unwrap() .unwrap(); for round in 0..ROUNDS { @@ -128,6 +129,7 @@ async fn flow_control_bounds_the_subscriptions_a_peer_holds() { let remote = kio::clock::timeout(TIMEOUT, client.consume().follow("bcast", None).unwrap().available()) .await .expect("announce timeout") + .unwrap() .unwrap(); let subscribing: Vec<_> = (0..TRACKS) .map(|i| { diff --git a/rs/moq-net/tests/integration/route_change.rs b/rs/moq-net/tests/integration/route_change.rs index 96a72f80329..2052cade2ce 100644 --- a/rs/moq-net/tests/integration/route_change.rs +++ b/rs/moq-net/tests/integration/route_change.rs @@ -22,7 +22,7 @@ use std::collections::BTreeMap; use std::time::Duration; use futures::{StreamExt, channel::mpsc}; -use moq_net::{Error, Hop, Timestamp, Version, announce, origin, track}; +use moq_net::{Error, Hop, Timestamp, Version, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; /// A millisecond track with no publisher max age: the frames written here carry timestamps, @@ -132,7 +132,13 @@ impl Topology { let a_to_r = link(version, &relay_a, &subscriber).await; let consumer = subscriber.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(60))); let sub = remote.track("video").unwrap().subscribe(prefs).await.unwrap(); @@ -404,13 +410,18 @@ async fn route_dies_across_instances(version: &str, trigger: Trigger) { let a_to_r = link(version, &relay_a, &relay).await; let _r_to_d = link(version, &relay, &downstream).await; let consumer = downstream.consume(); - let mut announced = consumer.announced(); + // Restart versus end and start is wire-level, which only the route stream shows. + let mut announced = consumer.routes(); match announced.next().await.expect("announced") { - announce::Event::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), + origin::RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), other => panic!("{version}: expected the initial start, got {other:?}"), } let mut follow = consumer.follow("live", None).unwrap(); - let first = follow.available().await.unwrap(); + let first = follow + .available() + .await + .unwrap() + .expect("the first change is an instance"); let sub = first.track("video").unwrap().subscribe(None).await.unwrap(); let mut rx = read(sub); @@ -441,7 +452,7 @@ async fn route_dies_across_instances(version: &str, trigger: Trigger) { // while lite-07 carries ANNOUNCE_RESTART. An in-place update must fail here. if version.alpn() == "moq-lite-07-wip" { match announced.next().await.expect("announced") { - announce::Event::Restart(announce) => { + origin::RouteEvent::Restart(announce) => { assert_eq!(announce.prefix.as_str(), "live"); assert_eq!( announce.route.instance, @@ -453,11 +464,11 @@ async fn route_dies_across_instances(version: &str, trigger: Trigger) { } } else { match announced.next().await.expect("announced") { - announce::Event::End(announce) => assert_eq!(announce.prefix.as_str(), "live"), + origin::RouteEvent::End(announce) => assert_eq!(announce.prefix.as_str(), "live"), other => panic!("{version} {trigger:?}: expected an end, got {other:?}"), } match announced.next().await.expect("announced") { - announce::Event::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), + origin::RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), other => panic!("{version} {trigger:?}: expected a fresh start, got {other:?}"), } } @@ -466,7 +477,17 @@ async fn route_dies_across_instances(version: &str, trigger: Trigger) { matches!(rx.next().await, Some((_, Err(_)))), "{version} {trigger:?}: the old subscription must fail" ); - let second = follow.available().await.expect("B serves a re-request"); + // Older wires carry the switch as an end then a start, which the follower reports as the + // path going offline before B. + if version.alpn() != "moq-lite-07-wip" { + let ended = follow.available().await.expect("the follow stays open"); + assert!(ended.is_none(), "{version} {trigger:?}: expected the end first"); + } + let second = follow + .available() + .await + .expect("B serves a re-request") + .expect("B, not an end"); assert!(!second.is_clone(&first), "{version}: joined the replaced broadcast"); let sub = second.track("video").unwrap().subscribe(None).await.unwrap(); let mut rx = read(sub); @@ -484,9 +505,9 @@ async fn better_route_restarts(version: &str) { let mut rx = read(sub); topology.standby().await; let consumer = topology.subscriber.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); match announced.next().await.expect("announced") { - announce::Event::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), + origin::RouteEvent::Start(announce) => assert_eq!(announce.prefix.as_str(), "live"), other => panic!("{version}: expected the route, got {other:?}"), } @@ -497,7 +518,7 @@ async fn better_route_restarts(version: &str) { topology.trigger(Trigger::BetterRoute).await; match announced.next().await.expect("announced") { - announce::Event::Restart(announce) => { + origin::RouteEvent::Restart(announce) => { assert_eq!(announce.prefix.as_str(), "live"); assert_eq!(announce.route.hops.len(), 1, "{version}: the direct route"); } @@ -662,7 +683,13 @@ async fn lagging_route_dies(version: Version) { } async fn subscribe(origin: &origin::Producer) -> track::Subscriber { let consumer = origin.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let preferences = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(60))); remote.track("video").unwrap().subscribe(preferences).await.unwrap() @@ -779,7 +806,13 @@ async fn undetected_outage(budget: Duration) -> BTreeMap> { let m1_e = link(version, &m1, &e).await; let consumer = e.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(budget)); let mut rx = read(remote.track("video").unwrap().subscribe(prefs).await.unwrap()); let m2_e = priced_link(version, &m2, &e, Some(1_000)).await; @@ -921,7 +954,7 @@ async fn redundant_pair_follows_the_next_instance(version: &str, loss: Loss) { settle().await; let mut follow = subscriber.consume().follow("live", None).unwrap(); - let first = follow.available().await.unwrap(); + let first = follow.available().await.unwrap().unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(60))); let mut rx = read(first.track("video").unwrap().subscribe(prefs.clone()).await.unwrap()); let write = |replicas: &mut Vec, sequence: u64| { @@ -958,7 +991,7 @@ async fn redundant_pair_follows_the_next_instance(version: &str, loss: Loss) { drop(broadcast); settle().await; - let second = follow.available().await.unwrap(); + let second = follow.available().await.unwrap().unwrap(); assert!( !second.is_clone(&first), "{version} {loss:?}: handed out the old broadcast" @@ -1034,7 +1067,13 @@ async fn replaced_behind_a_relay(version: &str) { let _r_to_d = link(version, &relay, &downstream).await; let consumer = downstream.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(60))); let mut rx = read(remote.track("video").unwrap().subscribe(prefs).await.unwrap()); @@ -1131,7 +1170,13 @@ async fn compression_changes_behind_a_relay(compress: bool) { let _r_to_d = link(version, &relay, &downstream).await; let consumer = downstream.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(60))); let mut rx = read(remote.track("video").unwrap().subscribe(prefs).await.unwrap()); @@ -1225,7 +1270,13 @@ async fn producer_replaced(version: &str) { let _a_to_r = link(version, &relay, &subscriber).await; let consumer = subscriber.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(60))); let sub = remote.track("video").unwrap().subscribe(prefs).await.unwrap(); let mut rx = read(sub); diff --git a/rs/moq-net/tests/integration/route_health.rs b/rs/moq-net/tests/integration/route_health.rs index 38c0cb976f7..bd9ba49a735 100644 --- a/rs/moq-net/tests/integration/route_health.rs +++ b/rs/moq-net/tests/integration/route_health.rs @@ -65,7 +65,13 @@ async fn silent_kill(budget: Duration, detect: Duration) -> Vec { let m1_e = link(version, &m1, &e).await; let consumer = e.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(budget)); let mut rx = read(remote.track("video").unwrap().subscribe(prefs).await.unwrap()); let m2_e = priced_link(version, &m2, &e, Some(1_000)).await; @@ -204,7 +210,13 @@ async fn payload_stall(stall: Stall, span: Duration) -> (Observed, Observed) { async fn subscribe(origin: &origin::Producer) -> track::Subscriber { let consumer = origin.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(2))); remote.track("video").unwrap().subscribe(prefs).await.unwrap() } diff --git a/rs/moq-net/tests/integration/segmented_frame.rs b/rs/moq-net/tests/integration/segmented_frame.rs index c87d4c7db06..a5a8e993db6 100644 --- a/rs/moq-net/tests/integration/segmented_frame.rs +++ b/rs/moq-net/tests/integration/segmented_frame.rs @@ -43,6 +43,7 @@ async fn a_segmented_frame_crosses_whole() { let remote = kio::clock::timeout(TIMEOUT, viewer.consume().follow("room", None).unwrap().available()) .await .expect("broadcast timeout") + .unwrap() .unwrap(); let mut sub = remote.track("file").unwrap().subscribe(None).await.unwrap(); subscribed(&track).await; diff --git a/rs/moq-net/tests/integration/session_close.rs b/rs/moq-net/tests/integration/session_close.rs index 21d677f6ef7..9793f301523 100644 --- a/rs/moq-net/tests/integration/session_close.rs +++ b/rs/moq-net/tests/integration/session_close.rs @@ -57,7 +57,8 @@ async fn setup_with_gate(version: &str, gate: Option>) -> Se let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); // The publisher only learns of a subscription once the subscriber polls it. let (ready, active) = futures::channel::oneshot::channel(); @@ -113,7 +114,13 @@ async fn track_info_interest_precedes_a_subscription() { options.client_subscribe = Some(subscriber.clone()); let _pair = connect_mock(options).await; let consumer = subscriber.consume(); - let remote = consumer.follow("bcast", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bcast", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let copy = remote.track("video").unwrap(); copy.info().await.expect("TRACK_INFO"); crate::support::wanted(track.demand()) diff --git a/rs/moq-net/tests/integration/session_idle.rs b/rs/moq-net/tests/integration/session_idle.rs index 542d7bde485..0d200234a21 100644 --- a/rs/moq-net/tests/integration/session_idle.rs +++ b/rs/moq-net/tests/integration/session_idle.rs @@ -52,7 +52,8 @@ async fn setup(version: &str) -> Setup { let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); Setup { pair, diff --git a/rs/moq-net/tests/integration/session_panic.rs b/rs/moq-net/tests/integration/session_panic.rs index 9f0f34dbec5..ecd374fb214 100644 --- a/rs/moq-net/tests/integration/session_panic.rs +++ b/rs/moq-net/tests/integration/session_panic.rs @@ -13,7 +13,7 @@ use crate::support; use std::time::Duration; -use moq_net::{Client, Hop, Server, Session, Version, announce, origin}; +use moq_net::{Client, Hop, Server, Session, Version, origin}; use support::harness::{now, spawn}; use support::mock::{MockSession, create_mock_session_pair}; @@ -77,15 +77,15 @@ async fn publish(version: Version, relay: &origin::Producer, hop: u64, name: &st } /// Wait until `path` is announced (`true`) or withdrawn (`false`) on `announced`. -async fn until(announced: &mut announce::Consumer, path: &str, active: bool) { +async fn until(announced: &mut origin::Routes, path: &str, active: bool) { let wait = async { loop { let (announce, now_active) = match announced.next().await.expect("origin closed") { - announce::Event::Start(announce) - | announce::Event::Update(announce) - | announce::Event::Restart(announce) - | announce::Event::Takeover { announce, .. } => (announce, true), - announce::Event::End(announce) => (announce, false), + origin::RouteEvent::Start(announce) + | origin::RouteEvent::Update(announce) + | origin::RouteEvent::Restart(announce) + | origin::RouteEvent::Takeover { announce, .. } => (announce, true), + origin::RouteEvent::End(announce) => (announce, false), }; if announce.prefix.as_str() == path && now_active == active { return; @@ -102,7 +102,7 @@ async fn a_panicking_session_leaves_the_others_serving() { for version in ["moq-lite-07-wip", "moq-transport-22"] { let version: Version = version.parse().unwrap(); let relay = produce_origin(100); - let mut announced = relay.consume().announced(); + let mut announced = relay.consume().routes(); let broken = publish(version, &relay, 1, "broken").await; until(&mut announced, "broken", true).await; diff --git a/rs/moq-net/tests/integration/source_errors.rs b/rs/moq-net/tests/integration/source_errors.rs index 5aecf07e94d..c85b21983a2 100644 --- a/rs/moq-net/tests/integration/source_errors.rs +++ b/rs/moq-net/tests/integration/source_errors.rs @@ -44,6 +44,7 @@ async fn resolve(origin: &moq_net::origin::Producer) -> moq_net::broadcast::Cons .await .expect("resolve timeout") .expect("broadcast resolves") + .unwrap() } async fn relayed(version: &str) -> Relayed { diff --git a/rs/moq-net/tests/integration/stream_credit.rs b/rs/moq-net/tests/integration/stream_credit.rs index 95690bfbe0e..88189608ceb 100644 --- a/rs/moq-net/tests/integration/stream_credit.rs +++ b/rs/moq-net/tests/integration/stream_credit.rs @@ -43,6 +43,7 @@ async fn one_stream_credit_serves_every_backlogged_group() { .unwrap() .available() .await + .unwrap() .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(30))); @@ -106,6 +107,7 @@ async fn a_subscription_past_the_credit_is_served_once_credit_returns() { .unwrap() .available() .await + .unwrap() .unwrap(); let prefs = track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_secs(30))); diff --git a/rs/moq-net/tests/integration/subscribe_priority.rs b/rs/moq-net/tests/integration/subscribe_priority.rs index e71b5e77ade..65ba7482252 100644 --- a/rs/moq-net/tests/integration/subscribe_priority.rs +++ b/rs/moq-net/tests/integration/subscribe_priority.rs @@ -68,7 +68,8 @@ async fn follows(version: Version, relay: bool) -> Result<(), String> { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let subscribe = |priority: i8| { let track = remote.track("video").unwrap(); async move { diff --git a/rs/moq-net/tests/integration/subscribe_ranges.rs b/rs/moq-net/tests/integration/subscribe_ranges.rs index 9b65ffcd540..30d0ea13136 100644 --- a/rs/moq-net/tests/integration/subscribe_ranges.rs +++ b/rs/moq-net/tests/integration/subscribe_ranges.rs @@ -101,6 +101,7 @@ async fn ahead_then_live(version: Option<&str>, relay: bool) -> (Vec, Vec Rang options.client_subscribe = Some(subscriber.clone()); sessions.push(connect_mock(options).await); let consumer = subscriber.consume(); - let remote = consumer.follow("ranges", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("ranges", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); RangeFixture { track, remote, @@ -762,7 +770,8 @@ async fn a_covered_bounded_subscription_stays_open() { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let bounded = |range: std::ops::Range| { Subscription::default() diff --git a/rs/moq-net/tests/integration/subscribe_tracks.rs b/rs/moq-net/tests/integration/subscribe_tracks.rs index 821c21b6f10..9425c44b6db 100644 --- a/rs/moq-net/tests/integration/subscribe_tracks.rs +++ b/rs/moq-net/tests/integration/subscribe_tracks.rs @@ -63,7 +63,8 @@ async fn setup(version: Version) -> Setup { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("room", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let (tx, frames) = futures::channel::mpsc::unbounded(); kio::clock::sim::spawn(async move { diff --git a/rs/moq-net/tests/integration/subscribe_with_track.rs b/rs/moq-net/tests/integration/subscribe_with_track.rs index 8e1eeea42c5..7f5e945a173 100644 --- a/rs/moq-net/tests/integration/subscribe_with_track.rs +++ b/rs/moq-net/tests/integration/subscribe_with_track.rs @@ -79,7 +79,13 @@ async fn first_frame(version: &str, relays: usize, arrival: Arrival) -> Duration broadcast.announce(Default::default()).unwrap(); let consumer = nodes.last().unwrap().consume(); - let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); if !matches!(arrival, Arrival::Together) { reader.hold_bidis(); diff --git a/rs/moq-net/tests/integration/subscriber_max_age.rs b/rs/moq-net/tests/integration/subscriber_max_age.rs index 35b83a339f0..ee474e46162 100644 --- a/rs/moq-net/tests/integration/subscriber_max_age.rs +++ b/rs/moq-net/tests/integration/subscriber_max_age.rs @@ -51,7 +51,13 @@ async fn relay_forwards_the_most_tolerant_subscriber_max_age() { group.finish().unwrap(); broadcast.announce(Default::default()).unwrap(); let consumer = viewer.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let cache = remote.track("catalog").unwrap(); let prefs = |millis| { track::Subscription::default().with_delay(track::Delay::default().with_max(Duration::from_millis(millis))) @@ -126,6 +132,7 @@ async fn relay_forwards_the_smallest_stated_min_delay() { .unwrap() .available() .await + .unwrap() .unwrap(); remote.track("catalog").unwrap() } diff --git a/rs/moq-net/tests/integration/subscription_end_integrity.rs b/rs/moq-net/tests/integration/subscription_end_integrity.rs index efc605ff202..6a2ec2d1daf 100644 --- a/rs/moq-net/tests/integration/subscription_end_integrity.rs +++ b/rs/moq-net/tests/integration/subscription_end_integrity.rs @@ -70,7 +70,8 @@ async fn round(drop_session: bool) -> (Vec>, Option) { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); // The publisher only learns of a subscription once the subscriber polls it, so the // reader runs concurrently. It reports the head group drained, then waits to be told @@ -229,7 +230,8 @@ async fn killed(version: &str, kill: Kill) -> (Option, Option moq_net::Error { .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut fetching = kio::clock::sim::spawn(async move { remote.track("video").unwrap().fetch_group(0, None).await }); let request = dynamic.requested_track().await.expect("the track request arrives"); diff --git a/rs/moq-net/tests/integration/support/harness.rs b/rs/moq-net/tests/integration/support/harness.rs index 5941243f948..3a1fefabed3 100644 --- a/rs/moq-net/tests/integration/support/harness.rs +++ b/rs/moq-net/tests/integration/support/harness.rs @@ -148,7 +148,7 @@ pub struct MockPair { /// The server's end of the mock transport, for steering delivery. pub server_transport: MockSession, /// See [`MockConnectOptions::client_discovers_all`]. - pub client_discovery: Option, + pub client_discovery: Option, } /// Run the MoQ handshake over the mock transport, returning connected sessions. @@ -188,7 +188,7 @@ pub async fn connect_mock(opts: MockConnectOptions) -> MockPair { .client_subscribe .as_ref() .filter(|_| opts.client_discovers_all) - .map(|subscribe| subscribe.consume().announced()); + .map(|subscribe| subscribe.consume().routes()); if let Some(subscribe) = opts.client_subscribe { client = client.with_subscriber(subscribe); } diff --git a/rs/moq-net/tests/integration/support/mod.rs b/rs/moq-net/tests/integration/support/mod.rs index 3ee59309894..8cbc4ddf5c3 100644 --- a/rs/moq-net/tests/integration/support/mod.rs +++ b/rs/moq-net/tests/integration/support/mod.rs @@ -3,7 +3,7 @@ pub mod harness; pub mod mock; -use moq_net::{announce, datagram, goaway, origin, track}; +use moq_net::{datagram, goaway, origin, track}; /// Wait until anyone wants the track, returning the group they want. #[allow(dead_code, reason = "shared by several test binaries, each using only part of it")] @@ -35,12 +35,12 @@ pub async fn datagram_priority(producer: &datagram::Producer, want: Option) /// Wait until `consumer` sees a route announced at exactly `path`, and return it. #[allow(dead_code, reason = "shared by several test binaries, each using only part of it")] pub async fn announced(consumer: &origin::Consumer, path: &str) -> origin::Route { - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); loop { match announced.next().await.expect("the origin closed") { - announce::Event::Start(announce) - | announce::Event::Update(announce) - | announce::Event::Restart(announce) + origin::RouteEvent::Start(announce) + | origin::RouteEvent::Update(announce) + | origin::RouteEvent::Restart(announce) if announce.prefix.as_str() == path => { return announce.route; diff --git a/rs/moq-net/tests/integration/synced.rs b/rs/moq-net/tests/integration/synced.rs index e623627de9c..2394a2de908 100644 --- a/rs/moq-net/tests/integration/synced.rs +++ b/rs/moq-net/tests/integration/synced.rs @@ -7,7 +7,7 @@ use std::collections::BTreeSet; use std::time::Duration; -use moq_net::{Hop, Version, announce, origin}; +use moq_net::{Hop, Version, origin}; use crate::support; use support::harness::{MockConnectOptions, connect_mock}; @@ -22,7 +22,7 @@ fn produce_origin(hop: u64) -> origin::Producer { } /// Read events until the consumer syncs, returning the prefixes then announced. -async fn read_until_synced(announced: &mut announce::Consumer) -> BTreeSet { +async fn read_until_synced(announced: &mut origin::Routes) -> BTreeSet { let mut seen = BTreeSet::new(); while !announced.poll_synced(&moq_net::kio::Waiter::noop()).is_ready() { let event = kio::clock::timeout(SETTLE, announced.next()) @@ -30,15 +30,15 @@ async fn read_until_synced(announced: &mut announce::Consumer) -> BTreeSet { + origin::RouteEvent::Start(announce) + | origin::RouteEvent::Restart(announce) + | origin::RouteEvent::Takeover { announce, .. } => { seen.insert(announce.prefix.to_string()); } - announce::Event::End(announce) => { + origin::RouteEvent::End(announce) => { seen.remove(announce.prefix.as_str()); } - announce::Event::Update(_) => {} + origin::RouteEvent::Update(_) => {} } } seen @@ -59,7 +59,7 @@ async fn synced(version: &str) -> BTreeSet { options.client_subscribe = Some(subscriber.clone()); let _pair = connect_mock(options).await; - let mut announced = subscriber.consume().announced(); + let mut announced = subscriber.consume().routes(); read_until_synced(&mut announced).await } @@ -105,7 +105,7 @@ async fn a_second_session_catches_up() { connect_mock(options) }; let first = connect().await; - let mut announced = subscriber.consume().announced(); + let mut announced = subscriber.consume().routes(); assert_eq!( read_until_synced(&mut announced).await, ["a"].into_iter().map(String::from).collect() @@ -128,7 +128,7 @@ async fn a_cold_relay_syncs_then_forwards_upstream_routes_as_live() { downstream.server_publish = Some(relay.consume()); downstream.client_subscribe = Some(subscriber.clone()); let _downstream = connect_mock(downstream).await; - let mut announced = subscriber.consume().announced(); + let mut announced = subscriber.consume().routes(); moq_net::kio::wait(|waiter| announced.poll_synced(waiter)).await; assert!(announced.try_next().is_none()); @@ -140,6 +140,6 @@ async fn a_cold_relay_syncs_then_forwards_upstream_routes_as_live() { options.client_subscribe = Some(relay.clone()); let _upstream = connect_mock(options).await; let event = announced.next().await.unwrap(); - assert!(matches!(event, announce::Event::Start(route) if route.prefix.as_str() == "room/cam")); + assert!(matches!(event, origin::RouteEvent::Start(route) if route.prefix.as_str() == "room/cam")); assert!(announced.poll_synced(&moq_net::kio::Waiter::noop()).is_ready()); } diff --git a/rs/moq-net/tests/integration/track_abort_over_session.rs b/rs/moq-net/tests/integration/track_abort_over_session.rs index 538a9afa8fc..c2475a7b26c 100644 --- a/rs/moq-net/tests/integration/track_abort_over_session.rs +++ b/rs/moq-net/tests/integration/track_abort_over_session.rs @@ -38,7 +38,13 @@ async fn after_abort(version: &str, hops: u32) { options.client_subscribe = Some(far.clone()); let _pair2 = connect_mock(options).await; let consumer = if hops == 2 { far.consume() } else { relay.consume() }; - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut sub = remote.track("video").unwrap().subscribe(None).await.unwrap(); let mut open = track.append_group().unwrap(); open.write_frame(Timestamp::ZERO, b"head".as_ref()).unwrap(); diff --git a/rs/moq-net/tests/integration/track_info_cancel.rs b/rs/moq-net/tests/integration/track_info_cancel.rs index 250c8fa4cf9..bf9fe31178a 100644 --- a/rs/moq-net/tests/integration/track_info_cancel.rs +++ b/rs/moq-net/tests/integration/track_info_cancel.rs @@ -87,6 +87,7 @@ async fn withdrawn_broadcast_releases_track_info(version: &str, answered: bool, .unwrap() .available() .await + .unwrap() .unwrap(); let mut query = remote.track("video").unwrap().info(); let request = match futures::future::select(std::pin::pin!(dynamic.requested_track()), &mut query).await { diff --git a/rs/moq-net/tests/integration/track_stream_demand.rs b/rs/moq-net/tests/integration/track_stream_demand.rs index 4af2c4c0143..1aaf88560b4 100644 --- a/rs/moq-net/tests/integration/track_stream_demand.rs +++ b/rs/moq-net/tests/integration/track_stream_demand.rs @@ -103,7 +103,13 @@ async fn one_used_edge(version: &str, relays: usize, reorder: bool) { }); let consumer = nodes.last().unwrap().consume(); - let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let subscribing = remote.track("video").unwrap().subscribe(None); if reorder { @@ -190,7 +196,13 @@ async fn bare_handle_is_lazy(version: &str, relays: usize) { }); let consumer = nodes.last().unwrap().consume(); - let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let handle = remote.track("video").unwrap(); kio::clock::sleep(Duration::from_secs(1)).await; diff --git a/rs/moq-net/tests/integration/track_tail.rs b/rs/moq-net/tests/integration/track_tail.rs index 6f3db524a20..6235bbed4f4 100644 --- a/rs/moq-net/tests/integration/track_tail.rs +++ b/rs/moq-net/tests/integration/track_tail.rs @@ -97,7 +97,8 @@ async fn connect(version: &str) -> Pair { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("bcast", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); Pair { pair, diff --git a/rs/moq-net/tests/integration/tracks.rs b/rs/moq-net/tests/integration/tracks.rs index 1f1b34fa060..e394273b83c 100644 --- a/rs/moq-net/tests/integration/tracks.rs +++ b/rs/moq-net/tests/integration/tracks.rs @@ -57,6 +57,7 @@ async fn resolve(origin: &moq_net::origin::Producer) -> moq_net::broadcast::Cons .await .expect("announce timeout") .expect("routed") + .unwrap() } /// A session syncs at once, then lists the peer's tracks and their changes until the diff --git a/rs/moq-net/tests/integration/tracks_ietf.rs b/rs/moq-net/tests/integration/tracks_ietf.rs index 6d437b3f8e8..4693717ffd1 100644 --- a/rs/moq-net/tests/integration/tracks_ietf.rs +++ b/rs/moq-net/tests/integration/tracks_ietf.rs @@ -62,6 +62,7 @@ async fn resolve(origin: &moq_net::origin::Producer, path: &str) -> moq_net::bro .await .expect("announce timeout") .expect("routed") + .unwrap() } /// How many request streams of `kind` the client has opened. diff --git a/rs/moq-net/tests/integration/unannounce_release.rs b/rs/moq-net/tests/integration/unannounce_release.rs index bd660de8e49..e86543f5af1 100644 --- a/rs/moq-net/tests/integration/unannounce_release.rs +++ b/rs/moq-net/tests/integration/unannounce_release.rs @@ -33,7 +33,8 @@ async fn release(version: Version) { .unwrap() .available() .await - .expect("resolves"); + .expect("resolves") + .unwrap(); let sub = remote.track("t").unwrap().subscribe(None).await.expect("subscribes"); crate::support::wanted(track.demand()).await.unwrap(); diff --git a/rs/moq-net/tests/integration/warm_backfill.rs b/rs/moq-net/tests/integration/warm_backfill.rs index c28162fd322..5edae666fc1 100644 --- a/rs/moq-net/tests/integration/warm_backfill.rs +++ b/rs/moq-net/tests/integration/warm_backfill.rs @@ -36,7 +36,13 @@ fn origin(hop: u64) -> origin::Producer { async fn subscribe(relay: &origin::Producer) -> track::Subscriber { let consumer = relay.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); remote .track("video") .unwrap() diff --git a/rs/moq-net/tests/integration/wide_sequence.rs b/rs/moq-net/tests/integration/wide_sequence.rs index 3dfb14cd918..f17fb410fb4 100644 --- a/rs/moq-net/tests/integration/wide_sequence.rs +++ b/rs/moq-net/tests/integration/wide_sequence.rs @@ -41,6 +41,7 @@ async fn largest_quic_sequence_reaches_the_subscriber() { .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = remote.track("data").unwrap().subscribe(None).await.unwrap(); for sequence in [5, 1 << 32, (1 << 53) - 1, (1 << 53) + 1, (1 << 62) - 1] { @@ -111,12 +112,19 @@ async fn wide_sequence_fails_only_its_subscription() { .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = cached.track("wide").unwrap().subscribe(None).await.unwrap(); assert_eq!(sub.recv_group().await.unwrap().unwrap().sequence, WIDE); let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let result = kio::clock::timeout(Duration::from_secs(5), async { let mut sub = remote.track("wide").unwrap().subscribe(None).await?; @@ -165,7 +173,13 @@ async fn a_later_wide_group_fails_only_that_subscription() { let _downstream = connect_mock(options).await; let consumer = client.consume(); - let remote = consumer.follow("bench", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("bench", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut sub = remote.track("wide").unwrap().subscribe(None).await.unwrap(); let mut group = sub.recv_group().await.unwrap().unwrap(); diff --git a/rs/moq-net/tests/integration/widened_max_age.rs b/rs/moq-net/tests/integration/widened_max_age.rs index b21e8b9273e..b313716f671 100644 --- a/rs/moq-net/tests/integration/widened_max_age.rs +++ b/rs/moq-net/tests/integration/widened_max_age.rs @@ -113,7 +113,13 @@ impl Chain { /// Subscribe from the viewer with `budget`, reading from the oldest group still around. async fn subscribe(&self, budget: Duration) -> track::Subscriber { let consumer = self.viewer.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let subscription = Subscription::default() .with_delay(Delay::default().with_max(budget)) .with_ranges([Range::oldest()]); @@ -318,7 +324,13 @@ async fn wider_newcomers_after_the_first_pull_nothing_upstream() { let viewer = origin(3); links.push(peer(version, &relay, &viewer).await); let consumer = viewer.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let subscription = Subscription::default() .with_delay(Delay::default().with_max(budget)) .with_ranges([Range::oldest()]); diff --git a/rs/moq-net/tests/integration/zero_copy.rs b/rs/moq-net/tests/integration/zero_copy.rs index b8888660444..3779106d2eb 100644 --- a/rs/moq-net/tests/integration/zero_copy.rs +++ b/rs/moq-net/tests/integration/zero_copy.rs @@ -63,7 +63,8 @@ async fn frames_cross_without_a_copy() { let remote = kio::clock::timeout(TIMEOUT, consumer.follow("room", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut sub = remote.track("video").unwrap().subscribe(None).await.expect("subscribe"); subscribed(&track).await; diff --git a/rs/moq-net/tests/send.rs b/rs/moq-net/tests/send.rs index b96177ff106..f0fa6daae6f 100644 --- a/rs/moq-net/tests/send.rs +++ b/rs/moq-net/tests/send.rs @@ -3,7 +3,7 @@ fn require_send(_: impl Send) {} -async fn watch_announcements(mut consumer: moq_net::announce::Consumer) { +async fn watch_announcements(mut consumer: moq_net::origin::Routes) { while let Some(event) = consumer.next().await { drop(event); } @@ -23,7 +23,7 @@ async fn read_frames(mut consumer: moq_net::group::Consumer) { #[test] fn announcement_next_is_send() { - let _: fn(moq_net::announce::Consumer) = |consumer| { + let _: fn(moq_net::origin::Routes) = |consumer| { require_send(watch_announcements(consumer)); }; } diff --git a/rs/moq-relay/benches/session.rs b/rs/moq-relay/benches/session.rs index 730adc32264..f29ee2e894b 100644 --- a/rs/moq-relay/benches/session.rs +++ b/rs/moq-relay/benches/session.rs @@ -93,7 +93,7 @@ impl Backend for Relay { .client_subscribe .as_ref() .filter(|_| options.client_discovers_all) - .map(|origin| origin.consume().announced()); + .map(|origin| origin.consume().routes()); let mut client = moq_net::Client::new() .with_versions(version.into()) .with_extensions(options.client_extensions); diff --git a/rs/moq-relay/src/cluster.rs b/rs/moq-relay/src/cluster.rs index 5686ddc3818..60eee46dabe 100644 --- a/rs/moq-relay/src/cluster.rs +++ b/rs/moq-relay/src/cluster.rs @@ -2529,7 +2529,7 @@ mod tests { false => cluster.scope(lease, &request), } .expect("admitted"); - let mut announced = admitted.subscriber.expect("subscribe grant").announced(); + let mut announced = admitted.subscriber.expect("subscribe grant").routes(); let mut seen = Vec::new(); while let Some(route) = try_next_announced(&mut announced) { seen.push(route.prefix.to_string()); @@ -2547,20 +2547,20 @@ mod tests { /// The next route and whether it is active. #[cfg(feature = "cluster-lan")] - async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { + async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } /// The next announcement without blocking. - fn try_next_announced(announced: &mut moq_net::announce::Consumer) -> Option { + fn try_next_announced(announced: &mut moq_net::origin::Routes) -> Option { match announced.try_next()? { - moq_net::announce::Event::Start(route) => Some(route), + moq_net::origin::RouteEvent::Start(route) => Some(route), other => panic!("expected an announcement: got {other:?}"), } } @@ -2650,7 +2650,15 @@ mod tests { let (cluster, driver) = Cluster::new(with_stats()).unwrap(); let consumer = cluster.origin.consume(); let path = Path::from_static(".stats/node"); - let announced = async { consumer.follow(&path, None).unwrap().available().await.unwrap() }; + let announced = async { + consumer + .follow(&path, None) + .unwrap() + .available() + .await + .unwrap() + .unwrap() + }; let futures::future::Either::Right((broadcast, driver)) = futures::future::select(Box::pin(driver.run()), Box::pin(announced)).await else { @@ -2689,7 +2697,8 @@ mod tests { ) .await .expect("stats broadcast announced within 5s") - .expect("stats broadcast resolves"); + .expect("stats broadcast resolves") + .unwrap(); assert!(broadcast.info().instance.is_some(), "stats announce under an instance"); // Several publish intervals (1s each by default) later, the task is still @@ -3380,7 +3389,8 @@ mod tests { ) .await .expect("stats announced") - .expect("stats present"); + .expect("stats present") + .unwrap(); } /// The reserved `cluster.id` 0 is rejected, since it cannot be excluded for loop @@ -3872,7 +3882,7 @@ mod tests { }; let _dial = fingerprint.dial_lan_target(&target).expect("dial"); - let mut announced = fingerprint.origin.consume().announced(); + let mut announced = fingerprint.origin.consume().routes(); let (update, _) = kio::clock::timeout(TIMEOUT, next_update(&mut announced)) .await .expect("timed out waiting for from-node") @@ -3883,7 +3893,7 @@ mod tests { let _from_fp = fingerprint.origin.create_broadcast("from-fingerprint").expect("create"); _from_fp.announce(Default::default()).expect("announce"); - let mut announced = node.origin.consume().announced(); + let mut announced = node.origin.consume().routes(); loop { let (update, _) = kio::clock::timeout(TIMEOUT, next_update(&mut announced)) .await @@ -3897,7 +3907,7 @@ mod tests { } // Each relay's local view holds only what it ingested itself. - let mut local = node.origin.consume().local().announced(); + let mut local = node.origin.consume().local().routes(); let update = try_next_announced(&mut local).expect("from-node is local"); assert_eq!(update.prefix.as_str(), "from-node"); assert_eq!(update.route.source(), origin::Source::Local); @@ -4194,7 +4204,7 @@ mod tests { .await .expect("dialer task"); - let mut announced = node.origin.consume().announced(); + let mut announced = node.origin.consume().routes(); loop { let (update, active) = kio::clock::timeout(TIMEOUT, next_update(&mut announced)) .await @@ -4242,14 +4252,14 @@ mod tests { .expect("peer") .publish("forwarded", Default::default()); - let mut announced = cluster.origin.consume().announced(); + let mut announced = cluster.origin.consume().routes(); let forwarded = try_next_announced(&mut announced).expect("forwarded"); assert_eq!(forwarded.prefix.as_str(), "forwarded"); assert!(matches!(forwarded.route.source(), origin::Source::Peer(_))); let ingest = try_next_announced(&mut announced).expect("ingest"); assert_eq!(ingest.route.source(), origin::Source::Local); - let mut local = cluster.origin.consume().local().announced(); + let mut local = cluster.origin.consume().local().routes(); assert_eq!( try_next_announced(&mut local).expect("ingest").prefix.as_str(), "ingest" @@ -4284,14 +4294,14 @@ mod tests { .expect("core") .publish("core", Default::default()); - let mut everyone = cluster.subscriber(&client).expect("client").consume().announced(); + let mut everyone = cluster.subscriber(&client).expect("client").consume().routes(); let mut prefixes = std::iter::from_fn(|| try_next_announced(&mut everyone)) .map(|announce| announce.prefix.to_string()) .collect::>(); prefixes.sort(); assert_eq!(prefixes, ["core", "ingest", "mesh"]); - let mut toward_core = cluster.subscriber(&core).expect("core").consume().announced(); + let mut toward_core = cluster.subscriber(&core).expect("core").consume().routes(); let mut prefixes = std::iter::from_fn(|| try_next_announced(&mut toward_core)) .map(|announce| announce.prefix.to_string()) .collect::>(); diff --git a/rs/moq-relay/src/internal.rs b/rs/moq-relay/src/internal.rs index fea46fb2876..264d4b05104 100644 --- a/rs/moq-relay/src/internal.rs +++ b/rs/moq-relay/src/internal.rs @@ -1349,13 +1349,13 @@ mod tests { } /// The next route and whether it is active. - async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { + async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } @@ -1831,7 +1831,7 @@ mod tests { let default_ctx = stats.tier(Tier::default()).session(moq_net::Path::from_static("acme")); let pub_origin = moq_tokio::origin::spawn(); let egress = pub_origin.consume().with_stats(default_ctx.clone()); - let mut announced = egress.announced(); + let mut announced = egress.routes(); let pub_source = pub_origin.create_broadcast("demo/x").unwrap(); pub_source.announce(Default::default()).unwrap(); let pub_track = pub_source.create_track("video", timed()).unwrap(); diff --git a/rs/moq-relay/src/web.rs b/rs/moq-relay/src/web.rs index 347c8a4df0e..bc32908b266 100644 --- a/rs/moq-relay/src/web.rs +++ b/rs/moq-relay/src/web.rs @@ -1038,14 +1038,14 @@ async fn serve_announced( }; let (lease, origin) = admit_http(&state, prefix, query, &uri, server_name, remote, mtls).await?; - let mut announced = origin.announced(); + let mut announced = origin.routes(); let mut broadcasts = Vec::new(); while let Some(event) = announced.try_next() { - if let moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) - | moq_net::announce::Event::Takeover { announce: update, .. } = event + if let moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Update(update) + | moq_net::origin::RouteEvent::Restart(update) + | moq_net::origin::RouteEvent::Takeover { announce: update, .. } = event { broadcasts.push(update.prefix); } @@ -1095,7 +1095,12 @@ async fn serve_fetch( // Block until a route covers the broadcast (within the fetch deadline) so // freshly-connected subscribers don't get a spurious 404 before the announce arrives. let mut following = origin.follow("", None).map_err(|_| StatusCode::NOT_FOUND)?; - let broadcast = following.available().await.map_err(|_| StatusCode::NOT_FOUND)?; + let broadcast = following + .available() + .await + .ok() + .flatten() + .ok_or(StatusCode::NOT_FOUND)?; let sequence = match params.group { FetchGroup::Num(sequence) => Some(sequence), FetchGroup::Latest => None, diff --git a/rs/moq-relay/tests/drills.rs b/rs/moq-relay/tests/drills.rs index 6ba8ce2dfbc..9baa6359d38 100644 --- a/rs/moq-relay/tests/drills.rs +++ b/rs/moq-relay/tests/drills.rs @@ -417,7 +417,8 @@ async fn subscribe(origin: &moq_net::origin::Consumer, path: &str) -> Reader { let broadcast = kio::clock::timeout(TIMEOUT, origin.follow(path, None).unwrap().available()) .await .expect("announcement timed out") - .expect("origin closed before announcing"); + .expect("origin closed before announcing") + .unwrap(); let track = broadcast.track(TRACK).expect("track"); let groups = kio::clock::timeout(TIMEOUT, track.subscribe(None)) @@ -703,7 +704,7 @@ async fn interrupted_publisher_republishes_new_content(lane: Lane) { .await .expect("subscriber connect timed out") .expect("subscriber connect failed"); - let mut announced = subscribed.announced(); + let mut announced = subscribed.routes(); let first = moq_tokio::origin::spawn(); let broadcast = first.create_broadcast("live").expect("create broadcast"); @@ -780,7 +781,7 @@ async fn interrupted_publisher_republishes_new_content(lane: Lane) { lanes!(interrupted_publisher_republishes_new_content); /// Wait for `path` to be announced (`want`) or unannounced (`!want`). -async fn expect_announce(announced: &mut moq_net::announce::Consumer, path: &str, want: bool, who: &str) { +async fn expect_announce(announced: &mut moq_net::origin::Routes, path: &str, want: bool, who: &str) { loop { let (update, active) = kio::clock::timeout(TIMEOUT, next_update(announced)) .await @@ -1044,7 +1045,7 @@ async fn cross_cluster(lane: Lane, link: PeerLink) { let subscriber = moq_tokio::origin::spawn(); let subscribed = subscriber.consume(); - let mut announced = subscribed.announced(); + let mut announced = subscribed.routes(); let url = subscribe_path.url.clone(); let subscribe_session = kio::clock::timeout( TIMEOUT, @@ -1358,7 +1359,7 @@ async fn no_publisher_never_delivers(lane: Lane) { Lane::Impaired => Duration::from_secs(2), }; - let mut announced = subscribed.announced(); + let mut announced = subscribed.routes(); let mut following = subscribed.follow("live", None).unwrap(); let (update, resolved) = tokio::join!( kio::clock::timeout(quiet, next_update(&mut announced)), @@ -1378,13 +1379,13 @@ async fn no_publisher_never_delivers(lane: Lane) { lanes!(no_publisher_never_delivers); /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } @@ -1452,6 +1453,7 @@ async fn subscriber_max_age_skips_bufferbloat(version: &str) { let remote = kio::clock::timeout(TIMEOUT, consumed.follow("live", None).unwrap().available()) .await .unwrap() + .unwrap() .unwrap(); let mut groups = remote .track(TRACK) diff --git a/rs/moq-relay/tests/integration/auth_lifetime.rs b/rs/moq-relay/tests/integration/auth_lifetime.rs index 5e7b2b85dd3..f29102a5cd4 100644 --- a/rs/moq-relay/tests/integration/auth_lifetime.rs +++ b/rs/moq-relay/tests/integration/auth_lifetime.rs @@ -294,7 +294,7 @@ async fn connect_and_round_trip(url: &url::Url) -> (moq_tokio::Connection, moq_t let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let sub_session = kio::clock::timeout( TIMEOUT, client() @@ -661,7 +661,7 @@ async fn a_moved_tier_retags_the_live_session() { let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let sub_session = kio::clock::timeout( TIMEOUT, client() @@ -822,7 +822,8 @@ async fn a_rechecked_grant_resizes_live_sessions() { let broadcast = kio::clock::timeout(TIMEOUT, sub_consumer.follow(path, None).unwrap().available()) .await .expect("announcement timeout") - .expect("announced broadcast resolves"); + .expect("announced broadcast resolves") + .unwrap(); subs.push( broadcast .track("media") @@ -885,7 +886,8 @@ async fn a_rechecked_grant_resizes_live_sessions() { let broadcast = kio::clock::timeout(TIMEOUT, sub_consumer.follow("alice/audio", None).unwrap().available()) .await .expect("the undeafened path was never announced again") - .expect("announced broadcast resolves"); + .expect("announced broadcast resolves") + .unwrap(); let mut audio = broadcast .track("media") .unwrap() @@ -1003,7 +1005,7 @@ async fn http_routes_hold_a_lease() { // Wait until the announcement reaches the relay before asking over HTTP: // the /announced handler only reports what has arrived so far. let sub_origin = moq_tokio::origin::spawn(); - let mut announcements = sub_origin.consume().announced(); + let mut announcements = sub_origin.consume().routes(); let _sub_session = kio::clock::timeout( TIMEOUT, client() @@ -1343,7 +1345,7 @@ async fn mtls_route_source(mtls_peer: bool) -> moq_net::origin::Source { .expect("connect timeout") .expect("the peer is admitted"); - let mut announced = cluster.origin.consume().announced(); + let mut announced = cluster.origin.consume().routes(); let (update, _) = kio::clock::timeout(TIMEOUT, next_update(&mut announced)) .await .expect("timed out waiting for forwarded") @@ -1710,13 +1712,13 @@ async fn a_late_decision_within_the_deadline_admits() { } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } @@ -1800,6 +1802,7 @@ async fn in_band_tokens_widen_and_withdraw_live_prefixes() { let a_read = kio::clock::timeout(TIMEOUT, read.follow("a/cam", None).unwrap().available()) .await .unwrap() + .unwrap() .unwrap(); let mut a_sub = a_read.track("video").unwrap().subscribe(None).await.unwrap(); let mut replacement = Grant::new(all(), "b/**".parse::().unwrap()); @@ -1812,6 +1815,7 @@ async fn in_band_tokens_widen_and_withdraw_live_prefixes() { let b_read = kio::clock::timeout(TIMEOUT, read.follow("b/cam", None).unwrap().available()) .await .expect("new prefix not announced") + .unwrap() .unwrap(); let mut b_sub = b_read.track("video").unwrap().subscribe(None).await.unwrap(); let mut group = b_track.append_group().unwrap(); @@ -1911,7 +1915,7 @@ async fn request_token_lease_ends_before_the_connection_and_reopens_later() { }; let attached = attaches(); assert_eq!(attached.len(), 1); - let mut announced = read.announced(); + let mut announced = read.routes(); drop(first); kio::clock::timeout(TIMEOUT, async { while let Some((route, live)) = next_update(&mut announced).await { @@ -1962,7 +1966,7 @@ async fn relay_request_limits_distinguish_clients_and_peers() { options.peer.announces = 2; let cluster = crate::spawn_cluster(options); let consumer = cluster.origin.consume(); - let mut observed = consumer.announced(); + let mut observed = consumer.routes(); let (port, relay) = match scheme { "tcp" => spawn_relay_with(auth, cluster).await, _ => spawn_ws_relay_with(auth, cluster).await, diff --git a/rs/moq-relay/tests/integration/cluster_fresh_port.rs b/rs/moq-relay/tests/integration/cluster_fresh_port.rs index dd43dc458aa..70ac6615738 100644 --- a/rs/moq-relay/tests/integration/cluster_fresh_port.rs +++ b/rs/moq-relay/tests/integration/cluster_fresh_port.rs @@ -152,13 +152,13 @@ async fn dropped_flow_recovers(budget: Duration, effective: Duration) { let mut config = cluster::Config::default(); config.connect = vec![Peer::new(format!("https://{proxy}/"))]; let cluster = crate::spawn_cluster(cluster::Options::new(config)).with_connect(connect, Default::default()); - let mut announced = cluster.origin.consume().announced(); + let mut announced = cluster.origin.consume().routes(); let run = tokio::spawn(cluster.clone().start().await.expect("cluster start").run()); kio::clock::timeout(effective + SLACK, async { loop { let event = announced.next().await.expect("origin closed"); - if let moq_net::announce::Event::Start(update) | moq_net::announce::Event::Update(update) = event + if let moq_net::origin::RouteEvent::Start(update) | moq_net::origin::RouteEvent::Update(update) = event && update.prefix.as_str() == "peer" { break; diff --git a/rs/moq-relay/tests/integration/cluster_idle.rs b/rs/moq-relay/tests/integration/cluster_idle.rs index 4696495b809..641f52264b6 100644 --- a/rs/moq-relay/tests/integration/cluster_idle.rs +++ b/rs/moq-relay/tests/integration/cluster_idle.rs @@ -108,13 +108,13 @@ async fn silent_peer_detected() { let mut quic = moq_tokio::quic::Config::default(); quic.idle_timeout = Some((IDLE).into()); let cluster = crate::spawn_cluster(cluster::Options::new(config)).with_connect(connect, quic); - let mut announced = cluster.origin.consume().announced(); + let mut announced = cluster.origin.consume().routes(); let run = tokio::spawn(cluster.clone().start().await.expect("cluster start").run()); kio::clock::timeout(DETECT, async { loop { let event = announced.next().await.expect("origin closed"); - if let moq_net::announce::Event::Start(update) | moq_net::announce::Event::Update(update) = event + if let moq_net::origin::RouteEvent::Start(update) | moq_net::origin::RouteEvent::Update(update) = event && update.prefix.as_str() == "peer" { break; @@ -132,7 +132,7 @@ async fn silent_peer_detected() { kio::clock::timeout(DETECT, async { loop { let event = announced.next().await.expect("origin closed"); - if let moq_net::announce::Event::End(update) = event + if let moq_net::origin::RouteEvent::End(update) = event && update.prefix.as_str() == "peer" { break; diff --git a/rs/moq-relay/tests/integration/cluster_unknown.rs b/rs/moq-relay/tests/integration/cluster_unknown.rs index 829e76a78a6..74449931f4e 100644 --- a/rs/moq-relay/tests/integration/cluster_unknown.rs +++ b/rs/moq-relay/tests/integration/cluster_unknown.rs @@ -240,7 +240,8 @@ async fn read_first_frame(port: u16) -> Result, String> { .unwrap() .available() .await - .map_err(|err| format!("broadcast unroutable: {err}"))?; + .map_err(|err| format!("broadcast unroutable: {err}"))? + .ok_or("the first change is an instance")?; // This verifies route propagation rather than real-time backlog skipping. Give // every hop enough tolerance for the next 100ms group to arrive while the @@ -281,7 +282,7 @@ async fn watch_announces( ) -> tokio::task::JoinHandle> { let url: Url = format!("tcp://127.0.0.1:{port}").parse().expect("parse url"); let origin = moq_tokio::origin::spawn(); - let mut announced = origin.consume().announced(); + let mut announced = origin.consume().routes(); let session = kio::clock::timeout(TIMEOUT, client(None).with_subscriber(origin).connect(url).established()) .await .expect("viewer connect timeout") @@ -368,7 +369,7 @@ async fn unknown_publisher_republish_stays_routable() { let url: Url = format!("tcp://127.0.0.1:{}", mesh.d()).parse().expect("parse url"); let origin = moq_tokio::origin::spawn(); - let mut announced = origin.consume().announced(); + let mut announced = origin.consume().routes(); let _session = kio::clock::timeout(TIMEOUT, client(None).with_subscriber(origin).connect(url).established()) .await .expect("viewer connect timeout") @@ -423,12 +424,12 @@ async fn unknown_publisher_does_not_flap_across_a_lite04_cluster_triangle() { } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } diff --git a/rs/moq-relay/tests/integration/connection.rs b/rs/moq-relay/tests/integration/connection.rs index d53bbe80c93..889128047b5 100644 --- a/rs/moq-relay/tests/integration/connection.rs +++ b/rs/moq-relay/tests/integration/connection.rs @@ -46,6 +46,7 @@ async fn largest_quic_sequence_reaches_the_subscriber() { .unwrap() .available() .await + .unwrap() .unwrap(); let mut sub = remote.track("data").unwrap().subscribe(None).await.unwrap(); for sequence in [5, 1 << 32, (1 << 53) - 1, (1 << 53) + 1, (1 << 62) - 1] { @@ -91,18 +92,18 @@ fn publish( } /// The next active route's prefix. -async fn next_active(announced: &mut moq_net::announce::Consumer) -> String { +async fn next_active(announced: &mut moq_net::origin::Routes) -> String { loop { match kio::clock::timeout(TIMEOUT, announced.next()) .await .expect("announcement timeout") .expect("origin closed") { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => return route.prefix.to_string(), - moq_net::announce::Event::End(_) => {} + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => return route.prefix.to_string(), + moq_net::origin::RouteEvent::End(_) => {} } } } @@ -179,7 +180,7 @@ async fn two_publish_only_clients_coexist() { .expect("beta connects"); let origin = memory::origin(); - let mut announced = origin.consume().announced(); + let mut announced = origin.consume().routes(); let _subscriber = connect( moq_net::Client::new().with_subscriber(origin), newest(), @@ -224,7 +225,7 @@ async fn the_setup_path_reaches_the_decider_and_roots_the_grant() { let origin = memory::origin(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let _subscriber = connect( moq_net::Client::new().with_subscriber(origin), newest(), diff --git a/rs/moq-relay/tests/integration/embed.rs b/rs/moq-relay/tests/integration/embed.rs index 270a199a0c2..5e36108e209 100644 --- a/rs/moq-relay/tests/integration/embed.rs +++ b/rs/moq-relay/tests/integration/embed.rs @@ -285,7 +285,7 @@ async fn embed_and_stop(mut config: Config) { let url: url::Url = format!("https://{quic}/").parse().expect("parse url"); let subscriber_origin = moq_tokio::origin::spawn(); let consumer = subscriber_origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let subscriber = kio::clock::timeout( TIMEOUT, client() @@ -591,12 +591,12 @@ fn embedded_loader_refuses_obsolete_file_settings() { } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } diff --git a/rs/moq-relay/tests/integration/goaway_cluster.rs b/rs/moq-relay/tests/integration/goaway_cluster.rs index 9035e470b4f..816567946c7 100644 --- a/rs/moq-relay/tests/integration/goaway_cluster.rs +++ b/rs/moq-relay/tests/integration/goaway_cluster.rs @@ -178,7 +178,8 @@ async fn cluster_continues_a_group_split_by_goaway_inner() { .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut sub = within("subscribe", bc.track("video").expect("track").subscribe(None)) .await .expect("subscribe"); @@ -205,7 +206,7 @@ async fn cluster_continues_a_group_split_by_goaway_inner() { // The cursor replays A's route first. After the GOAWAY, A's re-prices to DRAIN, so // the next route at any other cost is B's. - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); within("A's route", announced.next()).await.expect("announce update"); session_a .drain() @@ -216,10 +217,10 @@ async fn cluster_continues_a_group_split_by_goaway_inner() { loop { let event = announced.next().await.expect("announce update"); // Under another instance, B's route is another source: a restart. - if let moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Restart(update) - | moq_net::announce::Event::Takeover { announce: update, .. } = event + if let moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Update(update) + | moq_net::origin::RouteEvent::Restart(update) + | moq_net::origin::RouteEvent::Takeover { announce: update, .. } = event && update.route.cost != moq_net::origin::Cost::DRAIN { break; @@ -322,7 +323,8 @@ async fn cluster_migrates_on_upstream_goaway_inner() { .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut sub = bc .track("video") .expect("track handle") @@ -345,7 +347,7 @@ async fn cluster_migrates_on_upstream_goaway_inner() { // Watch for retraction during the swap: the migration must never // unannounce the path (metadata updates are expected: the drain // re-prices the old route and the sibling announces its own). - let mut announcements = cluster.origin.consume().announced(); + let mut announcements = cluster.origin.consume().routes(); let (first, _) = next_update(&mut announcements).await.expect("initial announce"); assert_eq!(first.prefix.as_str(), "cam"); @@ -552,7 +554,7 @@ async fn cluster_diamond_goaway_seamless_failover_inner() { // Watch announcements for the whole test: the failover must never // unannounce the broadcast under the subscriber. - let mut announcements = sub_origin.consume().announced(); + let mut announcements = sub_origin.consume().routes(); let (first, _) = within( "broadcast announced through the MID-A leg", next_update(&mut announcements), @@ -563,7 +565,13 @@ async fn cluster_diamond_goaway_seamless_failover_inner() { let bc = within("broadcast resolves on the subscriber origin", async { let consumer = sub_origin.consume(); - consumer.follow("diamond", None).unwrap().available().await.ok() + consumer + .follow("diamond", None) + .unwrap() + .available() + .await + .ok() + .flatten() }) .await .expect("broadcast announced"); @@ -851,12 +859,12 @@ async fn goaway_handover_is_enforced_while_the_replacement_dial_hangs_inner() { } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } diff --git a/rs/moq-relay/tests/integration/hidden_cluster.rs b/rs/moq-relay/tests/integration/hidden_cluster.rs index b030ce5da1e..bb2e8bb64c1 100644 --- a/rs/moq-relay/tests/integration/hidden_cluster.rs +++ b/rs/moq-relay/tests/integration/hidden_cluster.rs @@ -54,7 +54,7 @@ async fn old_peer_keeps_hidden_paths(version: moq_net::Version) { kio::clock::timeout(TEST_TIMEOUT, async { let (port, mut server) = listen_tcp(version).await; let peer_origin = moq_tokio::origin::spawn(); - let mut discovered = peer_origin.consume().with_hidden(true).announced(); + let mut discovered = peer_origin.consume().with_hidden(true).routes(); let accept = tokio::spawn(async move { let request = server.accept().await.expect("cluster dial"); let session = request.with_subscriber(peer_origin).ok().await.expect("accept"); @@ -72,7 +72,7 @@ async fn old_peer_keeps_hidden_paths(version: moq_net::Version) { let cluster_run = tokio::spawn(cluster.clone().start().await.expect("cluster start").run()); loop { - if let moq_net::announce::Event::Start(announce) | moq_net::announce::Event::Update(announce) = + if let moq_net::origin::RouteEvent::Start(announce) | moq_net::origin::RouteEvent::Update(announce) = discovered.next().await.expect("peer origin closed") && announce.prefix.as_str() == ".hidden/test" { diff --git a/rs/moq-relay/tests/integration/public_rules.rs b/rs/moq-relay/tests/integration/public_rules.rs index 40a7ac83f3e..d3d5cc73c5d 100644 --- a/rs/moq-relay/tests/integration/public_rules.rs +++ b/rs/moq-relay/tests/integration/public_rules.rs @@ -96,7 +96,7 @@ async fn publish(url: url::Url, name: &str) -> Box { async fn first_broadcast(url: url::Url) -> String { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); - let mut announcements = consumer.announced(); + let mut announcements = consumer.routes(); let _session = kio::clock::timeout( TIMEOUT, client() @@ -111,7 +111,7 @@ async fn first_broadcast(url: url::Url) -> String { let update = kio::clock::timeout(TIMEOUT, async { match announcements.next().await.expect("origin closed") { - moq_net::announce::Event::Start(update) => update, + moq_net::origin::RouteEvent::Start(update) => update, other => panic!("expected announce, got {other:?}"), } }) diff --git a/rs/moq-relay/tests/integration/refusals.rs b/rs/moq-relay/tests/integration/refusals.rs index dc8f6ba30a9..dde11bd406f 100644 --- a/rs/moq-relay/tests/integration/refusals.rs +++ b/rs/moq-relay/tests/integration/refusals.rs @@ -259,6 +259,7 @@ async fn refused_only_stats_depth_one_has_root_totals_and_shared_embedder_metric ) .await .unwrap() + .unwrap() .unwrap(); let consumer = moq_stats::Consumer::new(broadcast, Default::default()); let mut totals = consumer.totals().await.unwrap(); @@ -315,6 +316,7 @@ async fn peer_zero_close_is_clean_and_application_close_counts_once() { .unwrap() .available() .await + .unwrap() .unwrap(); let consumer = moq_stats::Consumer::new(broadcast, Default::default()); let mut totals = consumer.totals().await.unwrap(); diff --git a/rs/moq-relay/tests/integration/runtime_uring.rs b/rs/moq-relay/tests/integration/runtime_uring.rs index 45119009d1d..11a7dcff842 100644 --- a/rs/moq-relay/tests/integration/runtime_uring.rs +++ b/rs/moq-relay/tests/integration/runtime_uring.rs @@ -181,7 +181,7 @@ async fn uring_workers_serve_webtransport_and_raw_quic() { for url in [&wt_url, &raw_url, &wt_url, &raw_url] { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); - let announced = consumer.announced(); + let announced = consumer.routes(); let connection = connect(client().with_subscriber(origin), url.clone()).await; subscribers.push((connection, consumer, announced)); } @@ -279,7 +279,7 @@ async fn uring_workers_serve_every_configured_version() { let (version, client) = pinned(name); let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let connection = connect(client.with_subscriber(origin), url).await; assert_eq!(connection.version(), Some(version), "{scheme} {name}"); @@ -698,7 +698,7 @@ async fn an_mtls_client_authenticates_without_a_token() { let subscriber_origin = moq_tokio::origin::spawn(); let consumer = subscriber_origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let subscriber = connect(client().with_subscriber(subscriber_origin), url).await; let (update, active) = kio::clock::timeout(TIMEOUT, next_update(&mut announced)) @@ -779,7 +779,7 @@ async fn uring_workers_write_qlog_traces() { let subscriber_origin = moq_tokio::origin::spawn(); let consumer = subscriber_origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let subscriber = connect(client().with_subscriber(subscriber_origin), url).await; let (_, active) = kio::clock::timeout(TIMEOUT, next_update(&mut announced)) .await @@ -910,13 +910,13 @@ async fn spawn_auth_server(policy: moq_auth::serve::Policy) -> url::Url { } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } diff --git a/rs/moq-relay/tests/integration/runtime_workers.rs b/rs/moq-relay/tests/integration/runtime_workers.rs index 946dd29d249..509080ae6bc 100644 --- a/rs/moq-relay/tests/integration/runtime_workers.rs +++ b/rs/moq-relay/tests/integration/runtime_workers.rs @@ -114,7 +114,7 @@ async fn workers_serve_quic_and_share_one_origin() { for _ in 0..WORKERS { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); - let announced = consumer.announced(); + let announced = consumer.routes(); let connection = connect(client().with_subscriber(origin), url.clone()).await; subscribers.push((connection, consumer, announced)); } @@ -161,12 +161,12 @@ async fn workers_serve_quic_and_share_one_origin() { } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } diff --git a/rs/moq-relay/tests/integration/smoke.rs b/rs/moq-relay/tests/integration/smoke.rs index 0f942450395..2d71d3770a3 100644 --- a/rs/moq-relay/tests/integration/smoke.rs +++ b/rs/moq-relay/tests/integration/smoke.rs @@ -171,7 +171,7 @@ async fn relay_websocket_round_trip_uses_newest_version() { // ── subscriber ────────────────────────────────────────────────── let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let (_client, sub_connection) = kio::clock::timeout(TIMEOUT, connect_once(client().with_subscriber(sub_origin), url)) @@ -261,14 +261,14 @@ async fn relay_websocket_logs_its_close_under_a_cluster_id() { } /// Read announcements until `until` shows up, returning every active prefix seen. -async fn announced_until(announcements: &mut moq_net::announce::Consumer, until: &str) -> Vec { +async fn announced_until(announcements: &mut moq_net::origin::Routes, until: &str) -> Vec { let mut seen = Vec::new(); while !seen.iter().any(|prefix| prefix == until) { let event = kio::clock::timeout(TIMEOUT, announcements.next()) .await .expect("announcement timeout") .expect("origin closed"); - if let moq_net::announce::Event::Start(announce) | moq_net::announce::Event::Update(announce) = event { + if let moq_net::origin::RouteEvent::Start(announce) | moq_net::origin::RouteEvent::Update(announce) = event { seen.push(announce.prefix.as_str().to_owned()); } } @@ -307,7 +307,7 @@ async fn hidden_broadcasts_need_a_lite07_opt_in() { // An opted-in lite-07 client discovers the hidden broadcast. let opted_origin = moq_tokio::origin::spawn(); - let mut opted = opted_origin.consume().with_hidden(true).announced(); + let mut opted = opted_origin.consume().with_hidden(true).routes(); let (_opted_client, opted_connection) = kio::clock::timeout( TIMEOUT, connect_once(client_version(Some(lite07)).with_subscriber(opted_origin), url.clone()), @@ -328,7 +328,7 @@ async fn hidden_broadcasts_need_a_lite07_opt_in() { for (version, local_hidden) in [(lite07, false), (lite06, true)] { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume().with_hidden(local_hidden); - let mut announcements = consumer.announced(); + let mut announcements = consumer.routes(); let (_client, connection) = kio::clock::timeout( TIMEOUT, connect_once(client_version(Some(version)).with_subscriber(origin), url.clone()), @@ -514,7 +514,7 @@ async fn relay_websocket_root_path_upgrades() { // ── subscriber ────────────────────────────────────────────────── let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let (_client, sub_connection) = kio::clock::timeout(TIMEOUT, connect_once(client().with_subscriber(sub_origin), url)) .await @@ -652,7 +652,7 @@ async fn internal_tcp_round_trip() { // ── subscriber ────────────────────────────────────────────────── let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let (_client, sub_connection) = kio::clock::timeout(TIMEOUT, connect_once(client().with_subscriber(sub_origin), url)) .await @@ -755,7 +755,7 @@ async fn internal_unix_round_trip() { // ── subscriber ────────────────────────────────────────────────── let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let (_client, sub_connection) = kio::clock::timeout(TIMEOUT, connect_once(client().with_subscriber(sub_origin), url)) .await @@ -831,7 +831,7 @@ async fn path_round_trip(version: moq_net::Version, pub_url: url::Url, sub_url: let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + let mut announcements = sub_consumer.routes(); let sub_client = client_version(Some(version)).with_subscriber(sub_origin); let (_client, sub_connection) = kio::clock::timeout(TIMEOUT, connect_once(sub_client, sub_url)) .await @@ -970,12 +970,12 @@ async fn connect_once( } /// The next route and whether it is active. -async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { +async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } diff --git a/rs/moq-relay/tests/integration/tenant_matrix.rs b/rs/moq-relay/tests/integration/tenant_matrix.rs index a15cd2da643..ac4c3c8a9c6 100644 --- a/rs/moq-relay/tests/integration/tenant_matrix.rs +++ b/rs/moq-relay/tests/integration/tenant_matrix.rs @@ -236,7 +236,7 @@ impl Watcher { async fn connect(url: url::Url) -> Self { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let session = kio::clock::timeout( TIMEOUT, client() @@ -256,7 +256,7 @@ impl Watcher { let changed = changed.clone(); async move { while let Some(event) = announced.next().await { - if let moq_net::announce::Event::Start(route) = event { + if let moq_net::origin::RouteEvent::Start(route) = event { seen.lock().unwrap().push(route.prefix.to_string()); changed.notify_waiters(); } diff --git a/rs/moq-relay/tests/integration/upstream_links.rs b/rs/moq-relay/tests/integration/upstream_links.rs index 3b8e3eab306..c4b900ef3c3 100644 --- a/rs/moq-relay/tests/integration/upstream_links.rs +++ b/rs/moq-relay/tests/integration/upstream_links.rs @@ -11,7 +11,7 @@ use std::collections::BTreeMap; use std::time::Duration; use moq_auth::{Grant, Pattern, lease}; -use moq_net::{announce, origin}; +use moq_net::origin; use moq_relay::{ Connection, auth, cluster::{self, Peer}, @@ -181,7 +181,8 @@ async fn read_frame(origin: &origin::Producer, path: &str) { consumer.follow(path, None).unwrap().available(), ) .await - .unwrap_or_else(|err| panic!("{path} unroutable: {err}")); + .unwrap_or_else(|err| panic!("{path} unroutable: {err}")) + .expect("the first change is an instance"); let subscription = moq_net::track::Subscription::default() .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(1))); let mut track = within( @@ -207,10 +208,10 @@ async fn read_frame(origin: &origin::Producer, path: &str) { /// cursor replays that set when it registers, all starts and none held, so it /// drains without waiting. fn routes(origin: &origin::Producer) -> BTreeMap> { - let mut announced = origin.consume().announced(); + let mut announced = origin.consume().routes(); let mut routes = BTreeMap::new(); while let Some(event) = announced.try_next() { - let announce::Event::Start(announce) = event else { + let origin::RouteEvent::Start(announce) = event else { panic!("an initial set is only starts: got {event:?}"); }; let hops = announce.route.hops.iter().map(|hop| hop.id()).collect(); @@ -227,7 +228,7 @@ async fn routes_when( ) -> BTreeMap> { within(step, async { // Created first, so a change landing between two reads still wakes us. - let mut changes = origin.consume().announced(); + let mut changes = origin.consume().routes(); loop { let routes = routes(origin); if ready(&routes) { diff --git a/rs/moq-room/src/chat.rs b/rs/moq-room/src/chat.rs index 6868d5e14cb..55e018f3080 100644 --- a/rs/moq-room/src/chat.rs +++ b/rs/moq-room/src/chat.rs @@ -294,7 +294,13 @@ mod tests { let relay = origin(2); let _link = link(&publisher, &relay).await; let consumer = relay.consume(); - let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("room", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); // A first reader warms the relay's copy with the first window. { diff --git a/rs/moq-room/src/room.rs b/rs/moq-room/src/room.rs index af2c046a317..6d6cf5aa675 100644 --- a/rs/moq-room/src/room.rs +++ b/rs/moq-room/src/room.rs @@ -1,11 +1,8 @@ //! A room is a path prefix. Participants are discovered from the announce stream. -use std::task::{Poll, ready}; +use std::task::Poll; -use moq_net::{ - Path, announce, broadcast, - origin::{self, Requesting}, -}; +use moq_net::{Path, announce, broadcast, origin}; use crate::path::{Kind, parse}; @@ -33,23 +30,24 @@ impl std::fmt::Debug for Event { } } -/// An in-flight request for the broadcast an active announcement named. -struct Inflight { +/// A listed participant broadcast, followed across the instances that serve it. +struct Participant { identity: Path, kind: Kind, path: Path, - request: Requesting, + following: origin::Following, } /// Runs the announce loop and yields remote participant broadcasts. /// /// Skips paths that are not `{identity}/camera.hang` or `{identity}/screen.hang`, and -/// skips the local identity so a publisher does not see itself as a remote. +/// skips the local identity so a publisher does not see itself as a remote. A participant +/// that restarts is yielded again with its new broadcast. pub struct Room { origin: origin::Consumer, announced: announce::Consumer, local: Option, - inflight: Option, + participants: Vec, } impl std::fmt::Debug for Room { @@ -67,87 +65,76 @@ impl Room { announced: origin.announced(), origin: origin.clone(), local, - inflight: None, + participants: Vec::new(), } } /// Poll for the next remote camera/screen (un)announce. pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { loop { - if let Some(event) = ready!(self.poll_inflight(waiter)) { - return Poll::Ready(Some(event)); - } - - let (update, active) = match ready!(self.announced.poll_next(waiter)) { - // A restart resolves the new instance, replacing the one handed out before. - Some( - announce::Event::Start(update) - | announce::Event::Update(update) - | announce::Event::Restart(update) - | announce::Event::Takeover { announce: update, .. }, - ) => (update, true), - Some(announce::Event::End(update)) => (update, false), - None => return Poll::Ready(None), - }; - let path = update.prefix; - let Some(parsed) = parse(&path) else { - continue; - }; - if self.local.as_ref().is_some_and(|id| *id == parsed.identity) { - continue; - } - if !active { - return Poll::Ready(Some(Event { - identity: parsed.identity, - kind: parsed.kind, - path, - broadcast: None, - })); - } - - let mut request = self.origin.request(&path, None).into_inner(); - match kio::Task::poll(&mut request, waiter) { - Poll::Ready(Ok(broadcast)) => { + match self.announced.poll_next(waiter) { + Poll::Ready(Some(announce::Event::Present(announce))) => self.listed(announce.prefix), + Poll::Ready(Some(announce::Event::Absent(announce))) => { + let Some(index) = self.participants.iter().position(|p| p.path == announce.prefix) else { + continue; + }; + let gone = self.participants.swap_remove(index); return Poll::Ready(Some(Event { - identity: parsed.identity, - kind: parsed.kind, - path, - broadcast: Some(broadcast), + identity: gone.identity, + kind: gone.kind, + path: gone.path, + broadcast: None, })); } - Poll::Ready(Err(_)) => continue, - Poll::Pending => { - self.inflight = Some(Inflight { - identity: parsed.identity, - kind: parsed.kind, - path, - request, - }); - return Poll::Pending; - } + Poll::Ready(None) => return Poll::Ready(None), + Poll::Pending => break, } } + self.poll_participants(waiter).map(Some) } - fn poll_inflight(&mut self, waiter: &kio::Waiter) -> Poll> { - let Some(inflight) = self.inflight.as_mut() else { - return Poll::Ready(None); + /// Follow a listed path, unless it is not a remote participant broadcast. + fn listed(&mut self, path: Path) { + let Some(parsed) = parse(&path) else { + return; }; - match ready!(kio::Task::poll(&mut inflight.request, waiter)) { - Ok(broadcast) => { - let inflight = self.inflight.take().expect("inflight still set"); - Poll::Ready(Some(Event { - identity: inflight.identity, - kind: inflight.kind, - path: inflight.path, - broadcast: Some(broadcast), - })) - } - Err(_) => { - self.inflight = None; - Poll::Ready(None) + if self.local.as_ref().is_some_and(|id| *id == parsed.identity) { + return; + } + let Ok(following) = self.origin.follow(&path, None) else { + return; + }; + self.participants.push(Participant { + identity: parsed.identity, + kind: parsed.kind, + path, + following, + }); + } + + /// The next broadcast a participant's follower hands out. A refusal, such as a handler + /// refusing the path, yields nothing: the follower waits for the serving route to change, + /// and the participant's end comes from the listing. + fn poll_participants(&mut self, waiter: &kio::Waiter) -> Poll { + let mut index = 0; + while let Some(participant) = self.participants.get_mut(index) { + match participant.following.poll_available(waiter) { + Poll::Ready(Ok(Some(broadcast))) => { + return Poll::Ready(Event { + identity: participant.identity.clone(), + kind: participant.kind, + path: participant.path.clone(), + broadcast: Some(broadcast), + }); + } + Poll::Ready(Ok(None)) => continue, + // Re-polled on the next wake, rather than again now: a closed origin answers + // `Closed` every time until the listing ends. + Poll::Ready(Err(_)) => index += 1, + Poll::Pending => index += 1, } } + Poll::Pending } /// Wait for the next remote camera/screen (un)announce. @@ -193,6 +180,38 @@ mod tests { assert!(gone.broadcast.is_none()); } + /// A refused participant stays listed and followed: a publisher taking it over is yielded, and + /// its absence is still reported. + #[tokio::test(start_paused = true)] + async fn reports_the_absence_of_a_refused_participant() { + let wait = std::time::Duration::from_secs(1); + let origin = origin(); + let mut room = Room::new(&origin.consume(), None); + + let claim = origin.requests("bob/camera").expect("claim"); + let (request, nothing) = tokio::join!(claim.requested_broadcast(), kio::clock::timeout(wait, room.next())); + assert!(nothing.is_err(), "nothing serves bob yet"); + request.expect("requested").reject(moq_net::Error::NotFound); + // The refusal yields nothing. + assert!(kio::clock::timeout(wait, room.next()).await.is_err()); + + // A publisher taking the path over from the claim is still followed. + let bob = publish(&origin, "bob/camera"); + let back = kio::clock::timeout(wait, room.next()) + .await + .expect("the publisher was never yielded") + .expect("bob camera"); + assert!(back.broadcast.is_some()); + + drop((bob, claim)); + let gone = kio::clock::timeout(wait, room.next()) + .await + .expect("the absence was never reported") + .expect("bob unannounce"); + assert_eq!(gone.identity.as_str(), "bob"); + assert!(gone.broadcast.is_none()); + } + #[tokio::test] async fn yields_screen() { let origin = origin(); diff --git a/rs/moq-rtc/src/lib.rs b/rs/moq-rtc/src/lib.rs index 6b305030fd4..b7a9bec41c1 100644 --- a/rs/moq-rtc/src/lib.rs +++ b/rs/moq-rtc/src/lib.rs @@ -77,11 +77,8 @@ mod tests { /// The next route and whether it is active. async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::announce::Event::Present(route) => Some((route, true)), + moq_net::announce::Event::Absent(route) => Some((route, false)), } } @@ -166,14 +163,14 @@ mod tests { /// Publish a replacement instance of `source`, returning once the origin serves it. async fn take_over(origin: &moq_net::origin::Producer) -> (Box, moq_mux::catalog::Producer) { let instance = moq_net::Instance::random(); - let mut instances = moq_mux::follow::Instances::new(&origin.consume(), "source", Some(&instance)).unwrap(); + let mut instances = origin.consume().follow("source", instance.clone()).unwrap(); let (mut new, catalog) = publish_opus(origin, instance); push(&mut *new, 20_000, NEW_PACKET); - let started = kio::clock::timeout(TIMEOUT, instances.next()) + let started = kio::clock::timeout(TIMEOUT, instances.available()) .await .expect("the replacement never took over") .unwrap(); - assert!(matches!(started, moq_mux::follow::Event::Start(_))); + assert!(started.is_some()); (new, catalog) } @@ -286,7 +283,13 @@ mod tests { let source = moq_mux::Source::new(source, broadcast); let pushed = tokio::spawn(async move { client.publish(url, source).await }); let ingested = ingest_origin.consume(); - let ingested = ingested.follow("ingested", None).unwrap().available().await.unwrap(); + let ingested = ingested + .follow("ingested", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut played = opus_output(&ingested).await; assert_eq!(next_payload(&mut played).await, OPUS_PACKET); diff --git a/rs/moq-rtc/src/server/whep.rs b/rs/moq-rtc/src/server/whep.rs index 94214641072..5f0cdc9a224 100644 --- a/rs/moq-rtc/src/server/whep.rs +++ b/rs/moq-rtc/src/server/whep.rs @@ -210,7 +210,8 @@ async fn resolve(subscriber: &moq_net::origin::Consumer, path: moq_net::Path) -> let mut following = subscriber.follow(path, None)?; let broadcast = kio::clock::timeout_at(deadline, following.available()) .await - .map_err(|_| Error::Moq(moq_net::Error::Unroutable))??; + .map_err(|_| Error::Moq(moq_net::Error::Unroutable))?? + .ok_or(Error::Moq(moq_net::Error::Unroutable))?; kio::clock::timeout_at( deadline, EgressSource::new(moq_mux::Source::new(subscriber.clone(), broadcast)), diff --git a/rs/moq-rtmp/src/dial.rs b/rs/moq-rtmp/src/dial.rs index 1bfe60a142e..acc98c9e4e6 100644 --- a/rs/moq-rtmp/src/dial.rs +++ b/rs/moq-rtmp/src/dial.rs @@ -822,7 +822,13 @@ mod tests { }); let consumer = downstream.consume(); - let copy = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let copy = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); // The catalog goes out once the keyframe landed, so the copy holds media to linger with. let mut listed = copy.track("catalog.json").unwrap().subscribe(None).await.unwrap(); assert!(listed.recv_group().await.unwrap().is_some()); @@ -938,7 +944,13 @@ mod tests { // Read what reaches the remote back out as FLV. let consumer = downstream.consume(); - let copy = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let copy = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let source = moq_mux::Source::new(consumer, copy); let catalog = source .catalog::<()>(moq_mux::catalog::CatalogFormat::default()) @@ -955,12 +967,13 @@ mod tests { until(OLD_IDR).await; let instance = moq_net::Instance::random(); - let mut instances = moq_mux::follow::Instances::new(&upstream.consume(), "live/cam0", Some(&instance)).unwrap(); + let mut instances = upstream.consume().follow("live/cam0", instance.clone()).unwrap(); let (_new, _new_catalog) = publish(&upstream, &instance, NEW_IDR); - kio::clock::timeout(Duration::from_secs(5), instances.next()) + kio::clock::timeout(Duration::from_secs(5), instances.available()) .await .expect("the new publisher takes over") - .unwrap(); + .unwrap() + .expect("the new publisher serves"); let mut delta = vec![0x27, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, OLD_DELTA.len() as u8]; delta.extend_from_slice(OLD_DELTA); @@ -1037,6 +1050,7 @@ mod tests { .unwrap() .available() .await + .unwrap() .unwrap(); // It should carry a hang catalog track (proof the FLV demux produced real diff --git a/rs/moq-rtmp/src/server.rs b/rs/moq-rtmp/src/server.rs index c46343d4efc..9c1ed34d792 100644 --- a/rs/moq-rtmp/src/server.rs +++ b/rs/moq-rtmp/src/server.rs @@ -837,7 +837,7 @@ impl Play { } } }; - let Ok(broadcast) = broadcast else { + let Ok(Some(broadcast)) = broadcast else { tracing::debug!(peer = %self.peer, %path, "play broadcast unavailable"); return self.reject("stream not found").await; }; @@ -1901,7 +1901,13 @@ mod tests { let (mut client, server) = tokio::io::duplex(64 * 1024); let accept = tokio::spawn(accept_publish(server, origin.clone(), Duration::ZERO)); let catalog = async { - let broadcast = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let broadcast = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let track = broadcast.track(hang::Catalog::DEFAULT_NAME).unwrap(); track.subscribe(None).await.unwrap() }; @@ -2211,7 +2217,13 @@ mod tests { publisher.push(flv::TAG_VIDEO, 0, &vseq).unwrap(); let consumer = origin.consume(); - let broadcast = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let broadcast = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let info = broadcast.track("0.flv-v").unwrap().info().await.unwrap(); assert_eq!(info.max_age, Some(Duration::from_secs(3))); } @@ -2344,12 +2356,13 @@ mod tests { assert_eq!(next().await.1[..2], [0x17, 0x01]); let instance = moq_net::Instance::random(); - let mut instances = moq_mux::follow::Instances::new(&consumer, "live/cam0", Some(&instance)).unwrap(); + let mut instances = consumer.follow("live/cam0", instance.clone()).unwrap(); let (_new, _new_catalog) = publish_avc(&origin, instance, &[0x65, 0x99, 0x99, 0x99, 0x99]); - kio::clock::timeout(Duration::from_secs(5), instances.next()) + kio::clock::timeout(Duration::from_secs(5), instances.available()) .await .expect("the new publisher takes over") - .unwrap(); + .unwrap() + .expect("the new publisher serves"); let mut delta = vec![0x27, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, OLD_DELTA.len() as u8]; delta.extend_from_slice(OLD_DELTA); @@ -2803,7 +2816,13 @@ mod tests { let stale_client = tokio::spawn(async move { run_client(TcpStream::connect(addr).await.unwrap(), ClientMode::Publish).await; }); - let stale = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let stale = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let mut catalog = stale .track(hang::Catalog::DEFAULT_NAME) .unwrap() @@ -2811,12 +2830,12 @@ mod tests { .await .unwrap(); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let fresh_client = tokio::spawn(async move { run_client(TcpStream::connect(addr).await.unwrap(), ClientMode::Publish).await; }); kio::clock::timeout(Duration::from_secs(5), async { - while !matches!(announced.next().await, Some(moq_net::announce::Event::Restart(_))) {} + while !matches!(announced.next().await, Some(moq_net::origin::RouteEvent::Restart(_))) {} }) .await .expect("the reconnect restarts the broadcast"); diff --git a/rs/moq-srt/src/ts.rs b/rs/moq-srt/src/ts.rs index df0e3f3b593..e2e6106d6f6 100644 --- a/rs/moq-srt/src/ts.rs +++ b/rs/moq-srt/src/ts.rs @@ -292,7 +292,9 @@ impl Subscriber { // Join the broadcast once announced. The export resolves any referenced sibling // broadcast (the catalog `broadcast` field) through the same origin. let broadcast = match following.available().await { - Ok(broadcast) => broadcast, + Ok(Some(broadcast)) => broadcast, + // The first change is always an instance. + Ok(None) => return Ok(None), Err(moq_net::Error::Closed) => return Ok(None), Err(err) => return Err(err.into()), }; @@ -631,7 +633,7 @@ mod tests { /// announced. async fn program(origin: &moq_net::origin::Producer, path: &str) -> (u16, Vec) { let consumer = origin.consume(); - let broadcast = consumer.follow(path, None).unwrap().available().await.unwrap(); + let broadcast = consumer.follow(path, None).unwrap().available().await.unwrap().unwrap(); let mut catalog = moq_mux::catalog::Consumer::::new(&broadcast, CatalogFormat::Hang) .await .unwrap(); @@ -727,7 +729,13 @@ mod tests { publisher.feed(Bytes::from_static(BBB5S)).unwrap(); let consumer = origin.consume(); - let broadcast = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let broadcast = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let start = kio::clock::now(); let wait = tokio::spawn(publisher.finish(linger).unwrap().wait()); kio::clock::sleep(linger / 2).await; @@ -760,7 +768,13 @@ mod tests { publisher.feed(Bytes::from(ts)).unwrap(); let consumer = origin.consume(); - let broadcast = consumer.follow("live/cam0", None).unwrap().available().await.unwrap(); + let broadcast = consumer + .follow("live/cam0", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); let info = broadcast.track("0.avc3").unwrap().info().await.unwrap(); assert_eq!(info.max_age, Some(Duration::from_secs(3))); } @@ -831,7 +845,13 @@ mod tests { let mut publisher = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); let consumer = origin.consume(); - let broadcast = consumer.follow("ingest", None).unwrap().available().await.unwrap(); + let broadcast = consumer + .follow("ingest", None) + .unwrap() + .available() + .await + .unwrap() + .unwrap(); publisher.feed(bytes::Bytes::from_static(BBB5S)).unwrap(); diff --git a/rs/moq-stats/benches/producer_tick.rs b/rs/moq-stats/benches/producer_tick.rs index 17d46eb2079..d582c25e36d 100644 --- a/rs/moq-stats/benches/producer_tick.rs +++ b/rs/moq-stats/benches/producer_tick.rs @@ -36,7 +36,7 @@ use std::time::{Duration, Instant}; use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::FutureExt; -use moq_net::{announce, origin, stats}; +use moq_net::{origin, stats}; use moq_stats::produce::bench::{Driver, FrameBytes}; struct Counter; @@ -96,7 +96,7 @@ struct Relay { _origin: origin::Producer, _origin_driver: origin::Driver, _publishers: Vec, - _cursors: Vec, + _cursors: Vec, _sessions: Vec, _held: Vec, } @@ -131,7 +131,7 @@ impl Relay { .map(|tier| { let tier = stats::Tier::new(moq_net::Path::new(&format!("t{tier}")).unwrap()); let session = registry.tier(tier.clone()).session(moq_net::Path::from_static("root")); - let mut cursor = origin.consume().with_stats(session.clone()).announced(); + let mut cursor = origin.consume().with_stats(session.clone()).routes(); while cursor.next().now_or_never().flatten().is_some() {} cursors.push(cursor); sessions.push(session); diff --git a/rs/moq-stats/src/aggregate.rs b/rs/moq-stats/src/aggregate.rs index b33b1396df7..0724f337a41 100644 --- a/rs/moq-stats/src/aggregate.rs +++ b/rs/moq-stats/src/aggregate.rs @@ -763,7 +763,7 @@ impl Node { struct Merged { /// Resolves announced node paths into broadcasts. origin: origin::Consumer, - announce: moq_net::announce::Consumer, + announce: moq_net::origin::Routes, prefix: Path, /// Track name subscribed on each node broadcast. name: String, @@ -783,7 +783,7 @@ impl Merged { fn new(origin: origin::Consumer, config: &Config, name: String) -> Self { Self { // Stats live under a `.`-named prefix, which discovery hides by default. - announce: origin.clone().with_hidden(true).announced(), + announce: origin.clone().with_hidden(true).routes(), origin, prefix: config.prefix.clone(), name, @@ -808,16 +808,16 @@ impl Merged { match self.announce.poll_next(waiter) { // A takeover continues what was read through its predecessor, like an update. Poll::Ready(Some( - moq_net::announce::Event::Start(update) - | moq_net::announce::Event::Update(update) - | moq_net::announce::Event::Takeover { announce: update, .. }, + moq_net::origin::RouteEvent::Start(update) + | moq_net::origin::RouteEvent::Update(update) + | moq_net::origin::RouteEvent::Takeover { announce: update, .. }, )) => changed |= self.apply_announce(update, true), - Poll::Ready(Some(moq_net::announce::Event::End(update))) => { + Poll::Ready(Some(moq_net::origin::RouteEvent::End(update))) => { changed |= self.apply_announce(update, false) } // Another instance: the one tracked at the path ends, then the new one // starts, so its reader resolves afresh. - Poll::Ready(Some(moq_net::announce::Event::Restart(update))) => { + Poll::Ready(Some(moq_net::origin::RouteEvent::Restart(update))) => { // An announced prefix is a path beneath the root, so rooting it fits. let absolute = self.announce.absolute(&update.prefix).ok(); if let Some(previous) = absolute.and_then(|absolute| self.last_announced.get(&absolute).cloned()) { @@ -1133,19 +1133,19 @@ mod tests { } /// The next route and whether it is active. - async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { + async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } use std::time::Duration; - use moq_net::{Path, Timestamp, announce, broadcast, origin, track}; + use moq_net::{Path, Timestamp, broadcast, origin, track}; use moq_net::stats::Tier; @@ -1244,7 +1244,7 @@ mod tests { /// announce, bumping the `_closed` counters. #[allow(dead_code, reason = "held for their drop, never read")] struct Feed { - announced: announce::Consumer, + announced: origin::Routes, source: origin::Publication, consumer: broadcast::Consumer, sub: track::Subscriber, @@ -1262,7 +1262,7 @@ mod tests { let feed_origin = produce_origin(); let egress = feed_origin.consume().with_stats(ctx.clone()); - let mut announced = egress.announced(); + let mut announced = egress.routes(); let source = feed_origin.create_broadcast(path).expect("create_broadcast"); source.announce(origin::Route::default()).expect("announce"); let track = source.create_track("video", timed()).expect("create_track"); diff --git a/rs/moq-stats/src/consume.rs b/rs/moq-stats/src/consume.rs index 28bda8fa857..ba493f50ed3 100644 --- a/rs/moq-stats/src/consume.rs +++ b/rs/moq-stats/src/consume.rs @@ -162,19 +162,19 @@ mod tests { } /// The next route and whether it is active. - async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { + async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } use std::time::Duration; - use moq_net::{Consume, Path, Timestamp, announce, origin, track}; + use moq_net::{Consume, Path, Timestamp, origin, track}; use crate::{Producer, Tier, produce}; @@ -195,7 +195,7 @@ mod tests { struct Feed { track: track::Producer, sub: track::Subscriber, - _announced: announce::Consumer, + _announced: origin::Routes, _source: origin::Publication, _ctx: moq_net::stats::Session, } @@ -220,7 +220,7 @@ mod tests { let feed_origin = produce_origin(); let egress = feed_origin.consume().with_stats(ctx.clone()); - let mut announced = egress.announced(); + let mut announced = egress.routes(); let source = feed_origin.create_broadcast(path).unwrap(); source.announce(origin::Route::default()).unwrap(); let track = source.clone().create_track("video", timed()).unwrap(); @@ -240,7 +240,7 @@ mod tests { } async fn announced(origin: &origin::Producer) -> moq_net::broadcast::Consumer { - let mut consumer = origin.consume().with_hidden(true).announced(); + let mut consumer = origin.consume().with_hidden(true).routes(); tokio::time::advance(Duration::from_millis(1)).await; let (update, active) = next_update(&mut consumer).await.expect("expected announce"); assert!(active); diff --git a/rs/moq-stats/src/produce.rs b/rs/moq-stats/src/produce.rs index f61d5a6f7d4..f98bbdd4f6b 100644 --- a/rs/moq-stats/src/produce.rs +++ b/rs/moq-stats/src/produce.rs @@ -1476,7 +1476,7 @@ mod tests { drain.collect(); drain.publish(Instant::now()); let consumer = edge.consume().with_hidden(true); - let mut events = consumer.announced(); + let mut events = consumer.routes(); let (route, active) = next_update(&mut events).await.unwrap(); assert!(active); assert_eq!(route.prefix.as_str(), ".stats/node"); @@ -1615,20 +1615,20 @@ mod tests { } /// The next route and whether it is active. - async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { + async fn next_update(announced: &mut moq_net::origin::Routes) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::origin::RouteEvent::Start(route) + | moq_net::origin::RouteEvent::Update(route) + | moq_net::origin::RouteEvent::Restart(route) + | moq_net::origin::RouteEvent::Takeover { announce: route, .. } => Some((route, true)), + moq_net::origin::RouteEvent::End(route) => Some((route, false)), } } use std::collections::BTreeMap; use moq_net::stats::{Registry, Tier}; - use moq_net::{Instance, Timestamp, announce, broadcast, track}; + use moq_net::{Instance, Timestamp, broadcast, track}; use super::*; use crate::{PrefixFrame, PresenceFrame, TotalsFrame}; @@ -1647,7 +1647,7 @@ mod tests { /// announce (bumping the `_closed` counters). #[allow(dead_code, reason = "held for their drop, never read")] struct Feed { - announced: announce::Consumer, + announced: origin::Routes, source: origin::Publication, consumer: broadcast::Consumer, sub: Option, @@ -1672,7 +1672,7 @@ mod tests { // Egress (publisher side) is tagged; the local publisher stays untagged. let egress = origin.consume().with_stats(ctx); - let mut announced = egress.announced(); + let mut announced = egress.routes(); let source = origin.create_broadcast(path).expect("create_broadcast"); source.announce(origin::Route::default()).expect("announce"); let producer = source.create_track("video", timed()).expect("create_track"); @@ -1716,7 +1716,7 @@ mod tests { /// Awaits the stats announce and returns its broadcast. async fn announced(origin: &origin::Producer) -> (String, moq_net::broadcast::Consumer) { - let mut consumer = origin.consume().with_hidden(true).announced(); + let mut consumer = origin.consume().with_hidden(true).routes(); tokio::time::advance(Duration::from_millis(1)).await; let (update, active) = next_update(&mut consumer).await.expect("expected announce"); assert!(active); @@ -1924,7 +1924,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn restarted_producer_announces_a_new_instance() { let origin = produce_origin(); - let mut events = origin.consume().with_hidden(true).announced(); + let mut events = origin.consume().with_hidden(true).routes(); let first = node_producer(&origin); drive_tick().await; @@ -1953,7 +1953,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn restart_replaces_a_still_announced_instance() { let origin = produce_origin(); - let mut events = origin.consume().with_hidden(true).announced(); + let mut events = origin.consume().with_hidden(true).routes(); let first = node_producer(&origin); let _f1 = feed(first.registry(), Tier::default(), "foo/bar", true, 1, 100).await; @@ -1984,7 +1984,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_publisher_run_after_its_producer_dropped_announces_nothing() { let origin = produce_origin(); - let mut events = origin.consume().with_hidden(true).announced(); + let mut events = origin.consume().with_hidden(true).routes(); let (stale, publisher) = Producer::new( Config::new() @@ -2207,7 +2207,7 @@ mod tests { /// The announce events buffered so far in delivery order, as /// `(path, active, instance)`. Every stats route carries an instance. - fn take_instances(events: &mut announce::Consumer) -> Vec<(String, bool, Instance)> { + fn take_instances(events: &mut origin::Routes) -> Vec<(String, bool, Instance)> { use futures::FutureExt; let mut out = Vec::new(); while let Some(Some((update, active))) = next_update(events).now_or_never() { @@ -2993,7 +2993,7 @@ mod tests { drain.collect(); drain.publish(start); let consumer = edge.consume().with_hidden(true); - let mut announced = consumer.announced(); + let mut announced = consumer.routes(); let (route, active) = next_update(&mut announced).await.unwrap(); assert!(active); assert_eq!(route.prefix.as_str(), ".stats/node"); diff --git a/rs/moq-tokio/examples/clock.rs b/rs/moq-tokio/examples/clock.rs index 8ef3b628925..dc2307b5b26 100644 --- a/rs/moq-tokio/examples/clock.rs +++ b/rs/moq-tokio/examples/clock.rs @@ -112,14 +112,14 @@ async fn main() -> anyhow::Result<()> { tokio::select! { Some(event) = announced.next() => match event { // A restart is a new broadcast: subscribe to it afresh. - announce::Event::Start(update) | announce::Event::Update(update) | announce::Event::Restart(update) | announce::Event::Takeover { announce: update, .. } => { + announce::Event::Present(update) => { tracing::info!(broadcast = %update.prefix, "broadcast is online, subscribing to track"); let broadcast = consumer.request(&update.prefix, None).await?; let track = broadcast .track(&track)?.subscribe(None).await?; clock = Some(Subscriber::new(track)); } - announce::Event::End(update) => { + announce::Event::Absent(update) => { tracing::warn!(broadcast = %update.prefix, "broadcast is offline, waiting..."); } }, diff --git a/rs/moq-tokio/src/client.rs b/rs/moq-tokio/src/client.rs index 39b1c505585..e38c0246269 100644 --- a/rs/moq-tokio/src/client.rs +++ b/rs/moq-tokio/src/client.rs @@ -1187,7 +1187,7 @@ mod tests { let _ = websocket_handle.await; let update = match announcements.next().await.expect("origin closed") { - moq_net::announce::Event::Start(update) => update, + moq_net::announce::Event::Present(update) => update, event => panic!("expected announcement, got {event:?}"), }; assert_eq!(update.prefix.as_str(), "test"); diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index 09f8ee277eb..337cc4062ec 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -3430,6 +3430,7 @@ mod tests { let cam = kio::clock::timeout(WAIT, consumer.follow("cam", None).unwrap().available()) .await .unwrap() + .unwrap() .unwrap(); let mut sub = cam.track("video").unwrap().subscribe(None).await.unwrap(); @@ -3513,6 +3514,7 @@ mod tests { kio::clock::timeout(WAIT, subscriber.consume().follow("late", None).unwrap().available()) .await .expect("the drained session stopped serving") + .unwrap() .unwrap(); assert!( connection @@ -3751,7 +3753,7 @@ mod tests { .await .expect("the broadcast never reached the server") .expect("the server's origin closed"); - assert!(matches!(arrived, moq_net::announce::Event::Start(update) if update.prefix.as_str() == "room")); + assert!(matches!(arrived, moq_net::announce::Event::Present(update) if update.prefix.as_str() == "room")); assert!( futures::FutureExt::now_or_never(dialed.session.closed()).is_none(), "the session closed before the configured token was answered" diff --git a/rs/moq-tokio/src/origin.rs b/rs/moq-tokio/src/origin.rs index cb47828da7b..16e25f9b0c9 100644 --- a/rs/moq-tokio/src/origin.rs +++ b/rs/moq-tokio/src/origin.rs @@ -42,11 +42,8 @@ mod tests { /// The next route and whether it is active. async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::announce::Event::Present(route) => Some((route, true)), + moq_net::announce::Event::Absent(route) => Some((route, false)), } } diff --git a/rs/moq-tokio/src/server.rs b/rs/moq-tokio/src/server.rs index 1ca5c90c0a8..d09d8469b17 100644 --- a/rs/moq-tokio/src/server.rs +++ b/rs/moq-tokio/src/server.rs @@ -2017,11 +2017,8 @@ mod tests { #[cfg(all(feature = "uds", unix))] async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::announce::Event::Present(route) => Some((route, true)), + moq_net::announce::Event::Absent(route) => Some((route, false)), } } diff --git a/rs/moq-tokio/src/webrtc/tests.rs b/rs/moq-tokio/src/webrtc/tests.rs index 8446cc3e598..f008f3ee033 100644 --- a/rs/moq-tokio/src/webrtc/tests.rs +++ b/rs/moq-tokio/src/webrtc/tests.rs @@ -60,7 +60,7 @@ async fn transfer(offerer: &Offerer, answerer: &Answerer, payload: &[u8]) { .expect("handshake timed out"); let read = async { - let Some(moq_net::announce::Event::Start(route)) = announced.next().await else { + let Some(moq_net::announce::Event::Present(route)) = announced.next().await else { panic!("expected the broadcast to be announced"); }; assert_eq!(route.prefix.as_str(), "test"); diff --git a/rs/moq-tokio/tests/integration/backend.rs b/rs/moq-tokio/tests/integration/backend.rs index 425a7781c1a..6808fbba1f8 100644 --- a/rs/moq-tokio/tests/integration/backend.rs +++ b/rs/moq-tokio/tests/integration/backend.rs @@ -1187,10 +1187,7 @@ async fn quic_windows() { /// The next route and whether it is active. async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::announce::Event::Present(route) => Some((route, true)), + moq_net::announce::Event::Absent(route) => Some((route, false)), } } diff --git a/rs/moq-tokio/tests/integration/broadcast.rs b/rs/moq-tokio/tests/integration/broadcast.rs index 71631de674a..0a8a3fe8479 100644 --- a/rs/moq-tokio/tests/integration/broadcast.rs +++ b/rs/moq-tokio/tests/integration/broadcast.rs @@ -806,6 +806,22 @@ async fn broadcast_moq_transport_22_current_group_join() { server_handle.await.expect("server panicked").expect("server failed"); } +/// Wait for the next route change as `(announce, active)`, failing the test on a timeout or a +/// closed origin. +async fn next_route(routes: &mut moq_net::origin::Routes) -> (moq_net::announce::Announce, bool) { + let event = kio::clock::timeout(TIMEOUT, routes.next()) + .await + .expect("announce timeout") + .expect("origin closed"); + match event { + moq_net::origin::RouteEvent::End(announce) => (announce, false), + moq_net::origin::RouteEvent::Start(announce) + | moq_net::origin::RouteEvent::Update(announce) + | moq_net::origin::RouteEvent::Restart(announce) + | moq_net::origin::RouteEvent::Takeover { announce, .. } => (announce, true), + } +} + /// Wait for the next announce event, failing the test on a timeout or a closed origin. async fn next_announce(announcements: &mut moq_net::announce::Consumer) -> (moq_net::announce::Announce, bool) { kio::clock::timeout(TIMEOUT, next_update(announcements)) @@ -1110,7 +1126,7 @@ async fn broadcast_route_migration() { // active metadata updates (the standby's chain taking over). while let Some(event) = announcements.try_next() { assert!( - !matches!(event, moq_net::announce::Event::End(_)), + !matches!(event, moq_net::announce::Event::Absent(_)), "failover must not retract the route" ); } @@ -1444,7 +1460,8 @@ async fn route_reannounce_test(version: Option<&str>) { // ── subscriber (client) ───────────────────────────────────────── let sub_origin = moq_tokio::origin::spawn(); let sub_consumer = sub_origin.consume(); - let mut announcements = sub_consumer.announced(); + // A hop change is a route update, which only the relay-level stream reports. + let mut announcements = sub_consumer.routes(); let mut client_config = moq_tokio::connect::Config::default(); client_config.tls.insecure = Some(true); @@ -1458,7 +1475,7 @@ async fn route_reannounce_test(version: Option<&str>) { .expect("connect timeout") .expect("connect failed"); - let (update, active) = next_announce(&mut announcements).await; + let (update, active) = next_route(&mut announcements).await; assert_eq!(update.prefix.as_str(), "test"); assert!(active, "expected announce"); let initial = update.route; @@ -1485,7 +1502,7 @@ async fn route_reannounce_test(version: Option<&str>) { .expect("update route"); // The subscriber sees the new chain as another active update for the prefix... - let (update, active) = next_announce(&mut announcements).await; + let (update, active) = next_route(&mut announcements).await; assert_eq!(update.prefix.as_str(), "test"); assert!(active, "a restart must not retract the route"); assert_ne!(initial.hops, update.route.hops, "route must change"); @@ -3487,10 +3504,7 @@ async fn abort_carries_its_code_to_the_peer() { /// The next route and whether it is active. async fn next_update(announced: &mut moq_net::announce::Consumer) -> Option<(moq_net::announce::Announce, bool)> { match announced.next().await? { - moq_net::announce::Event::Start(route) - | moq_net::announce::Event::Update(route) - | moq_net::announce::Event::Restart(route) - | moq_net::announce::Event::Takeover { announce: route, .. } => Some((route, true)), - moq_net::announce::Event::End(route) => Some((route, false)), + moq_net::announce::Event::Present(route) => Some((route, true)), + moq_net::announce::Event::Absent(route) => Some((route, false)), } } diff --git a/rs/moq-tokio/tests/integration/datagram_delivery.rs b/rs/moq-tokio/tests/integration/datagram_delivery.rs index 26be2486335..99e93d2b5f8 100644 --- a/rs/moq-tokio/tests/integration/datagram_delivery.rs +++ b/rs/moq-tokio/tests/integration/datagram_delivery.rs @@ -103,7 +103,8 @@ impl Viewer { let broadcast = kio::clock::timeout(TIMEOUT, consumer.follow("room", None).unwrap().available()) .await .expect("resolve timeout") - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let datagrams = broadcast.datagrams("audio").expect("datagram track"); // Answered once the publisher's session reads the track, so what it sends next arrives. kio::clock::timeout(TIMEOUT, datagrams.info()) diff --git a/rs/moq-tokio/tests/integration/max_age.rs b/rs/moq-tokio/tests/integration/max_age.rs index f47a3c08582..c1d89af853e 100644 --- a/rs/moq-tokio/tests/integration/max_age.rs +++ b/rs/moq-tokio/tests/integration/max_age.rs @@ -100,7 +100,7 @@ async fn relay(version: moq_net::Version, javascript: Option) -> anyhow::R .next() .await .ok_or_else(|| anyhow::anyhow!("announcements closed"))?, - moq_net::announce::Event::Start(_) + moq_net::announce::Event::Present(_) ) {} let front = consumer.request("age", None).await?; for (i, age) in AGES.into_iter().enumerate() { diff --git a/rs/moq-tokio/tests/integration/migrate.rs b/rs/moq-tokio/tests/integration/migrate.rs index 85ea7208174..3e3adedc620 100644 --- a/rs/moq-tokio/tests/integration/migrate.rs +++ b/rs/moq-tokio/tests/integration/migrate.rs @@ -106,7 +106,8 @@ fn view(origin: &moq_net::origin::Producer, name: &str) -> tokio::sync::mpsc::Un .expect("follow") .available() .await - .expect("the broadcast resolves"); + .expect("the broadcast resolves") + .unwrap(); // A budget, so a group the move delays is late rather than skipped. let subscription = moq_net::track::Subscription::default() .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(5))); diff --git a/rs/moq-tokio/tests/integration/receive_credit.rs b/rs/moq-tokio/tests/integration/receive_credit.rs index 0cefefda3d1..e2a2d57a7fe 100644 --- a/rs/moq-tokio/tests/integration/receive_credit.rs +++ b/rs/moq-tokio/tests/integration/receive_credit.rs @@ -183,6 +183,7 @@ async fn room(origin: &moq_net::origin::Producer) -> moq_net::broadcast::Consume .await .expect("resolve timeout") .expect("broadcast resolves") + .unwrap() } /// Subscribe to track `name` of `remote` at `priority`. diff --git a/rs/moq-tokio/tests/integration/relay_fill.rs b/rs/moq-tokio/tests/integration/relay_fill.rs index 95f21bf5291..12fe84fd356 100644 --- a/rs/moq-tokio/tests/integration/relay_fill.rs +++ b/rs/moq-tokio/tests/integration/relay_fill.rs @@ -91,7 +91,12 @@ impl Relay { async fn fetch(&mut self, sequence: u64) -> anyhow::Result { self.command("dial", "dialed").await?; let viewer = self.viewers.recv().await.context("no downstream session")?; - let remote = viewer.consume().follow("history", None)?.available().await?; + let remote = viewer + .consume() + .follow("history", None)? + .available() + .await? + .context("offline")?; let mut group = remote.track("video")?.fetch_group(sequence, None).await?; let frame = group.read_frame().await?.context("empty group")?; Ok(String::from_utf8(frame.payload.to_vec())?) @@ -101,7 +106,12 @@ impl Relay { async fn subscribe(&mut self, subscription: Subscription) -> anyhow::Result { self.command("dial", "dialed").await?; let viewer = self.viewers.recv().await.context("no downstream session")?; - let remote = viewer.consume().follow("history", None)?.available().await?; + let remote = viewer + .consume() + .follow("history", None)? + .available() + .await? + .context("offline")?; Ok(remote.track("video")?.subscribe(subscription).await?) } } diff --git a/rs/moq-tokio/tests/integration/subscription_end_integrity.rs b/rs/moq-tokio/tests/integration/subscription_end_integrity.rs index 342ad178f4c..cd7c084ce27 100644 --- a/rs/moq-tokio/tests/integration/subscription_end_integrity.rs +++ b/rs/moq-tokio/tests/integration/subscription_end_integrity.rs @@ -89,7 +89,8 @@ async fn round(drop_session: bool) -> (usize, Option) { .unwrap() .available() .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .unwrap(); let mut sub = remote.track("video").unwrap().subscribe(None).await.expect("subscribe"); let mut got = 0; diff --git a/rs/moq-transcode/examples/transcode.rs b/rs/moq-transcode/examples/transcode.rs index 2e06b578bf8..cfbf624f60d 100644 --- a/rs/moq-transcode/examples/transcode.rs +++ b/rs/moq-transcode/examples/transcode.rs @@ -59,7 +59,7 @@ async fn main() -> anyhow::Result<()> { // subscribes upstream on demand. let mut following = remote.consume().follow(&source_path, None)?; let source = tokio::select! { - resolved = following.available() => resolved.context("source broadcast unavailable")?, + resolved = following.available() => resolved.context("source broadcast unavailable")?.context("went offline before serving")?, closed = session.closed() => { closed.context("session failed before the source broadcast was announced")?; anyhow::bail!("session closed before the source broadcast was announced"); diff --git a/rs/moq-uring/benches/session_lite.rs b/rs/moq-uring/benches/session_lite.rs index 2bd0cb22a0c..8f6e4bdfc1a 100644 --- a/rs/moq-uring/benches/session_lite.rs +++ b/rs/moq-uring/benches/session_lite.rs @@ -181,6 +181,7 @@ mod linux { .available() .await .expect("broadcast resolves") + .unwrap() }; let sub = bc .track("data") diff --git a/rs/moq-uring/tests/integration/receive_credit.rs b/rs/moq-uring/tests/integration/receive_credit.rs index 725a637fd5f..68fad55072e 100644 --- a/rs/moq-uring/tests/integration/receive_credit.rs +++ b/rs/moq-uring/tests/integration/receive_credit.rs @@ -157,7 +157,8 @@ fn partial_frame_gives_way() { consumer.follow("room", None).unwrap().available(), ) .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .expect("the first change is an instance"); let subscribe = |name: &str, priority: i8| { let track = remote.track(name).expect("track"); async move { @@ -309,7 +310,8 @@ fn javascript_partial_frame_gives_way() { consumer.follow("room", None).unwrap().available(), ) .await - .expect("broadcast resolves"); + .expect("broadcast resolves") + .expect("the first change is an instance"); let subscribe = |name: &str, priority: i8| { let track = remote.track(name).expect("track"); async move { diff --git a/rs/moq-uring/tests/integration/session.rs b/rs/moq-uring/tests/integration/session.rs index fa0ccb9dfbf..6cbe0006258 100644 --- a/rs/moq-uring/tests/integration/session.rs +++ b/rs/moq-uring/tests/integration/session.rs @@ -133,6 +133,7 @@ fn lite_session_over_the_worker() { .available() .await .expect("broadcast resolves") + .unwrap() }; let mut track = bc .track("data") @@ -271,6 +272,7 @@ fn two_lite_sessions_share_the_server_socket() { .available() .await .expect("broadcast resolves") + .unwrap() }; let mut track = bc .track("data") diff --git a/rs/moq-uring/tests/integration/web.rs b/rs/moq-uring/tests/integration/web.rs index 57fd5d015ba..b8c5b2673ee 100644 --- a/rs/moq-uring/tests/integration/web.rs +++ b/rs/moq-uring/tests/integration/web.rs @@ -456,6 +456,7 @@ fn lite_session_over_webtransport() { .available() .await .expect("broadcast resolves") + .unwrap() }; let mut track = bc .track("data") diff --git a/swift/MIGRATE.md b/swift/MIGRATE.md index a0d98402281..2b03b4ca9c7 100644 --- a/swift/MIGRATE.md +++ b/swift/MIGRATE.md @@ -4,6 +4,27 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `Following.available()` returns nil once the path goes offline + +`try await following.available()` returns `BroadcastConsumer?`. +After the first call, it returns nil once the followed path goes offline, or, for a `follow(path:instance:)` pinned to an instance, once another instance takes it over; the next call returns whatever serves the path next. The first call never returns nil. +Drop what you read from the previous broadcast either way. A caller that only waits for the first broadcast can take it with `!`. + +### Announcements are a listing + +`AnnounceConsumer` yields `.present` when a path becomes present and `.absent` when it goes, each carrying an `Announce`. +`.start` is `.present` and `.end` is `.absent`. +`.update`, `.restart`, and `.takeover` are gone: a path whose route or publisher instance changes stays present and is not reported again. +Where you requested the path again on a restart, hold `follow(path:)` instead; each `available()` returns the next instance's broadcast. + +```swift +let following = try session.consume.follow(path: "live/camera") +while true { + guard let broadcast = try await following.available() else { continue } // offline until the next instance + play(broadcast) +} +``` + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `Media.Video` and `Media.Audio` gain `warmup`, the catalog's decoder warmup; it defaults to `nil`, so existing initializers still compile. `Media.TrackConsumer` and the audio and video consumers read it from the rendition and start that much earlier. diff --git a/swift/README.md b/swift/README.md index ce520f854d4..0c3d296efb1 100644 --- a/swift/README.md +++ b/swift/README.md @@ -35,14 +35,10 @@ let session = try await client.connect(to: "https://relay.example.com") // origin between both sides. let announced = try session.consume.announced(prefix: "demos/", filter: "*/camera") for try await event in announced { - // `.update` reprices a route and `.end` retracts it. A `.restart` is a new - // publisher run at the same path: request it again. A `.takeover` continues what an - // earlier route served, like a `.start`. - let announcement: Announce - switch event { - case .start(let started), .restart(let started), .takeover(let started, _): announcement = started - default: continue - } + // The listing reports each path once while it stays present, even as its publisher + // restarts; `session.consume.follow(path:)` hands out each new run, or nil once it + // goes offline. + guard case .present(let announcement) = event else { continue } // Prefix stays origin-relative; captures reports what * matched. print("got broadcast \(announcement.prefix)") print("captures \(announcement.captures)") diff --git a/swift/Sources/Moq/Aliases.swift b/swift/Sources/Moq/Aliases.swift index c0b3c4022d4..cb47c30afb4 100644 --- a/swift/Sources/Moq/Aliases.swift +++ b/swift/Sources/Moq/Aliases.swift @@ -62,8 +62,9 @@ public typealias Route = MoqFFI.MoqRoute /// wildcard matched (`captures`, a required list, empty without wildcards), and the `route` serving it. /// Resolve a path with `OriginConsumer.request`. public typealias Announce = MoqFFI.MoqAnnounce -/// What an `AnnounceConsumer` yields: `.start`, `.update`, `.end`, or -/// `.restart` (request the path again), each carrying an `Announce`. +/// What an `AnnounceConsumer` yields: `.present` or `.absent`, each carrying an +/// `Announce`. Another publisher instance taking a path over keeps it present: +/// hold a `Following` to see the broadcast change. public typealias AnnounceEvent = MoqFFI.MoqAnnounceEvent /// Per-subscription delivery preferences: priority, delay, the ranges of the track to /// deliver, and group ordering. diff --git a/swift/Sources/Moq/Origin.swift b/swift/Sources/Moq/Origin.swift index caed7e9a67b..26f0e6f5af9 100644 --- a/swift/Sources/Moq/Origin.swift +++ b/swift/Sources/Moq/Origin.swift @@ -151,8 +151,10 @@ public final class OriginConsumer: Sendable { self.ffi = ffi } - /// Stream routes under a literal prefix matching an optional pattern filter. - /// Paths with a segment starting with `.` below the prefix are left out unless `hidden`. + /// List the paths under a literal prefix matching an optional pattern filter, as each + /// becomes present or absent. A new publisher instance keeps its path present, so use + /// `follow` to see it. Paths with a segment starting with `.` below the prefix are left + /// out unless `hidden`. public func announced(prefix: String = "", filter: String? = nil, hidden: Bool = false) throws -> AnnounceConsumer { AnnounceConsumer(try ffi.announced(config: MoqAnnounceConfig(prefix: prefix, filter: filter, hidden: hidden))) } @@ -173,7 +175,7 @@ public final class OriginConsumer: Sendable { } } -/// A stream of announce events. Iterate directly: +/// A listing of paths as each becomes present or absent. Iterate directly: /// `for try await event in announced { ... }`. /// The sequence ends when the origin closes; cancelling the consuming task /// cancels the subscription. @@ -213,17 +215,18 @@ public final class Following: Sendable { self.ffi = ffi } - /// Suspend until the next publisher instance serves the path, then resolve it. - /// The first call resolves the instance serving the path once one is announced. - /// Each later call waits for another to take over (a restarted publisher, or a - /// replacement session) or for the broadcast to return after it went offline. + /// Suspend until the publisher instance serving the path changes. + /// The first call resolves the instance serving the path once one is announced, and + /// never returns nil. Each later call returns another instance taking over (a + /// restarted publisher, or a replacement session), or nil once the path goes offline + /// (or, with an `instance`, another one takes it over), then whatever serves it next. /// Drop what was read from the previous instance then: its tracks end when its /// session does. Throws `Closed` if cancelled or the origin closes first. - public func available() async throws -> BroadcastConsumer { - BroadcastConsumer(try await ffi.available()) + public func available() async throws -> BroadcastConsumer? { + (try await ffi.available()).map(BroadcastConsumer.init) } - /// Cancel the pending `available()` call. + /// Cancel all current and future `available()` calls. public func cancel() { ffi.cancel() } diff --git a/swift/Tests/MoqTests/SmokeTests.swift b/swift/Tests/MoqTests/SmokeTests.swift index 92686f12e50..fbf8e2301e0 100644 --- a/swift/Tests/MoqTests/SmokeTests.swift +++ b/swift/Tests/MoqTests/SmokeTests.swift @@ -252,13 +252,13 @@ final class SmokeTests: XCTestCase { try publication.announce() let announced = try consumer.announced(prefix: "") - guard case .start(let first) = try await nextRoute(announced) else { + guard case .present(let first) = try await nextRoute(announced) else { return XCTFail("expected an announcement") } XCTAssertEqual(first.prefix, "live") try publication.unannounce() - guard case .end(let retracted) = try await nextRoute(announced) else { + guard case .absent(let retracted) = try await nextRoute(announced) else { return XCTFail("expected a retraction") } XCTAssertEqual(retracted.prefix, "live") @@ -268,7 +268,7 @@ final class SmokeTests: XCTestCase { } catch {} try publication.announce() - guard case .start = try await nextRoute(announced) else { + guard case .present = try await nextRoute(announced) else { return XCTFail("expected a reannouncement") } _ = try await consumer.request(path: "live") @@ -321,35 +321,64 @@ final class SmokeTests: XCTestCase { let chat = try origin.createBroadcast(path: "room/alice/chat") try chat.announce() - guard case .start(let update) = try await nextRoute(announced) else { + guard case .present(let update) = try await nextRoute(announced) else { return XCTFail("expected an announcement") } XCTAssertEqual(update.prefix, "room/alice/chat") XCTAssertEqual(update.captures, ["alice"]) } - func testAnnouncedInstanceRestarts() async throws { + func testNewInstanceRestartsFollowersButStaysListed() async throws { let origin = OriginProducer() - let announced = try origin.consume().announced(prefix: "") + let consumer = origin.consume() + let announced = try consumer.announced(prefix: "") + let following = try consumer.follow(path: "instance") let publication = try origin.createBroadcast(path: "instance") try publication.announce(route: AnnounceRoute()) - guard case .start(let start) = try await nextRoute(announced) else { + guard case .present(let present) = try await nextRoute(announced) else { return XCTFail("expected an announcement") } - XCTAssertFalse(start.route.instance.isEmpty) + XCTAssertFalse(present.route.instance.isEmpty) + let first = try await following.available() + XCTAssertNotNil(first) try publication.announce(route: AnnounceRoute(instance: "camera", epoch: 1)) - guard case .restart(let restart) = try await nextRoute(announced) else { - return XCTFail("expected a restart") + let takeover = try await following.available() + XCTAssertNotNil(takeover) + + // The takeover was not listed, so the next event is the path going absent. + try publication.unannounce() + guard case .absent(let absent) = try await nextRoute(announced) else { + return XCTFail("expected the path to go absent") } - XCTAssertEqual(restart.route.instance, "camera") + XCTAssertEqual(absent.route.instance, "camera") XCTAssertThrowsError(try publication.announce(route: AnnounceRoute(instance: ""))) { error in guard case MoqError.InvalidInstance = error else { return XCTFail("expected InvalidInstance, got \(error)") } } + following.cancel() + } + + func testFollowerReportsOfflineAndTheNextBroadcast() async throws { + let origin = OriginProducer() + let following = try origin.consume().follow(path: "offline") + let publication = try origin.createBroadcast(path: "offline") + + try publication.announce(route: AnnounceRoute()) + let first = try await following.available() + XCTAssertNotNil(first) + + try publication.unannounce() + let offline = try await following.available() + XCTAssertNil(offline) + + try publication.announce(route: AnnounceRoute()) + let returned = try await following.available() + XCTAssertNotNil(returned) + following.cancel() } func testDynamicServesARequestUnderAPrefix() async throws { diff --git a/test/interop/clients/c/subscribe.c b/test/interop/clients/c/subscribe.c index cc4ff43f0da..211d17fd457 100644 --- a/test/interop/clients/c/subscribe.c +++ b/test/interop/clients/c/subscribe.c @@ -267,11 +267,14 @@ static void start_audio(ctx_t *c, int32_t catalog) { } // Delivers a broadcast handle for each publisher instance serving the path, then -// a terminal code (<= 0). Store the first positive handle and wake main; one frame -// is all this client reads, so a later instance's handle is freed unread. -static void on_broadcast(void *ud, int32_t broadcast) { +// the follow's end. Store the first handle and wake main; one frame is all this +// client reads, so a later instance's handle is freed unread. +static void on_broadcast(void *ud, moq_follow_event event, int32_t broadcast) { ctx_t *c = (ctx_t *)ud; - if (broadcast <= 0) { // 0 = ended, negative = error + // Going offline is how a finished broadcast withdraws, and the stored handle's + // tracks report their own end, so there is nothing to do here. + if (event == MOQ_FOLLOW_EVENT_OFFLINE) return; + if (event == MOQ_FOLLOW_EVENT_END) { // 0 = ended, negative = error if (c->finish.enabled) { if (broadcast < 0) fprintf(stderr, "finish broadcast error: %d %s\n", broadcast, moq_error()); pthread_mutex_lock(&c->mu); @@ -364,7 +367,7 @@ static void on_finish_announce(void *ud, int32_t event) { if (read < 0) c->finish.failed = 1; else if (info.prefix_len == strlen(c->finish.path) && memcmp(info.prefix, c->finish.path, info.prefix_len) == 0 && - info.kind == MOQ_ANNOUNCE_KIND_END) c->finish.announce_end = 1; + info.kind == MOQ_ANNOUNCE_KIND_ABSENT) c->finish.announce_end = 1; pthread_cond_broadcast(&c->cv); pthread_mutex_unlock(&c->mu); moq_origin_announced_free((uint32_t)event); @@ -396,9 +399,17 @@ static void on_finish_health_frame(void *ud, int32_t frame) { moq_consume_track_frame_free((uint32_t)frame); } -static void on_finish_health(void *ud, int32_t broadcast) { +static void on_finish_health(void *ud, moq_follow_event event, int32_t broadcast) { ctx_t *c = ud; - if (broadcast <= 0) { + // The session that finished the broadcast must keep serving its health broadcast. + if (event == MOQ_FOLLOW_EVENT_OFFLINE) { + pthread_mutex_lock(&c->mu); + c->finish.failed = 1; + pthread_cond_broadcast(&c->cv); + pthread_mutex_unlock(&c->mu); + return; + } + if (event == MOQ_FOLLOW_EVENT_END) { if (broadcast < 0) fprintf(stderr, "finish health broadcast error: %d %s\n", broadcast, moq_error()); pthread_mutex_lock(&c->mu); c->finish.failed = 1; diff --git a/test/interop/clients/cpp/main.cpp b/test/interop/clients/cpp/main.cpp index 41e897c65e9..2b0b00ed70f 100644 --- a/test/interop/clients/cpp/main.cpp +++ b/test/interop/clients/cpp/main.cpp @@ -350,6 +350,7 @@ int subscribe( : deadline; auto announced = ok(session->consume()->follow(path, std::nullopt), "follow"); auto consumer = until(announced->available(), read_deadline, "available"); + if (!consumer) fail("first available() reported offline"); // The catalog is a live track. A lazy publisher (e.g. the browser, which only encodes on // demand) may announce a rendition in a later update rather than the first snapshot, so @@ -399,13 +400,14 @@ int subscribe( if (frames != expected_frames) fail("wrong finished media frame count"); while (until(catalogs->next(), read_deadline, "catalog EOF")) {} for (;;) { - auto event = until(routes->next(), read_deadline, "announcement END"); + auto event = until(routes->next(), read_deadline, "announcement absent"); if (!event) fail("origin ended before broadcast withdrawal"); - const auto *end = std::get_if(&event->get_variant()); + const auto *end = std::get_if(&event->get_variant()); if (end && end->announce.prefix == path) break; } auto watch = ok(session->consume()->follow(path + ".health", std::nullopt), "health watch"); auto health = until(watch->available(), read_deadline, "health available"); + if (!health) fail("first health available() reported offline"); auto track = until(health->subscribe_track("live", std::nullopt), read_deadline, "health subscribe"); auto group = until(track->next_group(), read_deadline, "health group"); if (!group) fail("missing health group"); diff --git a/test/interop/clients/go/main.go b/test/interop/clients/go/main.go index c433cf4e05e..e0df3ca7f2e 100644 --- a/test/interop/clients/go/main.go +++ b/test/interop/clients/go/main.go @@ -12,7 +12,7 @@ // ffmpeg ... -f h264 - | go-interop publish --url http://localhost:4443 --broadcast b.hang --tone 440,660 // go-interop subscribe --url http://localhost:4443 --broadcast b.hang --timeout 20 --audio-out /tmp/audio --audio-ms 600 // -// --finish verifies a finite tail, natural media/catalog EOF, announcement END, and +// --finish verifies a finite tail, natural media/catalog EOF, the announcement going absent, and // same-session health; --frames sets its exact size. --finish-ack selects publisher // finish mode and names the FIFO releasing teardown after the real linger. // --version offers only that version. --grant waits for the grant the relay sends @@ -380,24 +380,21 @@ func finishSubscribe(ctx context.Context, client *moq.Client, broadcast string, break } } - started := false + present := false for { event, err := announcements.Next(ctx) if err != nil { return err } if event == nil { - return errors.New("announcement stream ended before broadcast END") + return errors.New("announcement stream ended before the broadcast went absent") } - if start, ok := event.(moq.AnnounceEventStart); ok && start.Announce.Prefix == broadcast { - started = true + if added, ok := event.(moq.AnnounceEventPresent); ok && added.Announce.Prefix == broadcast { + present = true } - if restart, ok := event.(moq.AnnounceEventRestart); ok && restart.Announce.Prefix == broadcast { - return errors.New("broadcast restarted during finish") - } - if end, ok := event.(moq.AnnounceEventEnd); ok && end.Announce.Prefix == broadcast { - if !started { - return errors.New("broadcast ended without announcement") + if removed, ok := event.(moq.AnnounceEventAbsent); ok && removed.Announce.Prefix == broadcast { + if !present { + return errors.New("broadcast went absent without being present") } break } diff --git a/test/interop/clients/js-native/finish.ts b/test/interop/clients/js-native/finish.ts index 6544a35a628..8131f4fab5c 100644 --- a/test/interop/clients/js-native/finish.ts +++ b/test/interop/clients/js-native/finish.ts @@ -56,7 +56,7 @@ export async function readFinish( for (;;) { const event = await routes.next(); if (!event) throw new Error("origin ended before broadcast withdrawal"); - if (event.kind === "end" && event.prefix === path) break; + if (event.kind === "absent" && event.prefix === path) break; } let health = healthRequest.active.peek(); while (!health) { diff --git a/test/interop/clients/js-native/goaway.ts b/test/interop/clients/js-native/goaway.ts index f891f075542..dc93aabb2f0 100644 --- a/test/interop/clients/js-native/goaway.ts +++ b/test/interop/clients/js-native/goaway.ts @@ -57,7 +57,7 @@ try { for (;;) { const event = await announcements.next(); assert(event, "replacement announcements ended"); - if (event.kind === "start" && event.prefix === "handover") break; + if (event.kind === "present" && event.prefix === "handover") break; } } finally { announcements.close(); diff --git a/test/interop/clients/python/interop.py b/test/interop/clients/python/interop.py index 59c320f9f32..1f95dba8d76 100644 --- a/test/interop/clients/python/interop.py +++ b/test/interop/clients/python/interop.py @@ -9,7 +9,7 @@ has been decoded to --audio-out. for tone.py to judge (exit 1 on timeout / no data). ---finish verifies a finite tail, natural media/catalog EOF, announcement END, and +--finish verifies a finite tail, natural media/catalog EOF, the announcement going absent, and same-session health; --frames sets its exact size. --finish-ack selects publisher finish mode and names the FIFO releasing teardown after the real linger. --version offers only that version. --grant waits for the grant the relay sends over @@ -184,10 +184,11 @@ async def _catalog_with_media(consumer: moq.BroadcastConsumer) -> moq.media.Cata async def _finish_subscribe(client: moq.Client, broadcast: str, frames: int) -> None: - # Watch the exact path before resolving it, retaining END while media drains. + # Watch the exact path before resolving it, retaining its absence while media drains. async with client.announced(broadcast) as announcements: - async with client.follow(broadcast) as announced: - consumer = await announced + async with client.follow(broadcast) as following: + consumer = await following + assert consumer is not None async with await moq.media.CatalogConsumer.subscribe(consumer) as catalogs: async for catalog in catalogs: if catalog.video: @@ -218,19 +219,18 @@ async def _finish_subscribe(client: moq.Client, broadcast: str, frames: int) -> started = False async for event in announcements: - if isinstance(event, moq.AnnounceEventRestart) and event.announce.prefix == broadcast: - raise RuntimeError("broadcast restarted during finish") - if isinstance(event, moq.AnnounceEventStart) and event.announce.prefix == broadcast: + if isinstance(event, moq.AnnounceEventPresent) and event.announce.prefix == broadcast: started = True - if isinstance(event, moq.AnnounceEventEnd) and event.announce.prefix == broadcast: + if isinstance(event, moq.AnnounceEventAbsent) and event.announce.prefix == broadcast: if not started: raise RuntimeError("broadcast ended without announcement") break else: - raise RuntimeError("announcement stream ended before broadcast END") + raise RuntimeError("announcement stream ended before the broadcast went absent") async with client.follow(f"{broadcast}.health") as announced_health: health = await announced_health + assert health is not None async with await health.subscribe_track("live") as live: seen = False async for group in live: @@ -305,6 +305,7 @@ async def subscribe( return assert audio_out is not None and audio_ms is not None consumer = await asyncio.wait_for(client.follow(broadcast), timeout) + assert consumer is not None catalog = await asyncio.wait_for(_catalog_with_media(consumer), timeout) id, video = next(iter(catalog.video.items())) diff --git a/test/interop/clients/rust/audio.rs b/test/interop/clients/rust/audio.rs index c7be847447c..9ada6204099 100644 --- a/test/interop/clients/rust/audio.rs +++ b/test/interop/clients/rust/audio.rs @@ -41,7 +41,11 @@ async fn main() -> anyhow::Result<()> { .established() .await?; - let broadcast = consumer.follow(&args[2], None)?.available().await?; + let broadcast = consumer + .follow(&args[2], None)? + .available() + .await? + .context("went offline before serving")?; let source = moq_mux::Source::new(consumer, broadcast.clone()); let mut catalogs = moq_mux::catalog::Consumer::<()>::new(&broadcast, CatalogFormat::Hang).await?; // A lazy publisher (the browser) may add audio in a later catalog update. diff --git a/test/interop/clients/rust/finish.rs b/test/interop/clients/rust/finish.rs index 2e98d093d29..576fc541059 100644 --- a/test/interop/clients/rust/finish.rs +++ b/test/interop/clients/rust/finish.rs @@ -117,7 +117,11 @@ async fn main() -> anyhow::Result<()> { ensure!(command == "read\n", "finish read phase was not released"); kio::clock::timeout(args.timeout, async { let mut routes = consumer.announced(); - let broadcast = consumer.follow(&args.broadcast, None)?.available().await?; + let broadcast = consumer + .follow(&args.broadcast, None)? + .available() + .await? + .context("went offline before serving")?; let mut catalogs = moq_mux::catalog::Consumer::<()>::new(&broadcast, moq_mux::catalog::CatalogFormat::Hang).await?; let catalog = loop { @@ -156,7 +160,7 @@ async fn main() -> anyhow::Result<()> { .next() .await .context("origin ended before broadcast withdrawal")?; - if let moq_net::announce::Event::End(announce) = event + if let moq_net::announce::Event::Absent(announce) = event && announce.prefix.as_str() == args.broadcast { break; @@ -165,7 +169,8 @@ async fn main() -> anyhow::Result<()> { let health = consumer .follow(format!("{}.health", args.broadcast), None)? .available() - .await?; + .await? + .context("went offline before serving")?; let mut track = health.track("live")?.subscribe(None).await?; let mut group = track.recv_group().await?.context("no health group")?; let frame = group.read_frame().await?.context("no health frame")?; diff --git a/test/interop/clients/rust/goaway-client.rs b/test/interop/clients/rust/goaway-client.rs index ce9963804a9..9767b441381 100644 --- a/test/interop/clients/rust/goaway-client.rs +++ b/test/interop/clients/rust/goaway-client.rs @@ -41,7 +41,7 @@ async fn main() -> anyhow::Result<()> { "wrong negotiated version" ); let mut following = consumer.follow("handover", None)?; - let broadcast = following.available().await?; + let broadcast = following.available().await?.context("went offline before serving")?; let mut track = broadcast.track("video")?.subscribe(None).await?; let input = child.stdin.as_mut().context("stdin")?; for sequence in 0..2 { @@ -56,14 +56,18 @@ async fn main() -> anyhow::Result<()> { input.write_all(b"first group\n").await?; // The first endpoint's publisher scope excludes this broadcast, so its bytes // prove that the replacement session is serving beyond its SETUP handshake. - let ready = consumer.follow("replacement", None)?.available().await?; + let ready = consumer + .follow("replacement", None)? + .available() + .await? + .context("went offline before serving")?; let mut marker = ready.track("ready")?.subscribe(None).await?; let mut marker = marker.recv_group().await?.context("replacement marker")?; ensure!( marker.read_frame().await?.context("marker frame")?.payload.as_ref() == [42], "wrong marker" ); - let restarted = following.available().await?; + let restarted = following.available().await?.context("went offline before serving")?; ensure!(!restarted.is_clone(&broadcast), "redirect did not restart the instance"); track = restarted.track("video")?.subscribe(None).await?.with_range(1..); input.write_all(b"replacement ready\n").await?; diff --git a/test/interop/clients/rust/hls.rs b/test/interop/clients/rust/hls.rs index 43c0310dc73..96230a940ee 100644 --- a/test/interop/clients/rust/hls.rs +++ b/test/interop/clients/rust/hls.rs @@ -115,7 +115,12 @@ async fn main() -> anyhow::Result<()> { .established() .await?; kio::clock::timeout(Duration::from_secs(30), async { - let broadcast = origin.consume().follow(path, None)?.available().await?; + let broadcast = origin + .consume() + .follow(path, None)? + .available() + .await? + .context("went offline before serving")?; let mut catalogs = moq_mux::catalog::Consumer::<()>::new(&broadcast, moq_mux::catalog::CatalogFormat::Hang).await?; // Both renditions, once the interval is declared. diff --git a/test/interop/clients/rust/tail.rs b/test/interop/clients/rust/tail.rs index 1966f5b051e..b9f6aa2115d 100644 --- a/test/interop/clients/rust/tail.rs +++ b/test/interop/clients/rust/tail.rs @@ -173,7 +173,11 @@ async fn main() -> anyhow::Result<()> { .connect(url) .established() .await?; - let broadcast = consumer.follow(&args[3], None)?.available().await?; + let broadcast = consumer + .follow(&args[3], None)? + .available() + .await? + .context("went offline before serving")?; let remote = broadcast.track("tail")?; if status { // Query before subscribing so draft-18+ must use TRACK_STATUS, diff --git a/test/max-age/client.ts b/test/max-age/client.ts index 68ac059fa12..25ec0c8fe40 100644 --- a/test/max-age/client.ts +++ b/test/max-age/client.ts @@ -40,7 +40,7 @@ try { for (;;) { const event = await announcements.next(); assert(event, "announcements closed"); - if (event.kind === "start" && event.prefix === "age") break; + if (event.kind === "present" && event.prefix === "age") break; } const request = origin.consume().request(Path.from("age")); let broadcast = request.active.peek();