@resolveio/server-lib 20.14.14 → 20.14.16

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.
@@ -48,6 +48,8 @@ export declare class SubscriptionManager {
48
48
  private _latencyFlushPending;
49
49
  private readonly LATENCY_UPDATE_INTERVAL;
50
50
  private readonly LATENCY_UPDATE_THRESHOLD_MS;
51
+ private readonly LATENCY_DEBUG_THRESHOLD_MS;
52
+ private readonly PUBLICATION_DEBUG_THRESHOLD_MS;
51
53
  private _invalidationDebounceTimers;
52
54
  private _invalidationPendingTimestamps;
53
55
  private _pendingInvalidations;
@@ -159,6 +159,8 @@ var SubscriptionManager = /** @class */ (function () {
159
159
  this.LATENCY_UPDATE_INTERVAL = 60000;
160
160
  // Minimum time difference between two latency updates for the same user
161
161
  this.LATENCY_UPDATE_THRESHOLD_MS = 30000;
162
+ this.LATENCY_DEBUG_THRESHOLD_MS = 1000;
163
+ this.PUBLICATION_DEBUG_THRESHOLD_MS = 300;
162
164
  // private currentPerfomanceMonitor: CurrentPerformanceMonitor[] = [];
163
165
  // private idPerformance: number = 0;
164
166
  // private performanceThread;
@@ -667,20 +669,55 @@ var SubscriptionManager = /** @class */ (function () {
667
669
  };
668
670
  // Throttled `loggedInLatency` method
669
671
  SubscriptionManager.prototype.loggedInLatency = function (ws) {
670
- var loggedInUser = this._loggedInUsers.find(function (a) { return a.id_ws === ws['id_socket']; });
671
- if (!loggedInUser)
672
- return;
672
+ var _a, _b, _c, _d;
673
673
  var now = new Date();
674
- var existingEntry = this.latencyBuffer.get(ws['id_socket']);
675
674
  var newLatency = ws['latency'];
675
+ var socketId = ws['id_socket'];
676
+ var loggedInUser = this._loggedInUsers.find(function (a) { return a.id_ws === socketId; });
677
+ if (!loggedInUser) {
678
+ if (typeof newLatency === 'number' && newLatency >= this.LATENCY_DEBUG_THRESHOLD_MS) {
679
+ var lastRoute = this._lastRouteBySocket.get(socketId);
680
+ var workerDispatcher = (_b = (_a = resolveio_server_app_1.ResolveIOServer.getMainServer()) === null || _a === void 0 ? void 0 : _a.getWorkerDispatcherManager) === null || _b === void 0 ? void 0 : _b.call(_a);
681
+ var workerSnapshot = workerDispatcher && typeof workerDispatcher.getQueueSnapshot === 'function'
682
+ ? workerDispatcher.getQueueSnapshot()
683
+ : null;
684
+ console.log(new Date(), '[Latency Spike]', {
685
+ latencyMs: (0, common_1.round)(newLatency),
686
+ id_user: ws['id_user'] || null,
687
+ user: ws['user'] || null,
688
+ id_ws: socketId,
689
+ lastRoute: (lastRoute === null || lastRoute === void 0 ? void 0 : lastRoute.route) || null,
690
+ lastRouteAgeMs: lastRoute ? (Date.now() - lastRoute.dateMs) : null,
691
+ worker: workerSnapshot
692
+ });
693
+ }
694
+ return;
695
+ }
696
+ var existingEntry = this.latencyBuffer.get(socketId);
676
697
  // Throttle updates: only update if time threshold has passed or latency has significantly changed
677
698
  if (!existingEntry ||
678
699
  (now.getTime() - existingEntry.lastUpdate.getTime() >= this.LATENCY_UPDATE_THRESHOLD_MS) ||
679
700
  (Math.abs(newLatency - existingEntry.latency) > 100) // Optional: log only significant changes
680
701
  ) {
702
+ if (typeof newLatency === 'number' && newLatency >= this.LATENCY_DEBUG_THRESHOLD_MS) {
703
+ var lastRoute = this._lastRouteBySocket.get(socketId);
704
+ var workerDispatcher = (_d = (_c = resolveio_server_app_1.ResolveIOServer.getMainServer()) === null || _c === void 0 ? void 0 : _c.getWorkerDispatcherManager) === null || _d === void 0 ? void 0 : _d.call(_c);
705
+ var workerSnapshot = workerDispatcher && typeof workerDispatcher.getQueueSnapshot === 'function'
706
+ ? workerDispatcher.getQueueSnapshot()
707
+ : null;
708
+ console.log(new Date(), '[Latency Spike]', {
709
+ latencyMs: (0, common_1.round)(newLatency),
710
+ id_user: loggedInUser.id_user,
711
+ user: loggedInUser.user,
712
+ id_ws: socketId,
713
+ lastRoute: (lastRoute === null || lastRoute === void 0 ? void 0 : lastRoute.route) || null,
714
+ lastRouteAgeMs: lastRoute ? (Date.now() - lastRoute.dateMs) : null,
715
+ worker: workerSnapshot
716
+ });
717
+ }
681
718
  // Update in-memory and buffer
682
719
  loggedInUser.date = now;
683
- this.latencyBuffer.set(ws['id_socket'], { latency: newLatency, lastUpdate: now });
720
+ this.latencyBuffer.set(socketId, { latency: newLatency, lastUpdate: now });
684
721
  }
685
722
  };
686
723
  // Method to flush buffered latency updates in bulk
@@ -1703,12 +1740,13 @@ var SubscriptionManager = /** @class */ (function () {
1703
1740
  };
1704
1741
  SubscriptionManager.prototype.sendDataToOne = function (ws, messageId, sub, collection, type) {
1705
1742
  return __awaiter(this, void 0, void 0, function () {
1706
- var monitor, dependencySnapshot, res, execution, serverRes, err_1, _a, normalizedError, correlationId, serverRes, errorPayload;
1743
+ var startMs, monitor, dependencySnapshot, res, execution, serverRes, err_1, _a, normalizedError, correlationId, serverRes, errorPayload, durationMs;
1707
1744
  var _this = this;
1708
1745
  var _b;
1709
1746
  return __generator(this, function (_c) {
1710
1747
  switch (_c.label) {
1711
1748
  case 0:
1749
+ startMs = Date.now();
1712
1750
  monitor = this._monitorManagerFunction.startMonitorFunction('User Specific Publication', sub.publication, '', '', sub.subscriptionData);
1713
1751
  _c.label = 1;
1714
1752
  case 1:
@@ -1775,7 +1813,22 @@ var SubscriptionManager = /** @class */ (function () {
1775
1813
  case 4:
1776
1814
  _c.sent();
1777
1815
  return [3 /*break*/, 7];
1778
- case 5: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
1816
+ case 5:
1817
+ durationMs = Date.now() - startMs;
1818
+ if (durationMs >= this.PUBLICATION_DEBUG_THRESHOLD_MS) {
1819
+ console.log(new Date(), '[Slow Publication]', {
1820
+ durationMs: durationMs,
1821
+ publication: sub.publication,
1822
+ type: type,
1823
+ collection: collection,
1824
+ userSpecific: true,
1825
+ id_user: ws['id_user'] || null,
1826
+ id_ws: ws['id_socket'] || null,
1827
+ clients: sub.clients ? sub.clients.length : 0,
1828
+ subscriptionData: sub.subscriptionData
1829
+ });
1830
+ }
1831
+ return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
1779
1832
  case 6:
1780
1833
  _c.sent();
1781
1834
  return [7 /*endfinally*/];
@@ -1787,7 +1840,7 @@ var SubscriptionManager = /** @class */ (function () {
1787
1840
  // Fetch pub once, send to all clients linked to this pub
1788
1841
  SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
1789
1842
  return __awaiter(this, void 0, void 0, function () {
1790
- 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;
1843
+ var subIndex, startMs, 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, durationMs;
1791
1844
  var e_6, _h, e_7, _j, e_8, _k;
1792
1845
  var _this = this;
1793
1846
  var _l;
@@ -1805,6 +1858,7 @@ var SubscriptionManager = /** @class */ (function () {
1805
1858
  }
1806
1859
  return [2 /*return*/];
1807
1860
  case 1:
1861
+ startMs = Date.now();
1808
1862
  monitor = this._monitorManagerFunction.startMonitorFunction('Publication', sub.publication, '', '', sub.subscriptionData);
1809
1863
  dependencySnapshot = void 0;
1810
1864
  _m.label = 2;
@@ -1999,7 +2053,21 @@ var SubscriptionManager = /** @class */ (function () {
1999
2053
  case 5:
2000
2054
  _m.sent();
2001
2055
  return [3 /*break*/, 8];
2002
- case 6: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2056
+ case 6:
2057
+ durationMs = Date.now() - startMs;
2058
+ if (durationMs >= this.PUBLICATION_DEBUG_THRESHOLD_MS) {
2059
+ console.log(new Date(), '[Slow Publication]', {
2060
+ durationMs: durationMs,
2061
+ publication: sub.publication,
2062
+ type: type,
2063
+ collection: collection,
2064
+ userSpecific: false,
2065
+ clients: sub.clients ? sub.clients.length : 0,
2066
+ cacheId: sub.cacheId || 0,
2067
+ subscriptionData: sub.subscriptionData
2068
+ });
2069
+ }
2070
+ return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2003
2071
  case 7:
2004
2072
  _m.sent();
2005
2073
  return [7 /*endfinally*/];