@resolveio/server-lib 20.14.36 → 20.14.38

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.
@@ -36,6 +36,10 @@ export declare class SubscriptionManager {
36
36
  private _debugMongoQueueCollections;
37
37
  private _debugSendQueueHits;
38
38
  private _debugRemoveCacheHits;
39
+ private _subSendDebug;
40
+ private _subSendLogThresholdMs;
41
+ private _subSendLogBytes;
42
+ private _subSendLogSampleRate;
39
43
  private _oplogRetryCount;
40
44
  private _lastResumeToken;
41
45
  private _lastResumeTokenSaveMs;
@@ -96,8 +100,15 @@ export declare class SubscriptionManager {
96
100
  private resolveLocalOplogResyncIntervalMs;
97
101
  private resolveResumeTokenMaxAgeMs;
98
102
  private resolveConnectDebug;
103
+ private resolveSubSendDebug;
104
+ private resolveSubSendLogThresholdMs;
105
+ private resolveSubSendLogBytes;
106
+ private resolveSubSendLogSampleRate;
99
107
  private parseDebugFlag;
100
108
  private parsePositiveNumber;
109
+ private parseSampleRate;
110
+ private shouldLogSubSend;
111
+ private logSubSend;
101
112
  private connectDebug;
102
113
  private parsePositiveFloat;
103
114
  private isChangeStreamUnsupported;
@@ -75,6 +75,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
75
75
  exports.SubscriptionManager = void 0;
76
76
  var NodeCache = require("node-cache");
77
77
  var msgpackr_1 = require("msgpackr");
78
+ var WebSocket = require("ws");
78
79
  var flag_collection_1 = require("../collections/flag.collection");
79
80
  var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
80
81
  var app_status_1 = require("../publications/app-status");
@@ -145,6 +146,10 @@ var SubscriptionManager = /** @class */ (function () {
145
146
  this._debugMongoQueueCollections = [];
146
147
  this._debugSendQueueHits = 0;
147
148
  this._debugRemoveCacheHits = 0;
149
+ this._subSendDebug = false;
150
+ this._subSendLogThresholdMs = 0;
151
+ this._subSendLogBytes = 0;
152
+ this._subSendLogSampleRate = 1;
148
153
  this._oplogRetryCount = 0;
149
154
  this._lastResumeToken = null;
150
155
  this._lastResumeTokenSaveMs = 0;
@@ -226,6 +231,10 @@ var SubscriptionManager = /** @class */ (function () {
226
231
  this.serverConfig = serverConfig;
227
232
  this._wss = wss;
228
233
  this._connectDebug = this.resolveConnectDebug();
234
+ this._subSendDebug = this.resolveSubSendDebug();
235
+ this._subSendLogThresholdMs = this.resolveSubSendLogThresholdMs();
236
+ this._subSendLogBytes = this.resolveSubSendLogBytes();
237
+ this._subSendLogSampleRate = this.resolveSubSendLogSampleRate();
229
238
  this._oplogMode = this.resolveOplogMode();
230
239
  this._localOplogResyncIntervalMs = this.resolveLocalOplogResyncIntervalMs();
231
240
  this.registerCorePublications();
@@ -530,7 +539,7 @@ var SubscriptionManager = /** @class */ (function () {
530
539
  if (!!_b.done) return [3 /*break*/, 10];
531
540
  client = _b.value;
532
541
  ws = this._websocketManager.getWebSocket(client.id_socket);
533
- if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === ws.OPEN)) return [3 /*break*/, 9];
542
+ if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN)) return [3 /*break*/, 9];
534
543
  _f.label = 6;
535
544
  case 6:
536
545
  _f.trys.push([6, 8, , 9]);
@@ -1231,6 +1240,38 @@ var SubscriptionManager = /** @class */ (function () {
1231
1240
  || config['CONNECT_DEBUG'];
1232
1241
  return this.parseDebugFlag(raw);
1233
1242
  };
1243
+ SubscriptionManager.prototype.resolveSubSendDebug = function () {
1244
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1245
+ var raw = process.env.SUB_SEND_DEBUG
1246
+ || process.env.SUBSCRIPTION_SEND_DEBUG
1247
+ || config['SUB_SEND_DEBUG']
1248
+ || config['SUBSCRIPTION_SEND_DEBUG'];
1249
+ return this.parseDebugFlag(raw);
1250
+ };
1251
+ SubscriptionManager.prototype.resolveSubSendLogThresholdMs = function () {
1252
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1253
+ var raw = process.env.SUB_SEND_LOG_THRESHOLD_MS
1254
+ || process.env.SUBSCRIPTION_SEND_LOG_THRESHOLD_MS
1255
+ || config['SUB_SEND_LOG_THRESHOLD_MS']
1256
+ || config['SUBSCRIPTION_SEND_LOG_THRESHOLD_MS'];
1257
+ return this.parsePositiveNumber(raw);
1258
+ };
1259
+ SubscriptionManager.prototype.resolveSubSendLogBytes = function () {
1260
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1261
+ var raw = process.env.SUB_SEND_LOG_BYTES
1262
+ || process.env.SUBSCRIPTION_SEND_LOG_BYTES
1263
+ || config['SUB_SEND_LOG_BYTES']
1264
+ || config['SUBSCRIPTION_SEND_LOG_BYTES'];
1265
+ return this.parsePositiveNumber(raw);
1266
+ };
1267
+ SubscriptionManager.prototype.resolveSubSendLogSampleRate = function () {
1268
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1269
+ var raw = process.env.SUB_SEND_LOG_SAMPLE_RATE
1270
+ || process.env.SUBSCRIPTION_SEND_LOG_SAMPLE_RATE
1271
+ || config['SUB_SEND_LOG_SAMPLE_RATE']
1272
+ || config['SUBSCRIPTION_SEND_LOG_SAMPLE_RATE'];
1273
+ return this.parseSampleRate(raw, this._subSendLogSampleRate);
1274
+ };
1234
1275
  SubscriptionManager.prototype.parseDebugFlag = function (value) {
1235
1276
  if (value === true) {
1236
1277
  return true;
@@ -1259,6 +1300,45 @@ var SubscriptionManager = /** @class */ (function () {
1259
1300
  }
1260
1301
  return 0;
1261
1302
  };
1303
+ SubscriptionManager.prototype.parseSampleRate = function (value, fallback) {
1304
+ var parsed = parseFloat(value !== null && value !== void 0 ? value : '');
1305
+ if (Number.isFinite(parsed) && parsed > 0 && parsed <= 1) {
1306
+ return parsed;
1307
+ }
1308
+ return fallback;
1309
+ };
1310
+ SubscriptionManager.prototype.shouldLogSubSend = function (durationMs, payloadBytes) {
1311
+ if (this._subSendLogSampleRate < 1 && Math.random() > this._subSendLogSampleRate) {
1312
+ return false;
1313
+ }
1314
+ if (this._subSendDebug) {
1315
+ return true;
1316
+ }
1317
+ if (this._subSendLogThresholdMs > 0 && durationMs >= this._subSendLogThresholdMs) {
1318
+ return true;
1319
+ }
1320
+ if (payloadBytes !== null && this._subSendLogBytes > 0 && payloadBytes >= this._subSendLogBytes) {
1321
+ return true;
1322
+ }
1323
+ return false;
1324
+ };
1325
+ SubscriptionManager.prototype.logSubSend = function (sub, details) {
1326
+ if (!details.clients) {
1327
+ return;
1328
+ }
1329
+ if (!this.shouldLogSubSend(details.durationMs, details.payloadBytes)) {
1330
+ return;
1331
+ }
1332
+ console.log(new Date(), '[Sub Send]', JSON.stringify({
1333
+ publication: sub.publication,
1334
+ subscriptionKey: sub.subscriptionKey,
1335
+ clients: details.clients,
1336
+ durationMs: details.durationMs,
1337
+ payloadBytes: details.payloadBytes,
1338
+ collection: details.collection,
1339
+ type: details.type
1340
+ }));
1341
+ };
1262
1342
  SubscriptionManager.prototype.connectDebug = function (message, details) {
1263
1343
  if (!this._connectDebug) {
1264
1344
  return;
@@ -1663,7 +1743,7 @@ var SubscriptionManager = /** @class */ (function () {
1663
1743
  if (!!_b.done) return [3 /*break*/, 9];
1664
1744
  client = _b.value;
1665
1745
  ws = this._websocketManager.getWebSocket(client.id_socket);
1666
- if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === ws.OPEN)) return [3 /*break*/, 8];
1746
+ if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN)) return [3 /*break*/, 8];
1667
1747
  _l.label = 5;
1668
1748
  case 5:
1669
1749
  _l.trys.push([5, 7, , 8]);
@@ -2233,7 +2313,7 @@ var SubscriptionManager = /** @class */ (function () {
2233
2313
  // Fetch pub once, send to all clients linked to this pub
2234
2314
  SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
2235
2315
  return __awaiter(this, void 0, void 0, function () {
2236
- var subIndex, monitor, res, dependencySnapshot, execution, packedRes, shouldCache, cachedBuffer, isSame, _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;
2316
+ var subIndex, monitor, res, dependencySnapshot, execution, packedRes, payloadBytes, shouldCache, cachedBuffer, isSame, sendStartMs, sentClients, _a, _b, client, ws, serverRes, sendStartMs, sentClients, _c, _d, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _e, normalizedError, correlationId, _f, _g, client, ws, serverRes, errorPayload;
2237
2317
  var e_6, _h, e_7, _j, e_8, _k;
2238
2318
  var _l, _m;
2239
2319
  return __generator(this, function (_o) {
@@ -2266,16 +2346,19 @@ var SubscriptionManager = /** @class */ (function () {
2266
2346
  dependencySnapshot = execution.snapshot;
2267
2347
  this.updateSubscriptionDependencies(sub, dependencySnapshot);
2268
2348
  packedRes = (_l = execution.packedResult) !== null && _l !== void 0 ? _l : this.packCachePayload(res);
2349
+ payloadBytes = packedRes ? packedRes.byteLength : null;
2269
2350
  shouldCache = this.shouldCachePayload(sub, packedRes);
2270
2351
  if (sub.cacheId) {
2271
2352
  cachedBuffer = this.getCacheBuffer(this._nodeCache.get(sub.cacheId));
2272
2353
  isSame = this.buffersEqual(cachedBuffer, packedRes);
2273
2354
  if (!isSame) {
2355
+ sendStartMs = Date.now();
2356
+ sentClients = 0;
2274
2357
  try {
2275
2358
  for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
2276
2359
  client = _b.value;
2277
2360
  ws = this._websocketManager.getWebSocket(client.id_socket);
2278
- if (ws && ws.readyState === ws.OPEN) {
2361
+ if (ws && ws.readyState === WebSocket.OPEN) {
2279
2362
  if (packedRes) {
2280
2363
  this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack');
2281
2364
  }
@@ -2287,6 +2370,7 @@ var SubscriptionManager = /** @class */ (function () {
2287
2370
  };
2288
2371
  this.sendWS(ws, serverRes);
2289
2372
  }
2373
+ sentClients += 1;
2290
2374
  }
2291
2375
  }
2292
2376
  }
@@ -2297,6 +2381,13 @@ var SubscriptionManager = /** @class */ (function () {
2297
2381
  }
2298
2382
  finally { if (e_6) throw e_6.error; }
2299
2383
  }
2384
+ this.logSubSend(sub, {
2385
+ collection: collection,
2386
+ type: type,
2387
+ clients: sentClients,
2388
+ durationMs: Date.now() - sendStartMs,
2389
+ payloadBytes: payloadBytes
2390
+ });
2300
2391
  this._nodeCache.del(sub.cacheId);
2301
2392
  if (shouldCache) {
2302
2393
  this._nodeCache.set(sub.cacheId, packedRes);
@@ -2314,11 +2405,13 @@ var SubscriptionManager = /** @class */ (function () {
2314
2405
  }
2315
2406
  }
2316
2407
  else {
2408
+ sendStartMs = Date.now();
2409
+ sentClients = 0;
2317
2410
  try {
2318
2411
  for (_c = __values(sub.clients), _d = _c.next(); !_d.done; _d = _c.next()) {
2319
2412
  client = _d.value;
2320
2413
  ws = this._websocketManager.getWebSocket(client.id_socket);
2321
- if (ws && ws.readyState === ws.OPEN) {
2414
+ if (ws && ws.readyState === WebSocket.OPEN) {
2322
2415
  if (packedRes) {
2323
2416
  this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack');
2324
2417
  }
@@ -2330,6 +2423,7 @@ var SubscriptionManager = /** @class */ (function () {
2330
2423
  };
2331
2424
  this.sendWS(ws, serverRes);
2332
2425
  }
2426
+ sentClients += 1;
2333
2427
  }
2334
2428
  }
2335
2429
  }
@@ -2340,6 +2434,13 @@ var SubscriptionManager = /** @class */ (function () {
2340
2434
  }
2341
2435
  finally { if (e_7) throw e_7.error; }
2342
2436
  }
2437
+ this.logSubSend(sub, {
2438
+ collection: collection,
2439
+ type: type,
2440
+ clients: sentClients,
2441
+ durationMs: Date.now() - sendStartMs,
2442
+ payloadBytes: payloadBytes
2443
+ });
2343
2444
  if (shouldCache) {
2344
2445
  sub.cacheId = this._cacheId++;
2345
2446
  this._nodeCache.set(sub.cacheId, packedRes);
@@ -2379,7 +2480,7 @@ var SubscriptionManager = /** @class */ (function () {
2379
2480
  for (_f = __values(sub.clients), _g = _f.next(); !_g.done; _g = _f.next()) {
2380
2481
  client = _g.value;
2381
2482
  ws = this._websocketManager.getWebSocket(client.id_socket);
2382
- if (ws && ws.readyState === ws.OPEN) {
2483
+ if (ws && ws.readyState === WebSocket.OPEN) {
2383
2484
  serverRes = {
2384
2485
  messageId: client.messageId,
2385
2486
  hasError: true,