@dxos/echo-pipeline 0.7.4 → 0.7.5-main.499c70c

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.
@@ -310,7 +310,7 @@ var CollectionSynchronizer = class extends Resource2 {
310
310
  return;
311
311
  }
312
312
  for (const [collectionId, state] of this._perCollectionStates.entries()) {
313
- if (this._shouldSyncCollection(collectionId, peerId)) {
313
+ if (this._activeCollections.has(collectionId) && this._shouldSyncCollection(collectionId, peerId)) {
314
314
  state.interestedPeers.add(peerId);
315
315
  state.lastQueried.set(peerId, Date.now());
316
316
  this._queryCollectionState(collectionId, peerId);
@@ -1293,7 +1293,10 @@ var AutomergeHost = class extends Resource4 {
1293
1293
  if (toReplicate.length === 0) {
1294
1294
  return;
1295
1295
  }
1296
- log4.info("replication documents after collection sync", {
1296
+ log4.info("replicating documents after collection sync", {
1297
+ collectionId,
1298
+ peerId,
1299
+ toReplicate,
1297
1300
  count: toReplicate.length
1298
1301
  }, {
1299
1302
  F: __dxlog_file4,
@@ -1364,7 +1367,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1364
1367
  var decodeCollectionState = (state) => {
1365
1368
  invariant3(typeof state === "object" && state !== null, "Invalid state", {
1366
1369
  F: __dxlog_file4,
1367
- L: 557,
1370
+ L: 560,
1368
1371
  S: void 0,
1369
1372
  A: [
1370
1373
  "typeof state === 'object' && state !== null",
@@ -1656,10 +1659,10 @@ var MeshEchoReplicator = class {
1656
1659
  documentId: params.documentId,
1657
1660
  peerId: connection.peerId
1658
1661
  });
1659
- log6("document not found locally for share policy check, accepting the remote document", {
1662
+ log6("document not found locally for share policy check", {
1660
1663
  peerId: connection.peerId,
1661
1664
  documentId: params.documentId,
1662
- remoteDocumentExists
1665
+ acceptDocument: remoteDocumentExists
1663
1666
  }, {
1664
1667
  F: __dxlog_file7,
1665
1668
  L: 91,
@@ -3116,8 +3119,7 @@ var EchoHost = class extends Resource9 {
3116
3119
  };
3117
3120
 
3118
3121
  // packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts
3119
- import { Mutex, scheduleTask as scheduleTask2, scheduleMicroTask } from "@dxos/async";
3120
- import * as A5 from "@dxos/automerge/automerge";
3122
+ import { Mutex, scheduleTask as scheduleTask2, scheduleMicroTask, Trigger as Trigger2 } from "@dxos/async";
3121
3123
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
3122
3124
  import { Context as Context6, Resource as Resource10 } from "@dxos/context";
3123
3125
  import { randomUUID } from "@dxos/crypto";
@@ -3210,14 +3212,14 @@ var EchoEdgeReplicator = class {
3210
3212
  connectedSpaces: this._connectedSpaces.size
3211
3213
  }, {
3212
3214
  F: __dxlog_file16,
3213
- L: 60,
3215
+ L: 59,
3214
3216
  S: this,
3215
3217
  C: (f, a) => f(...a)
3216
3218
  });
3217
3219
  this._context = context;
3218
3220
  this._ctx = Context6.default(void 0, {
3219
3221
  F: __dxlog_file16,
3220
- L: 63
3222
+ L: 62
3221
3223
  });
3222
3224
  this._ctx.onDispose(this._edgeConnection.onReconnected(() => {
3223
3225
  this._ctx && scheduleMicroTask(this._ctx, () => this._handleReconnect());
@@ -3296,7 +3298,7 @@ var EchoEdgeReplicator = class {
3296
3298
  async _openConnection(spaceId, reconnects = 0) {
3297
3299
  invariant12(this._context, void 0, {
3298
3300
  F: __dxlog_file16,
3299
- L: 124,
3301
+ L: 123,
3300
3302
  S: this,
3301
3303
  A: [
3302
3304
  "this._context",
@@ -3305,7 +3307,7 @@ var EchoEdgeReplicator = class {
3305
3307
  });
3306
3308
  invariant12(!this._connections.has(spaceId), void 0, {
3307
3309
  F: __dxlog_file16,
3308
- L: 125,
3310
+ L: 124,
3309
3311
  S: this,
3310
3312
  A: [
3311
3313
  "!this._connections.has(spaceId)",
@@ -3335,7 +3337,7 @@ var EchoEdgeReplicator = class {
3335
3337
  restartDelay
3336
3338
  }, {
3337
3339
  F: __dxlog_file16,
3338
- L: 148,
3340
+ L: 147,
3339
3341
  S: this,
3340
3342
  C: (f, a) => f(...a)
3341
3343
  });
@@ -3366,10 +3368,20 @@ var EchoEdgeReplicator = class {
3366
3368
  await connection.open();
3367
3369
  }
3368
3370
  };
3371
+ var MAX_INFLIGHT_REQUESTS = 5;
3369
3372
  var EdgeReplicatorConnection = class extends Resource10 {
3370
3373
  constructor({ edgeConnection, spaceId, context, sharedPolicyEnabled, onRemoteConnected, onRemoteDisconnected, onRestartRequested }) {
3371
3374
  super();
3372
3375
  this._remotePeerId = null;
3376
+ /**
3377
+ * Prevents sending too many messages to edge over this connection so that we don't overwhelm
3378
+ * a replicator durable object.
3379
+ * inflightRequests counter is incremented on outgoing sync messages and decremented on incoming messages.
3380
+ * The trigger is waiting while the counter is above MAX_INFLIGHT_REQUESTS.
3381
+ * The counter can go negative because we receive edge-initiated sync messages on doc change broadcasts.
3382
+ */
3383
+ this._outgoingRequestsBarrier = new Trigger2();
3384
+ this._inflightRequests = 0;
3373
3385
  this._edgeConnection = edgeConnection;
3374
3386
  this._spaceId = spaceId;
3375
3387
  this._context = context;
@@ -3379,6 +3391,7 @@ var EdgeReplicatorConnection = class extends Resource10 {
3379
3391
  this._onRemoteConnected = onRemoteConnected;
3380
3392
  this._onRemoteDisconnected = onRemoteDisconnected;
3381
3393
  this._onRestartRequested = onRestartRequested;
3394
+ this._outgoingRequestsBarrier.wake();
3382
3395
  this.readable = new ReadableStream({
3383
3396
  start: (controller) => {
3384
3397
  this._readableStreamController = controller;
@@ -3386,6 +3399,11 @@ var EdgeReplicatorConnection = class extends Resource10 {
3386
3399
  });
3387
3400
  this.writable = new WritableStream({
3388
3401
  write: async (message, controller) => {
3402
+ await this._outgoingRequestsBarrier.wait();
3403
+ this._inflightRequests++;
3404
+ if (this._inflightRequests === MAX_INFLIGHT_REQUESTS) {
3405
+ this._outgoingRequestsBarrier.reset();
3406
+ }
3389
3407
  await this._sendMessage(message);
3390
3408
  }
3391
3409
  });
@@ -3393,7 +3411,7 @@ var EdgeReplicatorConnection = class extends Resource10 {
3393
3411
  async _open(ctx) {
3394
3412
  log11("open", void 0, {
3395
3413
  F: __dxlog_file16,
3396
- L: 242,
3414
+ L: 261,
3397
3415
  S: this,
3398
3416
  C: (f, a) => f(...a)
3399
3417
  });
@@ -3405,17 +3423,18 @@ var EdgeReplicatorConnection = class extends Resource10 {
3405
3423
  async _close() {
3406
3424
  log11("close", void 0, {
3407
3425
  F: __dxlog_file16,
3408
- L: 254,
3426
+ L: 273,
3409
3427
  S: this,
3410
3428
  C: (f, a) => f(...a)
3411
3429
  });
3412
3430
  this._readableStreamController.close();
3431
+ this._outgoingRequestsBarrier.throw(new Error("Connection closed."));
3413
3432
  await this._onRemoteDisconnected();
3414
3433
  }
3415
3434
  get peerId() {
3416
3435
  invariant12(this._remotePeerId, "Not connected", {
3417
3436
  F: __dxlog_file16,
3418
- L: 260,
3437
+ L: 282,
3419
3438
  S: this,
3420
3439
  A: [
3421
3440
  "this._remotePeerId",
@@ -3434,12 +3453,13 @@ var EdgeReplicatorConnection = class extends Resource10 {
3434
3453
  documentId: params.documentId,
3435
3454
  peerId: this._remotePeerId
3436
3455
  });
3437
- log11.info("document not found locally for share policy check, accepting the remote document", {
3456
+ log11.verbose("edge-replicator document not found locally for share policy check", {
3438
3457
  documentId: params.documentId,
3439
- remoteDocumentExists
3458
+ acceptDocument: remoteDocumentExists,
3459
+ remoteId: this._remotePeerId
3440
3460
  }, {
3441
3461
  F: __dxlog_file16,
3442
- L: 275,
3462
+ L: 297,
3443
3463
  S: this,
3444
3464
  C: (f, a) => f(...a)
3445
3465
  });
@@ -3452,23 +3472,20 @@ var EdgeReplicatorConnection = class extends Resource10 {
3452
3472
  return true;
3453
3473
  }
3454
3474
  const spaceId = getSpaceIdFromCollectionId(params.collectionId);
3455
- return spaceId === this._spaceId;
3475
+ return spaceId === this._spaceId && params.collectionId.split(":").length === 3;
3456
3476
  }
3457
3477
  _onMessage(message) {
3458
3478
  if (message.serviceId !== this._targetServiceId) {
3459
3479
  return;
3460
3480
  }
3461
3481
  const payload = cbor2.decode(message.payload.value);
3462
- log11("recv", () => {
3463
- const decodedData = payload.type === "sync" && payload.data ? A5.decodeSyncMessage(payload.data) : payload.type === "collection-state" ? payload.state : payload;
3464
- return {
3465
- from: message.serviceId,
3466
- type: payload.type,
3467
- decodedData
3468
- };
3482
+ log11.verbose("edge replicator receive", {
3483
+ type: payload.type,
3484
+ documentId: payload.type === "sync" && payload.documentId,
3485
+ remoteId: this._remotePeerId
3469
3486
  }, {
3470
3487
  F: __dxlog_file16,
3471
- L: 302,
3488
+ L: 326,
3472
3489
  S: this,
3473
3490
  C: (f, a) => f(...a)
3474
3491
  });
@@ -3480,18 +3497,23 @@ var EdgeReplicatorConnection = class extends Resource10 {
3480
3497
  this._onRestartRequested();
3481
3498
  return;
3482
3499
  }
3500
+ if (message.type === "sync") {
3501
+ this._inflightRequests--;
3502
+ if (this._inflightRequests === MAX_INFLIGHT_REQUESTS - 1) {
3503
+ this._outgoingRequestsBarrier.wake();
3504
+ }
3505
+ }
3483
3506
  this._readableStreamController.enqueue(message);
3484
3507
  }
3485
3508
  async _sendMessage(message) {
3486
3509
  message.targetId = this._targetServiceId;
3487
- log11("send", {
3510
+ log11.verbose("edge replicator send", {
3488
3511
  type: message.type,
3489
- senderId: message.senderId,
3490
- targetId: message.targetId,
3491
- documentId: message.documentId
3512
+ documentId: message.type === "sync" && message.documentId,
3513
+ remoteId: this._remotePeerId
3492
3514
  }, {
3493
3515
  F: __dxlog_file16,
3494
- L: 332,
3516
+ L: 360,
3495
3517
  S: this,
3496
3518
  C: (f, a) => f(...a)
3497
3519
  });