@dxos/messaging 0.1.58-main.75cbd70 → 0.1.58-main.7858df5

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.
@@ -54,6 +54,9 @@ var MESSAGE_TIMEOUT = 1e4;
54
54
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/messenger.ts";
55
55
  var ReliablePayload = import_protocols.schema.getCodecForType("dxos.mesh.messaging.ReliablePayload");
56
56
  var Acknowledgement = import_protocols.schema.getCodecForType("dxos.mesh.messaging.Acknowledgement");
57
+ var ERROR_LIMIT = 5;
58
+ var NETWORK_REBOOT_DELAY = 3e3;
59
+ var RECEIVED_MESSAGES_GC_INTERVAL = 12e4;
57
60
  var Messenger = class {
58
61
  constructor({ signalManager, retryDelay = 300 }) {
59
62
  // { peerId, payloadType } => listeners set
@@ -61,8 +64,14 @@ var Messenger = class {
61
64
  // peerId => listeners set
62
65
  this._defaultListeners = new import_util.ComplexMap(import_keys.PublicKey.hash);
63
66
  this._onAckCallbacks = new import_util.ComplexMap(import_keys.PublicKey.hash);
64
- this._receivedMessages = new import_util.ComplexSet((key) => key.toHex());
67
+ this._receivedMessages = new import_util.ComplexSet(import_keys.PublicKey.hash);
68
+ /**
69
+ * Keys scheduled to be cleared from _receivedMessages on the next iteration.
70
+ */
71
+ this._toClear = new import_util.ComplexSet(import_keys.PublicKey.hash);
65
72
  this._closed = true;
73
+ this._errorCount = 0;
74
+ this._rebooting = false;
66
75
  this._signalManager = signalManager;
67
76
  this._retryDelay = retryDelay;
68
77
  this.open();
@@ -76,14 +85,14 @@ var Messenger = class {
76
85
  id: traceId
77
86
  }), {
78
87
  F: __dxlog_file,
79
- L: 62,
88
+ L: 74,
80
89
  S: this,
81
90
  C: (f, a) => f(...a)
82
91
  });
83
92
  this._ctx = new import_context.Context({
84
93
  onError: (err) => import_log.log.catch(err, void 0, {
85
94
  F: __dxlog_file,
86
- L: 64,
95
+ L: 76,
87
96
  S: this,
88
97
  C: (f, a) => f(...a)
89
98
  })
@@ -93,18 +102,21 @@ var Messenger = class {
93
102
  from: message.author
94
103
  }, {
95
104
  F: __dxlog_file,
96
- L: 68,
105
+ L: 80,
97
106
  S: this,
98
107
  C: (f, a) => f(...a)
99
108
  });
100
109
  await this._handleMessage(message);
101
110
  }));
111
+ (0, import_async.scheduleTaskInterval)(this._ctx, async () => {
112
+ this._performGc();
113
+ }, RECEIVED_MESSAGES_GC_INTERVAL);
102
114
  this._closed = false;
103
115
  import_log.log.trace("dxos.mesh.messenger.open", import_protocols.trace.end({
104
116
  id: traceId
105
117
  }), {
106
118
  F: __dxlog_file,
107
- L: 73,
119
+ L: 95,
108
120
  S: this,
109
121
  C: (f, a) => f(...a)
110
122
  });
@@ -119,7 +131,7 @@ var Messenger = class {
119
131
  async sendMessage({ author, recipient, payload }) {
120
132
  (0, import_invariant.invariant)(!this._closed, "Closed", {
121
133
  F: __dxlog_file,
122
- L: 85,
134
+ L: 107,
123
135
  S: this,
124
136
  A: [
125
137
  "!this._closed",
@@ -133,7 +145,7 @@ var Messenger = class {
133
145
  };
134
146
  (0, import_invariant.invariant)(!this._onAckCallbacks.has(reliablePayload.messageId), void 0, {
135
147
  F: __dxlog_file,
136
- L: 92,
148
+ L: 114,
137
149
  S: this,
138
150
  A: [
139
151
  "!this._onAckCallbacks.has(reliablePayload.messageId!)",
@@ -146,7 +158,7 @@ var Messenger = class {
146
158
  recipient
147
159
  }, {
148
160
  F: __dxlog_file,
149
- L: 93,
161
+ L: 115,
150
162
  S: this,
151
163
  C: (f, a) => f(...a)
152
164
  });
@@ -161,7 +173,7 @@ var Messenger = class {
161
173
  messageId: reliablePayload.messageId
162
174
  }, {
163
175
  F: __dxlog_file,
164
- L: 107,
176
+ L: 129,
165
177
  S: this,
166
178
  C: (f, a) => f(...a)
167
179
  });
@@ -173,22 +185,23 @@ var Messenger = class {
173
185
  err
174
186
  }, {
175
187
  F: __dxlog_file,
176
- L: 109,
188
+ L: 131,
177
189
  S: this,
178
190
  C: (f, a) => f(...a)
179
191
  }));
180
192
  }, this._retryDelay);
181
- (0, import_async.scheduleTask)(messageContext, () => {
193
+ (0, import_async.scheduleTask)(messageContext, async () => {
182
194
  (0, import_log.log)("message not delivered", {
183
195
  messageId: reliablePayload.messageId
184
196
  }, {
185
197
  F: __dxlog_file,
186
- L: 118,
198
+ L: 140,
187
199
  S: this,
188
200
  C: (f, a) => f(...a)
189
201
  });
190
202
  this._onAckCallbacks.delete(reliablePayload.messageId);
191
- timeoutHit(new import_async.TimeoutError(MESSAGE_TIMEOUT, "Message not delivered"));
203
+ await this.errorLimitCheck();
204
+ timeoutHit(new import_protocols.TimeoutError("signaling message not delivered", new import_async.TimeoutError(MESSAGE_TIMEOUT, "Message not delivered")));
192
205
  void messageContext.dispose();
193
206
  }, MESSAGE_TIMEOUT);
194
207
  this._onAckCallbacks.set(reliablePayload.messageId, () => {
@@ -203,6 +216,54 @@ var Messenger = class {
203
216
  });
204
217
  return promise;
205
218
  }
219
+ async errorLimitCheck() {
220
+ (0, import_log.log)(`checking error limit ${this._errorCount}`, void 0, {
221
+ F: __dxlog_file,
222
+ L: 165,
223
+ S: this,
224
+ C: (f, a) => f(...a)
225
+ });
226
+ if (this._errorCount++ > ERROR_LIMIT) {
227
+ await this.rebootNetwork();
228
+ }
229
+ }
230
+ async rebootNetwork() {
231
+ if (this._rebooting) {
232
+ return;
233
+ }
234
+ this._rebooting = true;
235
+ (0, import_log.log)("rebooting Messenger/SignalManager", void 0, {
236
+ F: __dxlog_file,
237
+ L: 177,
238
+ S: this,
239
+ C: (f, a) => f(...a)
240
+ });
241
+ await this.close();
242
+ await this._signalManager.close();
243
+ (0, import_log.log)("pausing", void 0, {
244
+ F: __dxlog_file,
245
+ L: 180,
246
+ S: this,
247
+ C: (f, a) => f(...a)
248
+ });
249
+ await new Promise((resolve) => setTimeout(resolve, NETWORK_REBOOT_DELAY));
250
+ (0, import_log.log)("done pausing", void 0, {
251
+ F: __dxlog_file,
252
+ L: 182,
253
+ S: this,
254
+ C: (f, a) => f(...a)
255
+ });
256
+ await this.open();
257
+ await this._signalManager.open();
258
+ (0, import_log.log)("done rebooting", void 0, {
259
+ F: __dxlog_file,
260
+ L: 185,
261
+ S: this,
262
+ C: (f, a) => f(...a)
263
+ });
264
+ this._errorCount = 0;
265
+ this._rebooting = false;
266
+ }
206
267
  /**
207
268
  * Subscribes onMessage function to messages that contains payload with payloadType.
208
269
  * @param payloadType if not specified, onMessage will be subscribed to all types of messages.
@@ -210,7 +271,7 @@ var Messenger = class {
210
271
  async listen({ peerId, payloadType, onMessage }) {
211
272
  (0, import_invariant.invariant)(!this._closed, "Closed", {
212
273
  F: __dxlog_file,
213
- L: 149,
274
+ L: 204,
214
275
  S: this,
215
276
  A: [
216
277
  "!this._closed",
@@ -274,7 +335,7 @@ var Messenger = class {
274
335
  async _handleReliablePayload({ author, recipient, payload }) {
275
336
  (0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.ReliablePayload", void 0, {
276
337
  F: __dxlog_file,
277
- L: 210,
338
+ L: 265,
278
339
  S: this,
279
340
  A: [
280
341
  "payload.type_url === 'dxos.mesh.messaging.ReliablePayload'",
@@ -288,7 +349,7 @@ var Messenger = class {
288
349
  messageId: reliablePayload.messageId
289
350
  }, {
290
351
  F: __dxlog_file,
291
- L: 213,
352
+ L: 268,
292
353
  S: this,
293
354
  C: (f, a) => f(...a)
294
355
  });
@@ -310,7 +371,7 @@ var Messenger = class {
310
371
  async _handleAcknowledgement({ payload }) {
311
372
  (0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.Acknowledgement", void 0, {
312
373
  F: __dxlog_file,
313
- L: 235,
374
+ L: 290,
314
375
  S: this,
315
376
  A: [
316
377
  "payload.type_url === 'dxos.mesh.messaging.Acknowledgement'",
@@ -326,7 +387,7 @@ var Messenger = class {
326
387
  to: author
327
388
  }, {
328
389
  F: __dxlog_file,
329
- L: 248,
390
+ L: 303,
330
391
  S: this,
331
392
  C: (f, a) => f(...a)
332
393
  });
@@ -362,6 +423,27 @@ var Messenger = class {
362
423
  }
363
424
  }
364
425
  }
426
+ _performGc() {
427
+ const start = performance.now();
428
+ for (const key of this._toClear.keys()) {
429
+ this._receivedMessages.delete(key);
430
+ }
431
+ this._toClear.clear();
432
+ for (const key of this._receivedMessages.keys()) {
433
+ this._toClear.add(key);
434
+ }
435
+ const elapsed = performance.now() - start;
436
+ if (elapsed > 100) {
437
+ import_log.log.warn("GC took too long", {
438
+ elapsed
439
+ }, {
440
+ F: __dxlog_file,
441
+ L: 351,
442
+ S: this,
443
+ C: (f, a) => f(...a)
444
+ });
445
+ }
446
+ }
365
447
  };
366
448
 
367
449
  // packages/core/mesh/messaging/src/signal-client/signal-client.ts
@@ -1096,7 +1178,6 @@ var MemorySignalManagerContext = class {
1096
1178
  }
1097
1179
  };
1098
1180
  var MemorySignalManager = class {
1099
- // prettier-ignore
1100
1181
  constructor(_context) {
1101
1182
  this._context = _context;
1102
1183
  this.statusChanged = new import_async4.Event();
@@ -1137,7 +1218,7 @@ var MemorySignalManager = class {
1137
1218
  async join({ topic, peerId }) {
1138
1219
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1139
1220
  F: __dxlog_file4,
1140
- L: 104,
1221
+ L: 101,
1141
1222
  S: this,
1142
1223
  A: [
1143
1224
  "!this._ctx.disposed",
@@ -1178,7 +1259,7 @@ var MemorySignalManager = class {
1178
1259
  async leave({ topic, peerId }) {
1179
1260
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1180
1261
  F: __dxlog_file4,
1181
- L: 140,
1262
+ L: 137,
1182
1263
  S: this,
1183
1264
  A: [
1184
1265
  "!this._ctx.disposed",
@@ -1210,13 +1291,13 @@ var MemorySignalManager = class {
1210
1291
  ...dec(payload)
1211
1292
  }, {
1212
1293
  F: __dxlog_file4,
1213
- L: 160,
1294
+ L: 157,
1214
1295
  S: this,
1215
1296
  C: (f, a) => f(...a)
1216
1297
  });
1217
1298
  (0, import_invariant4.invariant)(recipient, void 0, {
1218
1299
  F: __dxlog_file4,
1219
- L: 162,
1300
+ L: 159,
1220
1301
  S: this,
1221
1302
  A: [
1222
1303
  "recipient",
@@ -1225,7 +1306,7 @@ var MemorySignalManager = class {
1225
1306
  });
1226
1307
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1227
1308
  F: __dxlog_file4,
1228
- L: 163,
1309
+ L: 160,
1229
1310
  S: this,
1230
1311
  A: [
1231
1312
  "!this._ctx.disposed",
@@ -1240,7 +1321,7 @@ var MemorySignalManager = class {
1240
1321
  recipient
1241
1322
  }, {
1242
1323
  F: __dxlog_file4,
1243
- L: 169,
1324
+ L: 166,
1244
1325
  S: this,
1245
1326
  C: (f, a) => f(...a)
1246
1327
  });
@@ -1252,7 +1333,7 @@ var MemorySignalManager = class {
1252
1333
  recipient
1253
1334
  }, {
1254
1335
  F: __dxlog_file4,
1255
- L: 174,
1336
+ L: 171,
1256
1337
  S: this,
1257
1338
  C: (f, a) => f(...a)
1258
1339
  });
@@ -1265,7 +1346,7 @@ var MemorySignalManager = class {
1265
1346
  recipient
1266
1347
  }, {
1267
1348
  F: __dxlog_file4,
1268
- L: 182,
1349
+ L: 179,
1269
1350
  S: this,
1270
1351
  C: (f, a) => f(...a)
1271
1352
  });
@@ -1277,7 +1358,7 @@ var MemorySignalManager = class {
1277
1358
  ...dec(payload)
1278
1359
  }, {
1279
1360
  F: __dxlog_file4,
1280
- L: 186,
1361
+ L: 183,
1281
1362
  S: this,
1282
1363
  C: (f, a) => f(...a)
1283
1364
  });
@@ -1291,7 +1372,7 @@ var MemorySignalManager = class {
1291
1372
  err
1292
1373
  }, {
1293
1374
  F: __dxlog_file4,
1294
- L: 191,
1375
+ L: 188,
1295
1376
  S: this,
1296
1377
  C: (f, a) => f(...a)
1297
1378
  });
@@ -1302,7 +1383,7 @@ var MemorySignalManager = class {
1302
1383
  peerId
1303
1384
  }, {
1304
1385
  F: __dxlog_file4,
1305
- L: 196,
1386
+ L: 193,
1306
1387
  S: this,
1307
1388
  C: (f, a) => f(...a)
1308
1389
  });
@@ -1313,7 +1394,7 @@ var MemorySignalManager = class {
1313
1394
  peerId
1314
1395
  }, {
1315
1396
  F: __dxlog_file4,
1316
- L: 201,
1397
+ L: 198,
1317
1398
  S: this,
1318
1399
  C: (f, a) => f(...a)
1319
1400
  });
@@ -1359,7 +1440,6 @@ function _ts_decorate(decorators, target, key, desc) {
1359
1440
  }
1360
1441
  var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/websocket-signal-manager.ts";
1361
1442
  var WebsocketSignalManager = class {
1362
- // prettier-ignore
1363
1443
  constructor(_hosts) {
1364
1444
  this._hosts = _hosts;
1365
1445
  this._servers = /* @__PURE__ */ new Map();
@@ -1373,7 +1453,7 @@ var WebsocketSignalManager = class {
1373
1453
  hosts: this._hosts
1374
1454
  }, {
1375
1455
  F: __dxlog_file5,
1376
- L: 46,
1456
+ L: 43,
1377
1457
  S: this,
1378
1458
  C: (f, a) => f(...a)
1379
1459
  });
@@ -1395,7 +1475,7 @@ var WebsocketSignalManager = class {
1395
1475
  hosts: this._hosts
1396
1476
  }, {
1397
1477
  F: __dxlog_file5,
1398
- L: 68,
1478
+ L: 65,
1399
1479
  S: this,
1400
1480
  C: (f, a) => f(...a)
1401
1481
  });
@@ -1403,7 +1483,7 @@ var WebsocketSignalManager = class {
1403
1483
  id: this._instanceId
1404
1484
  }), {
1405
1485
  F: __dxlog_file5,
1406
- L: 69,
1486
+ L: 66,
1407
1487
  S: this,
1408
1488
  C: (f, a) => f(...a)
1409
1489
  });
@@ -1416,7 +1496,7 @@ var WebsocketSignalManager = class {
1416
1496
  id: this._instanceId
1417
1497
  }), {
1418
1498
  F: __dxlog_file5,
1419
- L: 76,
1499
+ L: 73,
1420
1500
  S: this,
1421
1501
  C: (f, a) => f(...a)
1422
1502
  });
@@ -1438,13 +1518,13 @@ var WebsocketSignalManager = class {
1438
1518
  peerId
1439
1519
  }, {
1440
1520
  F: __dxlog_file5,
1441
- L: 96,
1521
+ L: 93,
1442
1522
  S: this,
1443
1523
  C: (f, a) => f(...a)
1444
1524
  });
1445
1525
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1446
1526
  F: __dxlog_file5,
1447
- L: 97,
1527
+ L: 94,
1448
1528
  S: this,
1449
1529
  A: [
1450
1530
  "this._opened",
@@ -1462,13 +1542,13 @@ var WebsocketSignalManager = class {
1462
1542
  peerId
1463
1543
  }, {
1464
1544
  F: __dxlog_file5,
1465
- L: 103,
1545
+ L: 100,
1466
1546
  S: this,
1467
1547
  C: (f, a) => f(...a)
1468
1548
  });
1469
1549
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1470
1550
  F: __dxlog_file5,
1471
- L: 104,
1551
+ L: 101,
1472
1552
  S: this,
1473
1553
  A: [
1474
1554
  "this._opened",
@@ -1483,13 +1563,13 @@ var WebsocketSignalManager = class {
1483
1563
  async sendMessage({ author, recipient, payload }) {
1484
1564
  (0, import_log5.log)(`Signal ${recipient}`, void 0, {
1485
1565
  F: __dxlog_file5,
1486
- L: 118,
1566
+ L: 115,
1487
1567
  S: this,
1488
1568
  C: (f, a) => f(...a)
1489
1569
  });
1490
1570
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1491
1571
  F: __dxlog_file5,
1492
- L: 119,
1572
+ L: 116,
1493
1573
  S: this,
1494
1574
  A: [
1495
1575
  "this._opened",
@@ -1503,7 +1583,7 @@ var WebsocketSignalManager = class {
1503
1583
  payload
1504
1584
  }).catch((err) => (0, import_log5.log)(err, void 0, {
1505
1585
  F: __dxlog_file5,
1506
- L: 122,
1586
+ L: 119,
1507
1587
  S: this,
1508
1588
  C: (f, a) => f(...a)
1509
1589
  }));
@@ -1512,13 +1592,13 @@ var WebsocketSignalManager = class {
1512
1592
  async subscribeMessages(peerId) {
1513
1593
  (0, import_log5.log)(`Subscribed for message stream peerId=${peerId}`, void 0, {
1514
1594
  F: __dxlog_file5,
1515
- L: 127,
1595
+ L: 124,
1516
1596
  S: this,
1517
1597
  C: (f, a) => f(...a)
1518
1598
  });
1519
1599
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1520
1600
  F: __dxlog_file5,
1521
- L: 128,
1601
+ L: 125,
1522
1602
  S: this,
1523
1603
  A: [
1524
1604
  "this._opened",
@@ -1530,13 +1610,13 @@ var WebsocketSignalManager = class {
1530
1610
  async unsubscribeMessages(peerId) {
1531
1611
  (0, import_log5.log)(`Subscribed for message stream peerId=${peerId}`, void 0, {
1532
1612
  F: __dxlog_file5,
1533
- L: 134,
1613
+ L: 131,
1534
1614
  S: this,
1535
1615
  C: (f, a) => f(...a)
1536
1616
  });
1537
1617
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1538
1618
  F: __dxlog_file5,
1539
- L: 135,
1619
+ L: 132,
1540
1620
  S: this,
1541
1621
  A: [
1542
1622
  "this._opened",
@@ -1549,7 +1629,7 @@ var WebsocketSignalManager = class {
1549
1629
  this._ctx = new import_context4.Context({
1550
1630
  onError: (err) => import_log5.log.catch(err, void 0, {
1551
1631
  F: __dxlog_file5,
1552
- L: 142,
1632
+ L: 139,
1553
1633
  S: this,
1554
1634
  C: (f, a) => f(...a)
1555
1635
  })