@moq/net 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/connection/accept.js +8 -3
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +5 -2
- package/connection/connect.js.map +1 -1
- package/connection/handshake.d.ts +9 -1
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +18 -14
- package/connection/handshake.js.map +1 -1
- package/ietf/connection.d.ts +7 -1
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +23 -3
- package/ietf/connection.js.map +1 -1
- package/ietf/index.d.ts +1 -0
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +1 -0
- package/ietf/index.js.map +1 -1
- package/ietf/parameters.d.ts +1 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +2 -0
- package/ietf/parameters.js.map +1 -1
- package/ietf/publisher.d.ts +22 -8
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +294 -77
- package/ietf/publisher.js.map +1 -1
- package/ietf/solicit.d.ts +41 -0
- package/ietf/solicit.d.ts.map +1 -0
- package/ietf/solicit.js +47 -0
- package/ietf/solicit.js.map +1 -0
- package/ietf/subscriber.d.ts +4 -0
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +122 -50
- package/ietf/subscriber.js.map +1 -1
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +43 -9
- package/lite/publisher.js.map +1 -1
- package/lite/subscriber.js +3 -3
- package/lite/subscriber.js.map +1 -1
- package/package.json +2 -2
- package/stream.d.ts +46 -2
- package/stream.d.ts.map +1 -1
- package/stream.js +71 -6
- package/stream.js.map +1 -1
- package/track.d.ts +20 -7
- package/track.d.ts.map +1 -1
- package/track.js +63 -27
- package/track.js.map +1 -1
- package/announced.test.d.ts +0 -2
- package/announced.test.d.ts.map +0 -1
- package/announced.test.js +0 -37
- package/announced.test.js.map +0 -1
- package/broadcast.test.d.ts +0 -2
- package/broadcast.test.d.ts.map +0 -1
- package/broadcast.test.js +0 -243
- package/broadcast.test.js.map +0 -1
- package/connection/browser.test.d.ts +0 -2
- package/connection/browser.test.d.ts.map +0 -1
- package/connection/browser.test.js +0 -29
- package/connection/browser.test.js.map +0 -1
- package/connection/cert.test.d.ts +0 -2
- package/connection/cert.test.d.ts.map +0 -1
- package/connection/cert.test.js +0 -26
- package/connection/cert.test.js.map +0 -1
- package/connection/connect.test.d.ts +0 -2
- package/connection/connect.test.d.ts.map +0 -1
- package/connection/connect.test.js +0 -118
- package/connection/connect.test.js.map +0 -1
- package/connection/reload.test.d.ts +0 -2
- package/connection/reload.test.d.ts.map +0 -1
- package/connection/reload.test.js +0 -160
- package/connection/reload.test.js.map +0 -1
- package/connection/stats.test.d.ts +0 -2
- package/connection/stats.test.d.ts.map +0 -1
- package/connection/stats.test.js +0 -120
- package/connection/stats.test.js.map +0 -1
- package/error.test.d.ts +0 -2
- package/error.test.d.ts.map +0 -1
- package/error.test.js +0 -89
- package/error.test.js.map +0 -1
- package/group.test.d.ts +0 -2
- package/group.test.d.ts.map +0 -1
- package/group.test.js +0 -152
- package/group.test.js.map +0 -1
- package/ietf/aliases.test.d.ts +0 -2
- package/ietf/aliases.test.d.ts.map +0 -1
- package/ietf/aliases.test.js +0 -30
- package/ietf/aliases.test.js.map +0 -1
- package/ietf/ietf.test.d.ts +0 -2
- package/ietf/ietf.test.d.ts.map +0 -1
- package/ietf/ietf.test.js +0 -983
- package/ietf/ietf.test.js.map +0 -1
- package/ietf/priority.test.d.ts +0 -2
- package/ietf/priority.test.d.ts.map +0 -1
- package/ietf/priority.test.js +0 -18
- package/ietf/priority.test.js.map +0 -1
- package/integration.test.d.ts +0 -2
- package/integration.test.d.ts.map +0 -1
- package/integration.test.js +0 -1006
- package/integration.test.js.map +0 -1
- package/lite/announce.test.d.ts +0 -2
- package/lite/announce.test.d.ts.map +0 -1
- package/lite/announce.test.js +0 -91
- package/lite/announce.test.js.map +0 -1
- package/lite/datagram.test.d.ts +0 -2
- package/lite/datagram.test.d.ts.map +0 -1
- package/lite/datagram.test.js +0 -26
- package/lite/datagram.test.js.map +0 -1
- package/lite/datagram_stream.test.d.ts +0 -2
- package/lite/datagram_stream.test.d.ts.map +0 -1
- package/lite/datagram_stream.test.js +0 -15
- package/lite/datagram_stream.test.js.map +0 -1
- package/lite/fetch.test.d.ts +0 -2
- package/lite/fetch.test.d.ts.map +0 -1
- package/lite/fetch.test.js +0 -41
- package/lite/fetch.test.js.map +0 -1
- package/lite/priority.test.d.ts +0 -2
- package/lite/priority.test.d.ts.map +0 -1
- package/lite/priority.test.js +0 -77
- package/lite/priority.test.js.map +0 -1
- package/lite/publisher.test.d.ts +0 -2
- package/lite/publisher.test.d.ts.map +0 -1
- package/lite/publisher.test.js +0 -334
- package/lite/publisher.test.js.map +0 -1
- package/lite/setup.test.d.ts +0 -2
- package/lite/setup.test.d.ts.map +0 -1
- package/lite/setup.test.js +0 -134
- package/lite/setup.test.js.map +0 -1
- package/lite/subscribe.test.d.ts +0 -2
- package/lite/subscribe.test.d.ts.map +0 -1
- package/lite/subscribe.test.js +0 -110
- package/lite/subscribe.test.js.map +0 -1
- package/lite/subscriber.test.d.ts +0 -2
- package/lite/subscriber.test.d.ts.map +0 -1
- package/lite/subscriber.test.js +0 -108
- package/lite/subscriber.test.js.map +0 -1
- package/lite/track.test.d.ts +0 -2
- package/lite/track.test.d.ts.map +0 -1
- package/lite/track.test.js +0 -55
- package/lite/track.test.js.map +0 -1
- package/path.test.d.ts +0 -2
- package/path.test.d.ts.map +0 -1
- package/path.test.js +0 -210
- package/path.test.js.map +0 -1
- package/stream.test.d.ts +0 -2
- package/stream.test.d.ts.map +0 -1
- package/stream.test.js +0 -252
- package/stream.test.js.map +0 -1
- package/time.test.d.ts +0 -2
- package/time.test.d.ts.map +0 -1
- package/time.test.js +0 -12
- package/time.test.js.map +0 -1
- package/track.test.d.ts +0 -2
- package/track.test.d.ts.map +0 -1
- package/track.test.js +0 -212
- package/track.test.js.map +0 -1
- package/util/timeout.test.d.ts +0 -2
- package/util/timeout.test.d.ts.map +0 -1
- package/util/timeout.test.js +0 -18
- package/util/timeout.test.js.map +0 -1
- package/varint.test.d.ts +0 -2
- package/varint.test.d.ts.map +0 -1
- package/varint.test.js +0 -106
- package/varint.test.js.map +0 -1
package/ietf/publisher.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Signal } from "@moq/signals";
|
|
|
3
3
|
import { error, reason } from "../error.js";
|
|
4
4
|
import * as Path from "../path.js";
|
|
5
5
|
import { Writer } from "../stream.js";
|
|
6
|
+
import { withTimeout } from "../util/timeout.js";
|
|
6
7
|
import { Frame, Group as GroupMessage } from "./object.js";
|
|
7
8
|
import { fromWire } from "./priority.js";
|
|
8
9
|
import { PublishDone } from "./publish.js";
|
|
@@ -12,6 +13,20 @@ import { SubscribeError, SubscribeOk } from "./subscribe.js";
|
|
|
12
13
|
import { SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, SubscribeNamespaceOk, } from "./subscribe_namespace.js";
|
|
13
14
|
import { TrackStatus } from "./track.js";
|
|
14
15
|
import { Version } from "./version.js";
|
|
16
|
+
/** First wait before re-offering a namespace the peer refused or we couldn't open for. */
|
|
17
|
+
const RETRY_BASE = 100;
|
|
18
|
+
/** Ceiling on that wait. The loop retries for the life of the session, so it must not spin. */
|
|
19
|
+
const RETRY_MAX = 5000;
|
|
20
|
+
/**
|
|
21
|
+
* How long one advertisement may take to be answered. Matches the Rust publisher, and the
|
|
22
|
+
* peer accepting the stream is only half the exchange: one it never answers on holds the
|
|
23
|
+
* loop just as effectively as one it never grants.
|
|
24
|
+
*/
|
|
25
|
+
const ADVERTISE_TIMEOUT_MS = 5000;
|
|
26
|
+
/** Sleep `delay`, jittered, so a relay's namespaces don't all retry on the same tick. */
|
|
27
|
+
function retryAfter(delay) {
|
|
28
|
+
return new Promise((resolve) => setTimeout(resolve, delay * (0.5 + Math.random() / 2)));
|
|
29
|
+
}
|
|
15
30
|
/**
|
|
16
31
|
* Handles publishing broadcasts using moq-transport protocol.
|
|
17
32
|
* Uses the stream-per-request pattern (real bidi streams for v17, virtual for v14-v16).
|
|
@@ -21,6 +36,7 @@ import { Version } from "./version.js";
|
|
|
21
36
|
export class Publisher {
|
|
22
37
|
#quic;
|
|
23
38
|
#session;
|
|
39
|
+
#requiresSolicitation;
|
|
24
40
|
// Our published broadcasts.
|
|
25
41
|
// It's a signal so we can live update any subscribe_namespace streams.
|
|
26
42
|
#broadcasts = new Signal(new Map());
|
|
@@ -28,17 +44,19 @@ export class Publisher {
|
|
|
28
44
|
* Creates a new Publisher instance.
|
|
29
45
|
* @param quic - The WebTransport session (for uni streams)
|
|
30
46
|
* @param session - The session abstraction for bidi streams and request IDs
|
|
47
|
+
* @param requiresSolicitation - Whether the peer's SETUP asked to be told on request
|
|
31
48
|
*
|
|
32
49
|
* @internal
|
|
33
50
|
*/
|
|
34
|
-
constructor(quic, session) {
|
|
51
|
+
constructor(quic, session, requiresSolicitation) {
|
|
35
52
|
this.#quic = quic;
|
|
36
53
|
this.#session = session;
|
|
54
|
+
this.#requiresSolicitation = requiresSolicitation;
|
|
37
55
|
}
|
|
38
56
|
/**
|
|
39
57
|
* Publishes a broadcast with any associated tracks.
|
|
40
|
-
* The namespace is
|
|
41
|
-
* (see {@link
|
|
58
|
+
* The namespace is advertised with an unsolicited PUBLISH_NAMESPACE, or on request
|
|
59
|
+
* if the peer asked for that (see {@link runPublishNamespaces}).
|
|
42
60
|
*/
|
|
43
61
|
publish(path, broadcast) {
|
|
44
62
|
this.#broadcasts.mutate((broadcasts) => {
|
|
@@ -102,13 +120,27 @@ export class Publisher {
|
|
|
102
120
|
});
|
|
103
121
|
await ok.encode(stream.writer, version);
|
|
104
122
|
console.debug(`publish ok: broadcast=${name} track=${track.name}`);
|
|
123
|
+
// Cancels groups still queued for a stream slot. Only the subscriber leaving counts:
|
|
124
|
+
// a track that ran out of groups still has to flush the ones already queued, and we
|
|
125
|
+
// close the stream ourselves below to say so.
|
|
126
|
+
let finished = false;
|
|
127
|
+
let unsubscribe;
|
|
128
|
+
const unsubscribed = new Promise((resolve) => {
|
|
129
|
+
unsubscribe = resolve;
|
|
130
|
+
});
|
|
131
|
+
void stream.reader.closed.then(() => {
|
|
132
|
+
if (!finished)
|
|
133
|
+
unsubscribe();
|
|
134
|
+
},
|
|
135
|
+
// A reset is always the peer.
|
|
136
|
+
() => unsubscribe());
|
|
105
137
|
// Serve track groups, racing with stream close (= Unsubscribe)
|
|
106
138
|
const serving = (async () => {
|
|
107
139
|
for (;;) {
|
|
108
140
|
const group = await track.recvGroup();
|
|
109
141
|
if (!group)
|
|
110
142
|
return;
|
|
111
|
-
void this.#runGroup(msg.requestId, group, timescale);
|
|
143
|
+
void this.#runGroup({ requestId: msg.requestId, group, timescale, unsubscribed });
|
|
112
144
|
}
|
|
113
145
|
})();
|
|
114
146
|
await Promise.race([serving, stream.reader.closed]);
|
|
@@ -128,6 +160,10 @@ export class Publisher {
|
|
|
128
160
|
// Stream might already be closed by peer
|
|
129
161
|
}
|
|
130
162
|
}
|
|
163
|
+
// Only now is the close below ours. Claiming it any earlier would read a peer FIN
|
|
164
|
+
// that lands while PublishDone is still going out as our own completion, leaving
|
|
165
|
+
// queued groups to open for a subscriber that has already left.
|
|
166
|
+
finished = true;
|
|
131
167
|
stream.close();
|
|
132
168
|
}
|
|
133
169
|
catch (err) {
|
|
@@ -142,9 +178,22 @@ export class Publisher {
|
|
|
142
178
|
/**
|
|
143
179
|
* Runs a group and sends its frames using ObjectStream (Subgroup delivery mode).
|
|
144
180
|
*/
|
|
145
|
-
async #runGroup(
|
|
181
|
+
async #runGroup(options) {
|
|
182
|
+
const { requestId, group, timescale, unsubscribed } = options;
|
|
146
183
|
try {
|
|
147
|
-
|
|
184
|
+
// One stream per group is faster than a peer at its limit can retire them, so this
|
|
185
|
+
// is the one path that doesn't wait for a slot: the transport would serve the opens
|
|
186
|
+
// in the order we asked, which is oldest-first, exactly backwards for live media.
|
|
187
|
+
// Failing here drops the group and lets the next one compete for the next slot.
|
|
188
|
+
const stream = await Writer.tryOpen(this.#quic, {
|
|
189
|
+
cancel: unsubscribed,
|
|
190
|
+
version: this.#session.version,
|
|
191
|
+
waitUntilAvailable: false,
|
|
192
|
+
});
|
|
193
|
+
if (!stream) {
|
|
194
|
+
group.close(new Error("no stream slot"));
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
148
197
|
const header = new GroupMessage({
|
|
149
198
|
trackAlias: requestId,
|
|
150
199
|
groupId: group.sequence,
|
|
@@ -180,11 +229,11 @@ export class Publisher {
|
|
|
180
229
|
/**
|
|
181
230
|
* Handles an incoming SUBSCRIBE_NAMESPACE on a bidi stream.
|
|
182
231
|
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
232
|
+
* This carries the advertisements only when the peer asked to be told on request
|
|
233
|
+
* (MoQ Solicit); otherwise {@link runPublishNamespaces} has already announced
|
|
234
|
+
* everything and repeating it here would leave the peer holding two sources for one
|
|
235
|
+
* broadcast. Draft-16+ streams Namespace entries inline; draft-14/15 predate those
|
|
236
|
+
* messages, so each advertisement is a PUBLISH_NAMESPACE request of its own.
|
|
188
237
|
*
|
|
189
238
|
* @internal
|
|
190
239
|
*/
|
|
@@ -208,61 +257,90 @@ export class Publisher {
|
|
|
208
257
|
});
|
|
209
258
|
await ok.encode(stream.writer, version);
|
|
210
259
|
}
|
|
260
|
+
if (!this.#requiresSolicitation) {
|
|
261
|
+
// Already announced, unasked. Hold the stream open until the peer is done.
|
|
262
|
+
await stream.reader.closed;
|
|
263
|
+
stream.close();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
// Reports whether the peer now holds the namespace: an inline entry always
|
|
267
|
+
// lands, but a PUBLISH_NAMESPACE request can be declined.
|
|
211
268
|
const advertise = async (suffix) => {
|
|
212
269
|
if (legacy) {
|
|
213
|
-
await this.#advertise(prefix, suffix, requests);
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
await stream.writer.u53(SubscribeNamespaceEntry.id);
|
|
217
|
-
await new SubscribeNamespaceEntry({ suffix }).encode(stream.writer, version);
|
|
270
|
+
return await this.#advertise(Path.join(prefix, suffix), requests, refused);
|
|
218
271
|
}
|
|
272
|
+
await stream.writer.u53(SubscribeNamespaceEntry.id);
|
|
273
|
+
await new SubscribeNamespaceEntry({ suffix }).encode(stream.writer, version);
|
|
274
|
+
return true;
|
|
219
275
|
};
|
|
220
276
|
const withdraw = async (suffix) => {
|
|
221
277
|
if (legacy) {
|
|
222
|
-
await this.#withdraw(suffix, requests);
|
|
278
|
+
await this.#withdraw(Path.join(prefix, suffix), requests);
|
|
223
279
|
}
|
|
224
280
|
else {
|
|
225
281
|
await stream.writer.u53(SubscribeNamespaceEntryDone.id);
|
|
226
282
|
await new SubscribeNamespaceEntryDone({ suffix }).encode(stream.writer, version);
|
|
227
283
|
}
|
|
228
284
|
};
|
|
229
|
-
// Advertise the currently published broadcasts under the prefix.
|
|
230
285
|
let active = new Set();
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
continue;
|
|
235
|
-
active.add(suffix);
|
|
236
|
-
}
|
|
237
|
-
for (const suffix of active) {
|
|
238
|
-
await advertise(suffix);
|
|
239
|
-
}
|
|
240
|
-
// Wait for updates to the broadcasts.
|
|
286
|
+
let retry = 0;
|
|
287
|
+
// What the peer refused, and whether coming back is worth anything.
|
|
288
|
+
const refused = new Map();
|
|
241
289
|
for (;;) {
|
|
290
|
+
// Subscribe BEFORE reconciling, for the same reason as
|
|
291
|
+
// {@link runPublishNamespaces}: a publish landing while an advertisement
|
|
292
|
+
// waits for its reply only notifies listeners already registered.
|
|
242
293
|
// TODO Make a better helper within Signals.
|
|
243
294
|
let dispose;
|
|
244
295
|
const changed = new Promise((resolve) => {
|
|
245
296
|
dispose = this.#broadcasts.changed(resolve);
|
|
246
297
|
});
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (!broadcasts)
|
|
298
|
+
const broadcasts = this.#broadcasts.peek();
|
|
299
|
+
if (!broadcasts) {
|
|
300
|
+
dispose();
|
|
251
301
|
break;
|
|
252
|
-
|
|
302
|
+
}
|
|
303
|
+
const updated = new Set();
|
|
253
304
|
for (const name of broadcasts.keys()) {
|
|
254
305
|
const suffix = Path.stripPrefix(prefix, name);
|
|
255
306
|
if (suffix === null)
|
|
256
307
|
continue;
|
|
257
|
-
|
|
308
|
+
updated.add(suffix);
|
|
258
309
|
}
|
|
259
|
-
|
|
260
|
-
|
|
310
|
+
// A namespace that is gone takes its refusal with it, so re-announcing the
|
|
311
|
+
// path offers it again.
|
|
312
|
+
const live = new Set([...updated].map((suffix) => Path.join(prefix, suffix)));
|
|
313
|
+
for (const path of [...refused.keys()]) {
|
|
314
|
+
if (!live.has(path))
|
|
315
|
+
refused.delete(path);
|
|
261
316
|
}
|
|
262
|
-
|
|
317
|
+
// Track what the peer holds rather than what we attempted: a declined
|
|
318
|
+
// advertisement stays out of `held`, so the next turn retries it instead of
|
|
319
|
+
// believing the namespace is already up.
|
|
320
|
+
const held = new Set(active);
|
|
321
|
+
for (const added of updated.difference(active)) {
|
|
322
|
+
if (this.#offerable(Path.join(prefix, added), refused)) {
|
|
323
|
+
if (await advertise(added))
|
|
324
|
+
held.add(added);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
for (const removed of active.difference(updated)) {
|
|
263
328
|
await withdraw(removed);
|
|
329
|
+
held.delete(removed);
|
|
264
330
|
}
|
|
265
|
-
active =
|
|
331
|
+
active = held;
|
|
332
|
+
// Whatever we wanted up and could not get up, as {@link runPublishNamespaces}
|
|
333
|
+
// does: only a legacy request can be declined, and nothing about the peer
|
|
334
|
+
// starting to answer raises a signal this loop is watching.
|
|
335
|
+
const outstanding = [...updated.difference(active)].some((suffix) => this.#pending(Path.join(prefix, suffix), refused));
|
|
336
|
+
retry = outstanding ? Math.min(retry ? retry * 2 : RETRY_BASE, RETRY_MAX) : 0;
|
|
337
|
+
// Wait for the next change, or for the peer to unsubscribe.
|
|
338
|
+
const next = await (retry
|
|
339
|
+
? Promise.race([changed, stream.reader.closed, retryAfter(retry).then(() => broadcasts)])
|
|
340
|
+
: Promise.race([changed, stream.reader.closed]));
|
|
341
|
+
dispose();
|
|
342
|
+
if (!next)
|
|
343
|
+
break;
|
|
266
344
|
}
|
|
267
345
|
stream.close();
|
|
268
346
|
}
|
|
@@ -272,64 +350,203 @@ export class Publisher {
|
|
|
272
350
|
stream.abort(e);
|
|
273
351
|
}
|
|
274
352
|
finally {
|
|
275
|
-
// This subscription's advertisements die with it
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
await this.#withdraw(suffix, requests);
|
|
353
|
+
// This subscription's advertisements die with it.
|
|
354
|
+
for (const path of [...requests.keys()]) {
|
|
355
|
+
await this.#withdraw(path, requests);
|
|
279
356
|
}
|
|
280
357
|
}
|
|
281
358
|
}
|
|
282
359
|
/**
|
|
283
|
-
* Advertise
|
|
284
|
-
*
|
|
285
|
-
*
|
|
360
|
+
* Advertise every published broadcast with an unsolicited PUBLISH_NAMESPACE, until
|
|
361
|
+
* the publisher is closed.
|
|
362
|
+
*
|
|
363
|
+
* The peers that never send SUBSCRIBE_NAMESPACE are exactly the ones expecting a
|
|
364
|
+
* publisher to announce itself, so announcing is the default. A peer that would
|
|
365
|
+
* rather ask says so in its SETUP (MoQ Solicit) and this does nothing, leaving
|
|
366
|
+
* {@link runSubscribeNamespace} to carry the advertisements instead. Exactly one of
|
|
367
|
+
* the two is live, so the peer never hears a namespace twice.
|
|
368
|
+
*
|
|
369
|
+
* @internal
|
|
286
370
|
*/
|
|
287
|
-
async
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
if (requestId === undefined)
|
|
371
|
+
async runPublishNamespaces() {
|
|
372
|
+
if (this.#requiresSolicitation) {
|
|
373
|
+
// The peer asked to be told on request; runSubscribeNamespace answers it.
|
|
291
374
|
return;
|
|
292
|
-
|
|
375
|
+
}
|
|
376
|
+
// The open PUBLISH_NAMESPACE request per advertised path.
|
|
377
|
+
const requests = new Map();
|
|
293
378
|
try {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
379
|
+
let active = new Set();
|
|
380
|
+
let retry = 0;
|
|
381
|
+
// What the peer refused, and whether coming back is worth anything.
|
|
382
|
+
const refused = new Map();
|
|
383
|
+
for (;;) {
|
|
384
|
+
// Subscribe BEFORE reconciling. Each advertisement below waits a round trip
|
|
385
|
+
// for the peer's reply, and a publish that lands in that window notifies
|
|
386
|
+
// only the listeners already registered; one created afterwards would sleep
|
|
387
|
+
// through it and leave the namespace unadvertised until something unrelated
|
|
388
|
+
// changed.
|
|
389
|
+
// TODO Make a better helper within Signals.
|
|
390
|
+
let dispose;
|
|
391
|
+
const changed = new Promise((resolve) => {
|
|
392
|
+
dispose = this.#broadcasts.changed(resolve);
|
|
393
|
+
});
|
|
394
|
+
const broadcasts = this.#broadcasts.peek();
|
|
395
|
+
if (!broadcasts) {
|
|
396
|
+
dispose();
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
const updated = new Set(broadcasts.keys());
|
|
400
|
+
// A namespace that is gone takes its refusal with it, so re-announcing the
|
|
401
|
+
// path offers it again. Rust gets this by rebuilding the watched entry.
|
|
402
|
+
for (const path of [...refused.keys()]) {
|
|
403
|
+
if (!updated.has(path))
|
|
404
|
+
refused.delete(path);
|
|
405
|
+
}
|
|
406
|
+
for (const added of updated.difference(active)) {
|
|
407
|
+
if (this.#offerable(added, refused)) {
|
|
408
|
+
await this.#advertise(added, requests, refused);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
for (const removed of active.difference(updated)) {
|
|
412
|
+
await this.#withdraw(removed, requests);
|
|
413
|
+
}
|
|
414
|
+
// What the peer holds, not what we attempted: a declined PUBLISH_NAMESPACE
|
|
415
|
+
// leaves no request behind, so it stays outstanding below.
|
|
416
|
+
active = new Set(requests.keys());
|
|
417
|
+
// Whatever we wanted up and could not get up. Stream credit freeing, a
|
|
418
|
+
// transient failure clearing, or the peer starting to answer raises no
|
|
419
|
+
// signal of its own, so the only way back is to ask again on a timer.
|
|
420
|
+
const outstanding = [...updated.difference(active)].some((path) => this.#pending(path, refused));
|
|
421
|
+
retry = outstanding ? Math.min(retry ? retry * 2 : RETRY_BASE, RETRY_MAX) : 0;
|
|
422
|
+
// Wait for the next change, which has already fired if one landed above.
|
|
423
|
+
const next = await (retry
|
|
424
|
+
? Promise.race([changed, retryAfter(retry).then(() => broadcasts)])
|
|
425
|
+
: changed);
|
|
426
|
+
dispose();
|
|
427
|
+
if (!next)
|
|
428
|
+
break;
|
|
305
429
|
}
|
|
306
|
-
|
|
307
|
-
|
|
430
|
+
}
|
|
431
|
+
catch (err) {
|
|
432
|
+
// Nothing restarts this loop, so whatever got us here cost the session its
|
|
433
|
+
// discovery. Not a debug-level event.
|
|
434
|
+
console.warn(`publish_namespace loop failed: ${reason(error(err))}`);
|
|
435
|
+
}
|
|
436
|
+
finally {
|
|
437
|
+
// Close out every open PUBLISH_NAMESPACE request.
|
|
438
|
+
for (const path of [...requests.keys()]) {
|
|
439
|
+
await this.#withdraw(path, requests);
|
|
308
440
|
}
|
|
309
|
-
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Whether a namespace may be offered to the peer right now.
|
|
445
|
+
*
|
|
446
|
+
* A peer that asked never to be offered it again means it, whatever brought us back;
|
|
447
|
+
* one that named a minimum wait gets it, even when our own backoff comes round sooner.
|
|
448
|
+
*/
|
|
449
|
+
#offerable(path, refused) {
|
|
450
|
+
const entry = refused.get(path);
|
|
451
|
+
if (entry === undefined)
|
|
452
|
+
return true;
|
|
453
|
+
return entry !== "never" && Date.now() >= entry;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Whether the loop should keep coming back to a namespace the peer does not hold.
|
|
457
|
+
*
|
|
458
|
+
* Distinct from {@link offerable}, and the difference is what arms the retry: a
|
|
459
|
+
* namespace waiting out a minimum is not offerable yet but is still pending, and
|
|
460
|
+
* gating the timer on offerable instead would disarm it for exactly the wait it is
|
|
461
|
+
* supposed to be counting. Only a refusal that forbids retrying ends it.
|
|
462
|
+
*/
|
|
463
|
+
#pending(path, refused) {
|
|
464
|
+
return refused.get(path) !== "never";
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Advertise one namespace on its own PUBLISH_NAMESPACE request. A declined request
|
|
468
|
+
* is logged and skipped: a peer that wants none of this rejects each one and stays
|
|
469
|
+
* connected.
|
|
470
|
+
*
|
|
471
|
+
* `refused` records what a refusal said about coming back, so a peer that asked not to
|
|
472
|
+
* be offered a namespace again is not re-offered it by the retry above.
|
|
473
|
+
*/
|
|
474
|
+
async #advertise(path, requests, refused) {
|
|
475
|
+
const requestId = await this.#session.nextRequestId();
|
|
476
|
+
if (requestId === undefined)
|
|
477
|
+
return false;
|
|
478
|
+
// Opening is inside the try because it fails like any other advertisement does:
|
|
479
|
+
// a peer out of stream credit rejects here, and letting that escape would unwind
|
|
480
|
+
// the announce loop and take discovery down for the whole session, rather than
|
|
481
|
+
// costing this one namespace a turn.
|
|
482
|
+
let request;
|
|
483
|
+
try {
|
|
484
|
+
request = await this.#session.openBi();
|
|
485
|
+
const stream = request;
|
|
486
|
+
// Bounded, like the subscriber's own request: the loop is single-threaded over
|
|
487
|
+
// advertisements, so a peer that takes the stream and answers nothing strands
|
|
488
|
+
// every publish and withdrawal behind it. Timing out costs this namespace a
|
|
489
|
+
// turn and leaves it outstanding, which the retry above re-offers.
|
|
490
|
+
await withTimeout((async () => {
|
|
491
|
+
await stream.writer.u53(PublishNamespace.id);
|
|
492
|
+
const msg = new PublishNamespace({ requestId, trackNamespace: path });
|
|
493
|
+
await msg.encode(stream.writer, this.#session.version);
|
|
494
|
+
// Read response (RequestOk and PublishNamespaceOk share 0x07)
|
|
495
|
+
const respTypeId = await stream.reader.u53();
|
|
496
|
+
if (respTypeId === RequestError.id) {
|
|
497
|
+
// The peer named how long to stay away, in milliseconds. Draft-14/15
|
|
498
|
+
// carry no such field, so a 0 there says nothing and our own backoff
|
|
499
|
+
// stands; everywhere else 0 means it does not want this again.
|
|
500
|
+
const err = await RequestError.decode(stream.reader, this.#session.version);
|
|
501
|
+
const legacy = this.#session.version === Version.DRAFT_14 || this.#session.version === Version.DRAFT_15;
|
|
502
|
+
if (!legacy) {
|
|
503
|
+
refused.set(path, err.retryInterval === 0n ? "never" : Date.now() + Number(err.retryInterval));
|
|
504
|
+
}
|
|
505
|
+
throw new Error(`PublishNamespace rejected: ${err.errorCode} ${err.reasonPhrase}`);
|
|
506
|
+
}
|
|
507
|
+
if (respTypeId !== RequestOk.id) {
|
|
508
|
+
throw new Error(`PublishNamespace rejected: typeId=0x${respTypeId.toString(16)}`);
|
|
509
|
+
}
|
|
510
|
+
// Draft-14 sends PublishNamespaceOk (requestId only, no parameters)
|
|
511
|
+
if (this.#session.version === Version.DRAFT_14) {
|
|
512
|
+
await PublishNamespaceOk.decode(stream.reader, this.#session.version);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
await RequestOk.decode(stream.reader, this.#session.version);
|
|
516
|
+
}
|
|
517
|
+
})(), ADVERTISE_TIMEOUT_MS, `advertisement timed out after ${ADVERTISE_TIMEOUT_MS}ms waiting for the peer's answer`);
|
|
518
|
+
requests.set(path, { path, requestId, stream: request });
|
|
519
|
+
return true;
|
|
310
520
|
}
|
|
311
521
|
catch (err) {
|
|
312
522
|
const e = error(err);
|
|
313
523
|
console.warn(`announce failed: broadcast=${path} error=${reason(e)}`);
|
|
314
|
-
request
|
|
524
|
+
request?.abort(e);
|
|
525
|
+
return false;
|
|
315
526
|
}
|
|
316
527
|
}
|
|
317
528
|
/**
|
|
318
|
-
* Close out a namespace's PUBLISH_NAMESPACE request with PUBLISH_NAMESPACE_DONE
|
|
319
|
-
* (draft-14/15).
|
|
529
|
+
* Close out a namespace's PUBLISH_NAMESPACE request with PUBLISH_NAMESPACE_DONE.
|
|
320
530
|
*/
|
|
321
|
-
async #withdraw(
|
|
322
|
-
const request = requests.get(
|
|
531
|
+
async #withdraw(path, requests) {
|
|
532
|
+
const request = requests.get(path);
|
|
323
533
|
if (!request)
|
|
324
534
|
return;
|
|
325
|
-
requests.delete(
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
535
|
+
requests.delete(path);
|
|
536
|
+
// Draft-17+ removed PUBLISH_NAMESPACE_DONE: the close below is the whole
|
|
537
|
+
// withdrawal. Sending it anyway puts the type on the wire before the body throws,
|
|
538
|
+
// and a receiver reading 0x09 there has no choice but to treat it as a protocol
|
|
539
|
+
// violation.
|
|
540
|
+
const version = this.#session.version;
|
|
541
|
+
if (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) {
|
|
542
|
+
try {
|
|
543
|
+
await request.stream.writer.u53(PublishNamespaceDone.id);
|
|
544
|
+
const done = new PublishNamespaceDone({ trackNamespace: request.path, requestId: request.requestId });
|
|
545
|
+
await done.encode(request.stream.writer, version);
|
|
546
|
+
}
|
|
547
|
+
catch {
|
|
548
|
+
// Stream might already be closed
|
|
549
|
+
}
|
|
333
550
|
}
|
|
334
551
|
request.stream.close();
|
|
335
552
|
}
|