@resolveio/server-lib 20.14.8 → 20.14.9

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.
@@ -44,6 +44,8 @@ export declare class SubscriptionManager {
44
44
  private readonly RESUME_TOKEN_COLLECTION;
45
45
  private readonly RESUME_TOKEN_SAVE_INTERVAL_MS;
46
46
  private latencyBuffer;
47
+ private _latencyFlushInProgress;
48
+ private _latencyFlushPending;
47
49
  private readonly LATENCY_UPDATE_INTERVAL;
48
50
  private readonly LATENCY_UPDATE_THRESHOLD_MS;
49
51
  private _invalidationDebounceTimers;
@@ -153,6 +153,8 @@ var SubscriptionManager = /** @class */ (function () {
153
153
  this.RESUME_TOKEN_SAVE_INTERVAL_MS = 5000;
154
154
  // Buffer to store throttled latency updates with timestamps
155
155
  this.latencyBuffer = new Map();
156
+ this._latencyFlushInProgress = false;
157
+ this._latencyFlushPending = false;
156
158
  // Interval to flush latency updates in MongoDB
157
159
  this.LATENCY_UPDATE_INTERVAL = 60000;
158
160
  // Minimum time difference between two latency updates for the same user
@@ -684,13 +686,22 @@ var SubscriptionManager = /** @class */ (function () {
684
686
  // Method to flush buffered latency updates in bulk
685
687
  SubscriptionManager.prototype.flushThrottledLatencyUpdates = function () {
686
688
  return __awaiter(this, void 0, void 0, function () {
687
- var updates, error_1;
688
- return __generator(this, function (_a) {
689
- switch (_a.label) {
689
+ var pendingEntries, updates, error_1, pendingEntries_1, pendingEntries_1_1, _a, id_ws, payload, current;
690
+ var e_3, _b;
691
+ var _this = this;
692
+ return __generator(this, function (_c) {
693
+ switch (_c.label) {
690
694
  case 0:
691
695
  if (this.latencyBuffer.size === 0)
692
696
  return [2 /*return*/]; // No updates to flush
693
- updates = Array.from(this.latencyBuffer.entries()).map(function (_a) {
697
+ if (this._latencyFlushInProgress) {
698
+ this._latencyFlushPending = true;
699
+ return [2 /*return*/];
700
+ }
701
+ this._latencyFlushInProgress = true;
702
+ pendingEntries = Array.from(this.latencyBuffer.entries());
703
+ this.latencyBuffer.clear();
704
+ updates = pendingEntries.map(function (_a) {
694
705
  var _b = __read(_a, 2), id_ws = _b[0], _c = _b[1], latency = _c.latency, lastUpdate = _c.lastUpdate;
695
706
  return ({
696
707
  updateOne: {
@@ -699,22 +710,44 @@ var SubscriptionManager = /** @class */ (function () {
699
710
  }
700
711
  });
701
712
  });
702
- _a.label = 1;
713
+ _c.label = 1;
703
714
  case 1:
704
- _a.trys.push([1, 3, , 4]);
705
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.bulkWrite(updates)];
715
+ _c.trys.push([1, 3, 4, 5]);
716
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.bulkWrite(updates, { ordered: false })];
706
717
  case 2:
707
- _a.sent();
708
- this.latencyBuffer.clear(); // Clear buffer after successful update
718
+ _c.sent();
709
719
  if (this.getEnableDebug()) {
710
720
  console.log(new Date(), 'Sub Manager', 'Throttled latency batch update successful', updates.length);
711
721
  }
712
- return [3 /*break*/, 4];
722
+ return [3 /*break*/, 5];
713
723
  case 3:
714
- error_1 = _a.sent();
724
+ error_1 = _c.sent();
725
+ try {
726
+ for (pendingEntries_1 = __values(pendingEntries), pendingEntries_1_1 = pendingEntries_1.next(); !pendingEntries_1_1.done; pendingEntries_1_1 = pendingEntries_1.next()) {
727
+ _a = __read(pendingEntries_1_1.value, 2), id_ws = _a[0], payload = _a[1];
728
+ current = this.latencyBuffer.get(id_ws);
729
+ if (!current || current.lastUpdate < payload.lastUpdate) {
730
+ this.latencyBuffer.set(id_ws, payload);
731
+ }
732
+ }
733
+ }
734
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
735
+ finally {
736
+ try {
737
+ if (pendingEntries_1_1 && !pendingEntries_1_1.done && (_b = pendingEntries_1.return)) _b.call(pendingEntries_1);
738
+ }
739
+ finally { if (e_3) throw e_3.error; }
740
+ }
715
741
  console.error(new Date(), 'Sub Manager', 'Throttled latency batch update failed', error_1);
716
- return [3 /*break*/, 4];
717
- case 4: return [2 /*return*/];
742
+ return [3 /*break*/, 5];
743
+ case 4:
744
+ this._latencyFlushInProgress = false;
745
+ if (this._latencyFlushPending) {
746
+ this._latencyFlushPending = false;
747
+ setImmediate(function () { return _this.flushThrottledLatencyUpdates(); });
748
+ }
749
+ return [7 /*endfinally*/];
750
+ case 5: return [2 /*return*/];
718
751
  }
719
752
  });
720
753
  });
@@ -1222,8 +1255,8 @@ var SubscriptionManager = /** @class */ (function () {
1222
1255
  };
1223
1256
  SubscriptionManager.prototype.fullResyncSubscriptions = function (reason) {
1224
1257
  return __awaiter(this, void 0, void 0, function () {
1225
- var subs, subs_1, subs_1_1, sub, pub, _a, _b, client, ws, _c, e_3_1, e_4_1, error_5;
1226
- var e_4, _d, e_3, _e;
1258
+ var subs, subs_1, subs_1_1, sub, pub, _a, _b, client, ws, _c, e_4_1, e_5_1, error_5;
1259
+ var e_5, _d, e_4, _e;
1227
1260
  var _f, _g, _h, _j, _k;
1228
1261
  return __generator(this, function (_l) {
1229
1262
  switch (_l.label) {
@@ -1268,7 +1301,7 @@ var SubscriptionManager = /** @class */ (function () {
1268
1301
  _l.label = 3;
1269
1302
  case 3:
1270
1303
  _l.trys.push([3, 10, 11, 12]);
1271
- _a = (e_3 = void 0, __values(sub.clients)), _b = _a.next();
1304
+ _a = (e_4 = void 0, __values(sub.clients)), _b = _a.next();
1272
1305
  _l.label = 4;
1273
1306
  case 4:
1274
1307
  if (!!_b.done) return [3 /*break*/, 9];
@@ -1290,14 +1323,14 @@ var SubscriptionManager = /** @class */ (function () {
1290
1323
  return [3 /*break*/, 4];
1291
1324
  case 9: return [3 /*break*/, 12];
1292
1325
  case 10:
1293
- e_3_1 = _l.sent();
1294
- e_3 = { error: e_3_1 };
1326
+ e_4_1 = _l.sent();
1327
+ e_4 = { error: e_4_1 };
1295
1328
  return [3 /*break*/, 12];
1296
1329
  case 11:
1297
1330
  try {
1298
1331
  if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1299
1332
  }
1300
- finally { if (e_3) throw e_3.error; }
1333
+ finally { if (e_4) throw e_4.error; }
1301
1334
  return [7 /*endfinally*/];
1302
1335
  case 12: return [3 /*break*/, 15];
1303
1336
  case 13: return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', reason)];
@@ -1309,14 +1342,14 @@ var SubscriptionManager = /** @class */ (function () {
1309
1342
  return [3 /*break*/, 2];
1310
1343
  case 16: return [3 /*break*/, 19];
1311
1344
  case 17:
1312
- e_4_1 = _l.sent();
1313
- e_4 = { error: e_4_1 };
1345
+ e_5_1 = _l.sent();
1346
+ e_5 = { error: e_5_1 };
1314
1347
  return [3 /*break*/, 19];
1315
1348
  case 18:
1316
1349
  try {
1317
1350
  if (subs_1_1 && !subs_1_1.done && (_d = subs_1.return)) _d.call(subs_1);
1318
1351
  }
1319
- finally { if (e_4) throw e_4.error; }
1352
+ finally { if (e_5) throw e_5.error; }
1320
1353
  return [7 /*endfinally*/];
1321
1354
  case 19: return [3 /*break*/, 21];
1322
1355
  case 20:
@@ -1724,7 +1757,7 @@ var SubscriptionManager = /** @class */ (function () {
1724
1757
  SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
1725
1758
  return __awaiter(this, void 0, void 0, function () {
1726
1759
  var subIndex, monitor, res_1, dependencySnapshot, execution, cacheData, _a, _b, client, ws, serverRes, _c, _d, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _e, normalizedError, correlationId, _f, _g, client, ws, serverRes, errorPayload;
1727
- var e_5, _h, e_6, _j, e_7, _k;
1760
+ var e_6, _h, e_7, _j, e_8, _k;
1728
1761
  var _this = this;
1729
1762
  var _l;
1730
1763
  return __generator(this, function (_m) {
@@ -1779,12 +1812,12 @@ var SubscriptionManager = /** @class */ (function () {
1779
1812
  }
1780
1813
  }
1781
1814
  }
1782
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
1815
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
1783
1816
  finally {
1784
1817
  try {
1785
1818
  if (_b && !_b.done && (_h = _a.return)) _h.call(_a);
1786
1819
  }
1787
- finally { if (e_5) throw e_5.error; }
1820
+ finally { if (e_6) throw e_6.error; }
1788
1821
  }
1789
1822
  this._nodeCache.del(sub.cacheId);
1790
1823
  if ((0, common_1.getBinarySize)(JSON.stringify(res_1)) < 1000000 &&
@@ -1837,12 +1870,12 @@ var SubscriptionManager = /** @class */ (function () {
1837
1870
  }
1838
1871
  }
1839
1872
  }
1840
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1873
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
1841
1874
  finally {
1842
1875
  try {
1843
1876
  if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
1844
1877
  }
1845
- finally { if (e_6) throw e_6.error; }
1878
+ finally { if (e_7) throw e_7.error; }
1846
1879
  }
1847
1880
  if ((0, common_1.getBinarySize)(JSON.stringify(res_1)) < 1000000 &&
1848
1881
  !sub.collections.includes('logs') &&
@@ -1896,12 +1929,12 @@ var SubscriptionManager = /** @class */ (function () {
1896
1929
  }
1897
1930
  }
1898
1931
  }
1899
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
1932
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
1900
1933
  finally {
1901
1934
  try {
1902
1935
  if (_g && !_g.done && (_k = _f.return)) _k.call(_f);
1903
1936
  }
1904
- finally { if (e_7) throw e_7.error; }
1937
+ finally { if (e_8) throw e_8.error; }
1905
1938
  }
1906
1939
  errorPayload = {
1907
1940
  publication: sub.publication,
@@ -2062,7 +2095,7 @@ var SubscriptionManager = /** @class */ (function () {
2062
2095
  '$bitsAnyClear'
2063
2096
  ]);
2064
2097
  var walk = function (value) {
2065
- var e_8, _a;
2098
+ var e_9, _a;
2066
2099
  if (Array.isArray(value)) {
2067
2100
  return value.every(function (item) { return walk(item); });
2068
2101
  }
@@ -2085,19 +2118,19 @@ var SubscriptionManager = /** @class */ (function () {
2085
2118
  }
2086
2119
  }
2087
2120
  }
2088
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
2121
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
2089
2122
  finally {
2090
2123
  try {
2091
2124
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2092
2125
  }
2093
- finally { if (e_8) throw e_8.error; }
2126
+ finally { if (e_9) throw e_9.error; }
2094
2127
  }
2095
2128
  return true;
2096
2129
  };
2097
2130
  return walk(filter);
2098
2131
  };
2099
2132
  SubscriptionManager.prototype.documentIdMatchesEvent = function (documentId, document) {
2100
- var e_9, _a;
2133
+ var e_10, _a;
2101
2134
  var _this = this;
2102
2135
  if (!document || documentId === undefined || documentId === null) {
2103
2136
  return true;
@@ -2120,12 +2153,12 @@ var SubscriptionManager = /** @class */ (function () {
2120
2153
  }
2121
2154
  }
2122
2155
  }
2123
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
2156
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
2124
2157
  finally {
2125
2158
  try {
2126
2159
  if (eventCandidates_1_1 && !eventCandidates_1_1.done && (_a = eventCandidates_1.return)) _a.call(eventCandidates_1);
2127
2160
  }
2128
- finally { if (e_9) throw e_9.error; }
2161
+ finally { if (e_10) throw e_10.error; }
2129
2162
  }
2130
2163
  return false;
2131
2164
  };
@@ -2149,8 +2182,8 @@ var SubscriptionManager = /** @class */ (function () {
2149
2182
  };
2150
2183
  SubscriptionManager.prototype.documentMatchesFilter = function (collection, documentId, filter, document) {
2151
2184
  return __awaiter(this, void 0, void 0, function () {
2152
- var inMemoryMatch, db, filterCopy, idCandidates, idCandidates_1, idCandidates_1_1, idValue, combinedFilter, doc, e_10_1, _a;
2153
- var e_10, _b;
2185
+ var inMemoryMatch, db, filterCopy, idCandidates, idCandidates_1, idCandidates_1_1, idValue, combinedFilter, doc, e_11_1, _a;
2186
+ var e_11, _b;
2154
2187
  return __generator(this, function (_c) {
2155
2188
  switch (_c.label) {
2156
2189
  case 0:
@@ -2188,14 +2221,14 @@ var SubscriptionManager = /** @class */ (function () {
2188
2221
  return [3 /*break*/, 2];
2189
2222
  case 5: return [3 /*break*/, 8];
2190
2223
  case 6:
2191
- e_10_1 = _c.sent();
2192
- e_10 = { error: e_10_1 };
2224
+ e_11_1 = _c.sent();
2225
+ e_11 = { error: e_11_1 };
2193
2226
  return [3 /*break*/, 8];
2194
2227
  case 7:
2195
2228
  try {
2196
2229
  if (idCandidates_1_1 && !idCandidates_1_1.done && (_b = idCandidates_1.return)) _b.call(idCandidates_1);
2197
2230
  }
2198
- finally { if (e_10) throw e_10.error; }
2231
+ finally { if (e_11) throw e_11.error; }
2199
2232
  return [7 /*endfinally*/];
2200
2233
  case 8: return [2 /*return*/, false];
2201
2234
  case 9:
@@ -2208,8 +2241,8 @@ var SubscriptionManager = /** @class */ (function () {
2208
2241
  };
2209
2242
  SubscriptionManager.prototype.shouldInvalidateSubscription = function (sub, collection, type, documentId, document) {
2210
2243
  return __awaiter(this, void 0, void 0, function () {
2211
- var normalizedDocumentId, hasDependencyData, trackedIds, filters, filters_1, filters_1_1, filter, e_11_1;
2212
- var e_11, _a;
2244
+ var normalizedDocumentId, hasDependencyData, trackedIds, filters, filters_1, filters_1_1, filter, e_12_1;
2245
+ var e_12, _a;
2213
2246
  var _b, _c;
2214
2247
  return __generator(this, function (_d) {
2215
2248
  switch (_d.label) {
@@ -2266,14 +2299,14 @@ var SubscriptionManager = /** @class */ (function () {
2266
2299
  return [3 /*break*/, 2];
2267
2300
  case 5: return [3 /*break*/, 8];
2268
2301
  case 6:
2269
- e_11_1 = _d.sent();
2270
- e_11 = { error: e_11_1 };
2302
+ e_12_1 = _d.sent();
2303
+ e_12 = { error: e_12_1 };
2271
2304
  return [3 /*break*/, 8];
2272
2305
  case 7:
2273
2306
  try {
2274
2307
  if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
2275
2308
  }
2276
- finally { if (e_11) throw e_11.error; }
2309
+ finally { if (e_12) throw e_12.error; }
2277
2310
  return [7 /*endfinally*/];
2278
2311
  case 8:
2279
2312
  this.dependencyDebug('Skip invalidation after dependency checks', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
@@ -2284,8 +2317,8 @@ var SubscriptionManager = /** @class */ (function () {
2284
2317
  };
2285
2318
  SubscriptionManager.prototype.shouldInvalidateSubscriptionForEvents = function (sub, collection, events) {
2286
2319
  return __awaiter(this, void 0, void 0, function () {
2287
- var sawInsert, sawDelete, events_1, events_1_1, event_1, e_12_1, paginationMeta, paginationReasons;
2288
- var e_12, _a;
2320
+ var sawInsert, sawDelete, events_1, events_1_1, event_1, e_13_1, paginationMeta, paginationReasons;
2321
+ var e_13, _a;
2289
2322
  return __generator(this, function (_b) {
2290
2323
  switch (_b.label) {
2291
2324
  case 0:
@@ -2316,14 +2349,14 @@ var SubscriptionManager = /** @class */ (function () {
2316
2349
  return [3 /*break*/, 2];
2317
2350
  case 5: return [3 /*break*/, 8];
2318
2351
  case 6:
2319
- e_12_1 = _b.sent();
2320
- e_12 = { error: e_12_1 };
2352
+ e_13_1 = _b.sent();
2353
+ e_13 = { error: e_13_1 };
2321
2354
  return [3 /*break*/, 8];
2322
2355
  case 7:
2323
2356
  try {
2324
2357
  if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
2325
2358
  }
2326
- finally { if (e_12) throw e_12.error; }
2359
+ finally { if (e_13) throw e_13.error; }
2327
2360
  return [7 /*endfinally*/];
2328
2361
  case 8:
2329
2362
  paginationMeta = this.getPaginationMeta(sub, collection);