@resolveio/server-lib 20.14.37 → 20.14.39

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;
@@ -146,6 +146,10 @@ var SubscriptionManager = /** @class */ (function () {
146
146
  this._debugMongoQueueCollections = [];
147
147
  this._debugSendQueueHits = 0;
148
148
  this._debugRemoveCacheHits = 0;
149
+ this._subSendDebug = false;
150
+ this._subSendLogThresholdMs = 0;
151
+ this._subSendLogBytes = 0;
152
+ this._subSendLogSampleRate = 1;
149
153
  this._oplogRetryCount = 0;
150
154
  this._lastResumeToken = null;
151
155
  this._lastResumeTokenSaveMs = 0;
@@ -227,6 +231,10 @@ var SubscriptionManager = /** @class */ (function () {
227
231
  this.serverConfig = serverConfig;
228
232
  this._wss = wss;
229
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();
230
238
  this._oplogMode = this.resolveOplogMode();
231
239
  this._localOplogResyncIntervalMs = this.resolveLocalOplogResyncIntervalMs();
232
240
  this.registerCorePublications();
@@ -1232,6 +1240,38 @@ var SubscriptionManager = /** @class */ (function () {
1232
1240
  || config['CONNECT_DEBUG'];
1233
1241
  return this.parseDebugFlag(raw);
1234
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
+ };
1235
1275
  SubscriptionManager.prototype.parseDebugFlag = function (value) {
1236
1276
  if (value === true) {
1237
1277
  return true;
@@ -1260,6 +1300,45 @@ var SubscriptionManager = /** @class */ (function () {
1260
1300
  }
1261
1301
  return 0;
1262
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
+ };
1263
1342
  SubscriptionManager.prototype.connectDebug = function (message, details) {
1264
1343
  if (!this._connectDebug) {
1265
1344
  return;
@@ -2234,7 +2313,7 @@ var SubscriptionManager = /** @class */ (function () {
2234
2313
  // Fetch pub once, send to all clients linked to this pub
2235
2314
  SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
2236
2315
  return __awaiter(this, void 0, void 0, function () {
2237
- 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;
2238
2317
  var e_6, _h, e_7, _j, e_8, _k;
2239
2318
  var _l, _m;
2240
2319
  return __generator(this, function (_o) {
@@ -2267,11 +2346,14 @@ var SubscriptionManager = /** @class */ (function () {
2267
2346
  dependencySnapshot = execution.snapshot;
2268
2347
  this.updateSubscriptionDependencies(sub, dependencySnapshot);
2269
2348
  packedRes = (_l = execution.packedResult) !== null && _l !== void 0 ? _l : this.packCachePayload(res);
2349
+ payloadBytes = packedRes ? packedRes.byteLength : null;
2270
2350
  shouldCache = this.shouldCachePayload(sub, packedRes);
2271
2351
  if (sub.cacheId) {
2272
2352
  cachedBuffer = this.getCacheBuffer(this._nodeCache.get(sub.cacheId));
2273
2353
  isSame = this.buffersEqual(cachedBuffer, packedRes);
2274
2354
  if (!isSame) {
2355
+ sendStartMs = Date.now();
2356
+ sentClients = 0;
2275
2357
  try {
2276
2358
  for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
2277
2359
  client = _b.value;
@@ -2288,6 +2370,7 @@ var SubscriptionManager = /** @class */ (function () {
2288
2370
  };
2289
2371
  this.sendWS(ws, serverRes);
2290
2372
  }
2373
+ sentClients += 1;
2291
2374
  }
2292
2375
  }
2293
2376
  }
@@ -2298,6 +2381,13 @@ var SubscriptionManager = /** @class */ (function () {
2298
2381
  }
2299
2382
  finally { if (e_6) throw e_6.error; }
2300
2383
  }
2384
+ this.logSubSend(sub, {
2385
+ collection: collection,
2386
+ type: type,
2387
+ clients: sentClients,
2388
+ durationMs: Date.now() - sendStartMs,
2389
+ payloadBytes: payloadBytes
2390
+ });
2301
2391
  this._nodeCache.del(sub.cacheId);
2302
2392
  if (shouldCache) {
2303
2393
  this._nodeCache.set(sub.cacheId, packedRes);
@@ -2315,6 +2405,8 @@ var SubscriptionManager = /** @class */ (function () {
2315
2405
  }
2316
2406
  }
2317
2407
  else {
2408
+ sendStartMs = Date.now();
2409
+ sentClients = 0;
2318
2410
  try {
2319
2411
  for (_c = __values(sub.clients), _d = _c.next(); !_d.done; _d = _c.next()) {
2320
2412
  client = _d.value;
@@ -2331,6 +2423,7 @@ var SubscriptionManager = /** @class */ (function () {
2331
2423
  };
2332
2424
  this.sendWS(ws, serverRes);
2333
2425
  }
2426
+ sentClients += 1;
2334
2427
  }
2335
2428
  }
2336
2429
  }
@@ -2341,6 +2434,13 @@ var SubscriptionManager = /** @class */ (function () {
2341
2434
  }
2342
2435
  finally { if (e_7) throw e_7.error; }
2343
2436
  }
2437
+ this.logSubSend(sub, {
2438
+ collection: collection,
2439
+ type: type,
2440
+ clients: sentClients,
2441
+ durationMs: Date.now() - sendStartMs,
2442
+ payloadBytes: payloadBytes
2443
+ });
2344
2444
  if (shouldCache) {
2345
2445
  sub.cacheId = this._cacheId++;
2346
2446
  this._nodeCache.set(sub.cacheId, packedRes);