@resolveio/server-lib 20.14.27 → 20.14.29

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.
@@ -75,8 +75,6 @@ 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 os = require("os");
79
- var perf_hooks_1 = require("perf_hooks");
80
78
  var flag_collection_1 = require("../collections/flag.collection");
81
79
  var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
82
80
  var app_status_1 = require("../publications/app-status");
@@ -172,39 +170,6 @@ var SubscriptionManager = /** @class */ (function () {
172
170
  this._pendingInvalidations = new Map();
173
171
  this.DEBOUNCE_DELAY = 100; // 100ms debounce window
174
172
  this.MAX_WAIT_TIME = 500; // 500ms maximum delay
175
- this._warmupEnabled = false;
176
- this._warmupEndsAtMs = 0;
177
- this._warmupQueue = [];
178
- this._warmupQueueKeys = new Set();
179
- this._warmupTimer = null;
180
- this._warmupInFlight = 0;
181
- this._warmupProcessing = false;
182
- this._warmupMaxInFlight = 2;
183
- this._warmupMaxPerTick = 2;
184
- this._warmupTickMs = 200;
185
- this._warmupQueueLimit = 0;
186
- this._warmupLogIntervalMs = 10000;
187
- this._warmupLastLogMs = 0;
188
- this._warmupAllowPublications = new Set();
189
- this._warmupRampMaxPerTick = 0;
190
- this._warmupRampMaxInFlight = 0;
191
- this._warmupWindowEndedLogged = false;
192
- this._warmupAdaptiveEnabled = true;
193
- this._warmupAdaptiveSampleMs = 1000;
194
- this._warmupAdaptiveAlpha = 0.2;
195
- this._warmupTargetCpuPct = 55;
196
- this._warmupMaxCpuPct = 85;
197
- this._warmupTargetMemPct = 70;
198
- this._warmupMaxMemPct = 85;
199
- this._warmupTargetLagMs = 80;
200
- this._warmupMaxLagMs = 200;
201
- this._warmupAdaptiveScale = 0;
202
- this._warmupAdaptiveLastSampleAt = 0;
203
- this._warmupAdaptiveMetrics = { cpuPct: 0, memPct: 0, rssMB: 0, lagMs: 0, pressure: 0 };
204
- this._warmupCpuSampleUsage = process.cpuUsage();
205
- this._warmupCpuSampleAt = process.hrtime.bigint();
206
- this._warmupLagMonitor = null;
207
- this._publicationWorkersEnabled = true;
208
173
  this._publicationWorkerQueueLimit = 0;
209
174
  }
210
175
  SubscriptionManager.createPublicationRegistry = function (serverConfig) {
@@ -399,7 +364,6 @@ var SubscriptionManager = /** @class */ (function () {
399
364
  this._enableDependencyDebug = process.env.ENABLE_DEPENDENCY_DEBUG === 'true';
400
365
  }
401
366
  this.setCacheLimit();
402
- this.configureWarmup();
403
367
  this.configurePublicationWorkers();
404
368
  return [2 /*return*/];
405
369
  }
@@ -430,76 +394,10 @@ var SubscriptionManager = /** @class */ (function () {
430
394
  this._heapLimit = this._heapSize * 0.3; // Use 50% of total heap size
431
395
  }
432
396
  };
433
- SubscriptionManager.prototype.configureWarmup = function () {
434
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
435
- var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
436
- var enabledRaw = (_a = process.env.SUBSCRIPTION_WARMUP_ENABLED) !== null && _a !== void 0 ? _a : config['SUBSCRIPTION_WARMUP_ENABLED'];
437
- var warmupMs = this.parsePositiveNumber((_b = process.env.SUBSCRIPTION_WARMUP_MS) !== null && _b !== void 0 ? _b : config['SUBSCRIPTION_WARMUP_MS']);
438
- var enabled = this.parseBoolean(enabledRaw) || warmupMs > 0;
439
- if (!enabled) {
440
- this._warmupEnabled = false;
441
- return;
442
- }
443
- var durationMs = warmupMs;
444
- this._warmupEnabled = true;
445
- this._warmupEndsAtMs = durationMs > 0 ? Date.now() + durationMs : 0;
446
- this._warmupTickMs = this.parsePositiveNumber((_c = process.env.SUBSCRIPTION_WARMUP_TICK_MS) !== null && _c !== void 0 ? _c : config['SUBSCRIPTION_WARMUP_TICK_MS']) || this._warmupTickMs;
447
- this._warmupMaxPerTick = this.parsePositiveNumber((_d = process.env.SUBSCRIPTION_WARMUP_MAX_PER_TICK) !== null && _d !== void 0 ? _d : config['SUBSCRIPTION_WARMUP_MAX_PER_TICK']) || this._warmupMaxPerTick;
448
- this._warmupMaxInFlight = this.parsePositiveNumber((_e = process.env.SUBSCRIPTION_WARMUP_MAX_IN_FLIGHT) !== null && _e !== void 0 ? _e : config['SUBSCRIPTION_WARMUP_MAX_IN_FLIGHT']) || Math.max(1, this._warmupMaxPerTick);
449
- this._warmupRampMaxPerTick = this.parsePositiveNumber((_f = process.env.SUBSCRIPTION_WARMUP_RAMP_MAX_PER_TICK) !== null && _f !== void 0 ? _f : config['SUBSCRIPTION_WARMUP_RAMP_MAX_PER_TICK']) || this._warmupMaxPerTick;
450
- this._warmupRampMaxInFlight = this.parsePositiveNumber((_g = process.env.SUBSCRIPTION_WARMUP_RAMP_MAX_IN_FLIGHT) !== null && _g !== void 0 ? _g : config['SUBSCRIPTION_WARMUP_RAMP_MAX_IN_FLIGHT']) || this._warmupMaxInFlight;
451
- this._warmupQueueLimit = this.parsePositiveNumber((_h = process.env.SUBSCRIPTION_WARMUP_QUEUE_LIMIT) !== null && _h !== void 0 ? _h : config['SUBSCRIPTION_WARMUP_QUEUE_LIMIT']) || this._warmupQueueLimit;
452
- this._warmupLogIntervalMs = this.parsePositiveNumber((_j = process.env.SUBSCRIPTION_WARMUP_LOG_INTERVAL_MS) !== null && _j !== void 0 ? _j : config['SUBSCRIPTION_WARMUP_LOG_INTERVAL_MS']) || this._warmupLogIntervalMs;
453
- var adaptiveRaw = (_k = process.env.SUBSCRIPTION_WARMUP_ADAPTIVE_ENABLED) !== null && _k !== void 0 ? _k : config['SUBSCRIPTION_WARMUP_ADAPTIVE_ENABLED'];
454
- this._warmupAdaptiveEnabled = adaptiveRaw === undefined ? true : this.parseBoolean(adaptiveRaw);
455
- this._warmupAdaptiveSampleMs = this.parsePositiveNumber((_l = process.env.SUBSCRIPTION_WARMUP_ADAPTIVE_SAMPLE_MS) !== null && _l !== void 0 ? _l : config['SUBSCRIPTION_WARMUP_ADAPTIVE_SAMPLE_MS']) || this._warmupAdaptiveSampleMs;
456
- this._warmupAdaptiveAlpha = this.parsePositiveFloat((_m = process.env.SUBSCRIPTION_WARMUP_ADAPTIVE_ALPHA) !== null && _m !== void 0 ? _m : config['SUBSCRIPTION_WARMUP_ADAPTIVE_ALPHA']) || this._warmupAdaptiveAlpha;
457
- this._warmupTargetCpuPct = this.parsePositiveNumber((_o = process.env.SUBSCRIPTION_WARMUP_TARGET_CPU_PCT) !== null && _o !== void 0 ? _o : config['SUBSCRIPTION_WARMUP_TARGET_CPU_PCT']) || this._warmupTargetCpuPct;
458
- this._warmupMaxCpuPct = this.parsePositiveNumber((_p = process.env.SUBSCRIPTION_WARMUP_MAX_CPU_PCT) !== null && _p !== void 0 ? _p : config['SUBSCRIPTION_WARMUP_MAX_CPU_PCT']) || this._warmupMaxCpuPct;
459
- this._warmupTargetMemPct = this.parsePositiveNumber((_q = process.env.SUBSCRIPTION_WARMUP_TARGET_MEM_PCT) !== null && _q !== void 0 ? _q : config['SUBSCRIPTION_WARMUP_TARGET_MEM_PCT']) || this._warmupTargetMemPct;
460
- this._warmupMaxMemPct = this.parsePositiveNumber((_r = process.env.SUBSCRIPTION_WARMUP_MAX_MEM_PCT) !== null && _r !== void 0 ? _r : config['SUBSCRIPTION_WARMUP_MAX_MEM_PCT']) || this._warmupMaxMemPct;
461
- this._warmupTargetLagMs = this.parsePositiveNumber((_s = process.env.SUBSCRIPTION_WARMUP_TARGET_LAG_MS) !== null && _s !== void 0 ? _s : config['SUBSCRIPTION_WARMUP_TARGET_LAG_MS']) || this._warmupTargetLagMs;
462
- this._warmupMaxLagMs = this.parsePositiveNumber((_t = process.env.SUBSCRIPTION_WARMUP_MAX_LAG_MS) !== null && _t !== void 0 ? _t : config['SUBSCRIPTION_WARMUP_MAX_LAG_MS']) || this._warmupMaxLagMs;
463
- this._warmupAdaptiveScale = 0;
464
- this._warmupAdaptiveLastSampleAt = 0;
465
- this._warmupWindowEndedLogged = false;
466
- if (this._warmupAdaptiveEnabled) {
467
- this.ensureWarmupAdaptiveMonitor();
468
- }
469
- var allowRaw = (_u = process.env.SUBSCRIPTION_WARMUP_ALLOW_PUBS) !== null && _u !== void 0 ? _u : config['SUBSCRIPTION_WARMUP_ALLOW_PUBS'];
470
- if (typeof allowRaw === 'string' && allowRaw.trim()) {
471
- this._warmupAllowPublications = new Set(allowRaw
472
- .split(',')
473
- .map(function (entry) { return entry.trim(); })
474
- .filter(Boolean));
475
- }
476
- this.startWarmupTimer();
477
- console.log(new Date(), 'Sub Manager', 'Warmup enabled', {
478
- minDurationMs: durationMs,
479
- tickMs: this._warmupTickMs,
480
- maxPerTick: this._warmupMaxPerTick,
481
- maxInFlight: this._warmupMaxInFlight,
482
- rampMaxPerTick: this._warmupRampMaxPerTick,
483
- rampMaxInFlight: this._warmupRampMaxInFlight,
484
- adaptiveEnabled: this._warmupAdaptiveEnabled,
485
- adaptiveSampleMs: this._warmupAdaptiveSampleMs,
486
- adaptiveAlpha: this._warmupAdaptiveAlpha,
487
- targetCpuPct: this._warmupTargetCpuPct,
488
- maxCpuPct: this._warmupMaxCpuPct,
489
- targetMemPct: this._warmupTargetMemPct,
490
- maxMemPct: this._warmupMaxMemPct,
491
- targetLagMs: this._warmupTargetLagMs,
492
- maxLagMs: this._warmupMaxLagMs,
493
- queueLimit: this._warmupQueueLimit,
494
- allowPubs: Array.from(this._warmupAllowPublications.values())
495
- });
496
- };
497
397
  SubscriptionManager.prototype.configurePublicationWorkers = function () {
498
- var _a, _b;
398
+ var _a;
499
399
  var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
500
- var enabledRaw = (_a = process.env.SUBSCRIPTION_WORKERS_ENABLED) !== null && _a !== void 0 ? _a : config['SUBSCRIPTION_WORKERS_ENABLED'];
501
- this._publicationWorkersEnabled = enabledRaw === undefined ? true : this.parseBoolean(enabledRaw);
502
- this._publicationWorkerQueueLimit = this.parsePositiveNumber((_b = process.env.SUBSCRIPTION_WORKER_QUEUE_MAX) !== null && _b !== void 0 ? _b : config['SUBSCRIPTION_WORKER_QUEUE_MAX']);
400
+ this._publicationWorkerQueueLimit = this.parsePositiveNumber((_a = process.env.SUBSCRIPTION_WORKER_QUEUE_MAX) !== null && _a !== void 0 ? _a : config['SUBSCRIPTION_WORKER_QUEUE_MAX']);
503
401
  };
504
402
  SubscriptionManager.prototype.parseBoolean = function (value) {
505
403
  if (value === true) {
@@ -526,368 +424,6 @@ var SubscriptionManager = /** @class */ (function () {
526
424
  }
527
425
  return value;
528
426
  };
529
- SubscriptionManager.prototype.ensureWarmupAdaptiveMonitor = function () {
530
- if (this._warmupLagMonitor) {
531
- return;
532
- }
533
- this._warmupLagMonitor = (0, perf_hooks_1.monitorEventLoopDelay)({ resolution: 20 });
534
- this._warmupLagMonitor.enable();
535
- this._warmupCpuSampleUsage = process.cpuUsage();
536
- this._warmupCpuSampleAt = process.hrtime.bigint();
537
- this._warmupAdaptiveLastSampleAt = Date.now();
538
- };
539
- SubscriptionManager.prototype.maybeDisableWarmup = function (nowMs) {
540
- if (!this._warmupEnabled) {
541
- return;
542
- }
543
- if (this._warmupQueue.length > 0 || this._warmupInFlight > 0) {
544
- return;
545
- }
546
- if (this._warmupEndsAtMs > 0 && nowMs < this._warmupEndsAtMs) {
547
- return;
548
- }
549
- this._warmupEnabled = false;
550
- this._warmupAdaptiveScale = 0;
551
- this._warmupAdaptiveLastSampleAt = 0;
552
- if (this._warmupLagMonitor) {
553
- this._warmupLagMonitor.disable();
554
- this._warmupLagMonitor = null;
555
- }
556
- console.log(new Date(), 'Sub Manager', 'Warmup complete', {
557
- queued: this._warmupQueue.length,
558
- inFlight: this._warmupInFlight
559
- });
560
- };
561
- SubscriptionManager.prototype.normalizePressure = function (value, target, max) {
562
- if (!Number.isFinite(value)) {
563
- return 0;
564
- }
565
- if (max <= target) {
566
- return value > target ? 1 : 0;
567
- }
568
- var pressure = (value - target) / (max - target);
569
- return this.clampNumber(pressure, 0, 1);
570
- };
571
- SubscriptionManager.prototype.sampleCpuPercent = function () {
572
- var _a, _b, _c;
573
- var usage = process.cpuUsage();
574
- var now = process.hrtime.bigint();
575
- var elapsedMicros = Number(now - this._warmupCpuSampleAt) / 1000;
576
- var usageDelta = (usage.user - this._warmupCpuSampleUsage.user) + (usage.system - this._warmupCpuSampleUsage.system);
577
- this._warmupCpuSampleUsage = usage;
578
- this._warmupCpuSampleAt = now;
579
- if (!elapsedMicros || elapsedMicros <= 0) {
580
- return (_b = (_a = this._warmupAdaptiveMetrics) === null || _a === void 0 ? void 0 : _a.cpuPct) !== null && _b !== void 0 ? _b : 0;
581
- }
582
- var cores = Math.max(1, ((_c = os.cpus()) === null || _c === void 0 ? void 0 : _c.length) || 1);
583
- var cpuPct = (usageDelta / (elapsedMicros * cores)) * 100;
584
- return this.clampNumber(cpuPct, 0, 100);
585
- };
586
- SubscriptionManager.prototype.sampleWarmupAdaptiveMetrics = function (nowMs) {
587
- if (!this._warmupAdaptiveEnabled) {
588
- return this._warmupAdaptiveMetrics;
589
- }
590
- if (this._warmupAdaptiveLastSampleAt && (nowMs - this._warmupAdaptiveLastSampleAt) < this._warmupAdaptiveSampleMs) {
591
- return this._warmupAdaptiveMetrics;
592
- }
593
- this.ensureWarmupAdaptiveMonitor();
594
- var cpuPct = this.sampleCpuPercent();
595
- var mem = process.memoryUsage();
596
- var totalMem = os.totalmem();
597
- var rssMB = mem.rss / (1024 * 1024);
598
- var rssPct = totalMem > 0 ? (mem.rss / totalMem) * 100 : 0;
599
- var systemMemPct = totalMem > 0 ? ((totalMem - os.freemem()) / totalMem) * 100 : 0;
600
- var memPct = this.clampNumber(Math.max(rssPct, systemMemPct), 0, 100);
601
- var lagMs = 0;
602
- if (this._warmupLagMonitor) {
603
- lagMs = this._warmupLagMonitor.percentile(99) / 1e6;
604
- this._warmupLagMonitor.reset();
605
- }
606
- var cpuPressure = this.normalizePressure(cpuPct, this._warmupTargetCpuPct, this._warmupMaxCpuPct);
607
- var memPressure = this.normalizePressure(memPct, this._warmupTargetMemPct, this._warmupMaxMemPct);
608
- var lagPressure = this.normalizePressure(lagMs, this._warmupTargetLagMs, this._warmupMaxLagMs);
609
- var pressure = Math.max(cpuPressure, memPressure, lagPressure);
610
- this._warmupAdaptiveMetrics = {
611
- cpuPct: cpuPct,
612
- memPct: memPct,
613
- rssMB: rssMB,
614
- lagMs: lagMs,
615
- pressure: pressure
616
- };
617
- this._warmupAdaptiveLastSampleAt = nowMs;
618
- return this._warmupAdaptiveMetrics;
619
- };
620
- SubscriptionManager.prototype.updateAdaptiveScale = function (targetScale) {
621
- var alpha = this.clampNumber(this._warmupAdaptiveAlpha, 0, 1);
622
- if (!Number.isFinite(this._warmupAdaptiveScale)) {
623
- this._warmupAdaptiveScale = targetScale;
624
- return this._warmupAdaptiveScale;
625
- }
626
- this._warmupAdaptiveScale = this.clampNumber((alpha * targetScale) + ((1 - alpha) * this._warmupAdaptiveScale), 0, 1);
627
- return this._warmupAdaptiveScale;
628
- };
629
- SubscriptionManager.prototype.isWarmupGateActive = function () {
630
- if (!this._warmupEnabled) {
631
- return false;
632
- }
633
- return true;
634
- };
635
- SubscriptionManager.prototype.shouldQueueWarmup = function (publication, messageRoute) {
636
- if (!this.isWarmupGateActive()) {
637
- return false;
638
- }
639
- if (messageRoute === 'Bypass') {
640
- return false;
641
- }
642
- if (this._warmupAllowPublications.has(publication)) {
643
- return false;
644
- }
645
- return true;
646
- };
647
- SubscriptionManager.prototype.buildWarmupKey = function (socketId, messageId, publication, subscriptionData) {
648
- var normalizedSubscriptionData = Array.isArray(subscriptionData) ? subscriptionData : [];
649
- return "".concat(socketId, ":").concat(messageId, ":").concat(publication, ":").concat(JSON.stringify(normalizedSubscriptionData));
650
- };
651
- SubscriptionManager.prototype.queueWarmupSubscription = function (params) {
652
- var _a;
653
- if (!this.isWarmupGateActive()) {
654
- return false;
655
- }
656
- if (this._warmupQueueLimit > 0 && this._warmupQueue.length >= this._warmupQueueLimit) {
657
- if (this._enableDebug) {
658
- console.log(new Date(), 'Sub Manager', 'Warmup queue full; processing immediately', this._warmupQueue.length);
659
- }
660
- return false;
661
- }
662
- var socketId = (_a = params.ws) === null || _a === void 0 ? void 0 : _a['id_socket'];
663
- if (!socketId) {
664
- return false;
665
- }
666
- var key = this.buildWarmupKey(socketId, params.messageId, params.publication, params.subscriptionData);
667
- if (this._warmupQueueKeys.has(key)) {
668
- return true;
669
- }
670
- this._warmupQueue.push({
671
- key: key,
672
- socketId: socketId,
673
- messageRoute: params.messageRoute,
674
- messageDate: params.messageDate,
675
- messageId: params.messageId,
676
- publication: params.publication,
677
- subscriptionData: params.subscriptionData,
678
- enqueuedAt: Date.now()
679
- });
680
- this._warmupQueueKeys.add(key);
681
- this.startWarmupTimer();
682
- this.logWarmupStatus('queued');
683
- return true;
684
- };
685
- SubscriptionManager.prototype.startWarmupTimer = function () {
686
- var _this = this;
687
- if (this._warmupTimer) {
688
- return;
689
- }
690
- this._warmupTimer = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
691
- var error_1;
692
- return __generator(this, function (_a) {
693
- switch (_a.label) {
694
- case 0:
695
- _a.trys.push([0, 2, , 3]);
696
- return [4 /*yield*/, this.processWarmupQueue()];
697
- case 1:
698
- _a.sent();
699
- return [3 /*break*/, 3];
700
- case 2:
701
- error_1 = _a.sent();
702
- console.error(new Date(), 'Sub Manager', 'Warmup queue processing failed', error_1);
703
- return [3 /*break*/, 3];
704
- case 3: return [2 /*return*/];
705
- }
706
- });
707
- }); }, this._warmupTickMs);
708
- };
709
- SubscriptionManager.prototype.stopWarmupTimerIfIdle = function () {
710
- if (!this._warmupTimer) {
711
- return;
712
- }
713
- if (this._warmupQueue.length > 0 || this._warmupInFlight > 0) {
714
- return;
715
- }
716
- clearInterval(this._warmupTimer);
717
- this._warmupTimer = null;
718
- this.maybeDisableWarmup(Date.now());
719
- };
720
- SubscriptionManager.prototype.processWarmupQueue = function () {
721
- return __awaiter(this, void 0, void 0, function () {
722
- var pendingTasks, now, limits, processed, _loop_3, this_3, state_1;
723
- var _a;
724
- return __generator(this, function (_b) {
725
- switch (_b.label) {
726
- case 0:
727
- if (this._warmupProcessing) {
728
- return [2 /*return*/];
729
- }
730
- this._warmupProcessing = true;
731
- _b.label = 1;
732
- case 1:
733
- _b.trys.push([1, , 4, 5]);
734
- pendingTasks = [];
735
- now = Date.now();
736
- if (this._warmupEnabled && this._warmupEndsAtMs > 0 && now >= this._warmupEndsAtMs && !this._warmupWindowEndedLogged) {
737
- this._warmupWindowEndedLogged = true;
738
- console.log(new Date(), 'Sub Manager', 'Warmup minimum window ended', {
739
- queued: this._warmupQueue.length
740
- });
741
- }
742
- if (!this._warmupQueue.length) {
743
- this.stopWarmupTimerIfIdle();
744
- return [2 /*return*/];
745
- }
746
- limits = this.getWarmupLimits(now);
747
- processed = 0;
748
- _loop_3 = function () {
749
- var item = this_3._warmupQueue.shift();
750
- if (!item) {
751
- return "break";
752
- }
753
- this_3._warmupQueueKeys.delete(item.key);
754
- var wasLastItem = this_3._warmupQueue.length === 0;
755
- var ws = this_3._websocketManager.getWebSocket(item.socketId);
756
- if (!ws || ws.readyState !== ws.OPEN) {
757
- return "continue";
758
- }
759
- var normalizedSubscriptionData = Array.isArray(item.subscriptionData) ? item.subscriptionData : [];
760
- var subscriptionKey = JSON.stringify(normalizedSubscriptionData);
761
- var sub = this_3._subscriptions.find(function (a) { return a.publication === item.publication && a.subscriptionKey === subscriptionKey; });
762
- if (!sub || !((_a = sub.clients) === null || _a === void 0 ? void 0 : _a.length)) {
763
- return "continue";
764
- }
765
- if (sub.running) {
766
- this_3.requeueWarmupItem(item);
767
- processed += 1;
768
- if (wasLastItem) {
769
- return "break";
770
- }
771
- return "continue";
772
- }
773
- this_3._warmupInFlight += 1;
774
- processed += 1;
775
- pendingTasks.push(this_3.processWarmupItemWithCleanup(item, ws, sub));
776
- };
777
- this_3 = this;
778
- while (this._warmupQueue.length &&
779
- processed < limits.maxPerTick &&
780
- this._warmupInFlight < limits.maxInFlight) {
781
- state_1 = _loop_3();
782
- if (state_1 === "break")
783
- break;
784
- }
785
- this.logWarmupStatus('tick', limits);
786
- if (!pendingTasks.length) return [3 /*break*/, 3];
787
- return [4 /*yield*/, Promise.allSettled(pendingTasks)];
788
- case 2:
789
- _b.sent();
790
- _b.label = 3;
791
- case 3: return [3 /*break*/, 5];
792
- case 4:
793
- this._warmupProcessing = false;
794
- return [7 /*endfinally*/];
795
- case 5: return [2 /*return*/];
796
- }
797
- });
798
- });
799
- };
800
- SubscriptionManager.prototype.processWarmupItem = function (item, ws, sub) {
801
- return __awaiter(this, void 0, void 0, function () {
802
- return __generator(this, function (_a) {
803
- switch (_a.label) {
804
- case 0: return [4 /*yield*/, this.processSubscription(sub, ws, item.messageId)];
805
- case 1:
806
- _a.sent();
807
- return [2 /*return*/];
808
- }
809
- });
810
- });
811
- };
812
- SubscriptionManager.prototype.processWarmupItemWithCleanup = function (item, ws, sub) {
813
- return __awaiter(this, void 0, void 0, function () {
814
- var error_2;
815
- return __generator(this, function (_a) {
816
- switch (_a.label) {
817
- case 0:
818
- _a.trys.push([0, 2, 3, 4]);
819
- return [4 /*yield*/, this.processWarmupItem(item, ws, sub)];
820
- case 1:
821
- _a.sent();
822
- return [3 /*break*/, 4];
823
- case 2:
824
- error_2 = _a.sent();
825
- console.error(new Date(), 'Sub Manager', 'Warmup item failed', error_2);
826
- return [3 /*break*/, 4];
827
- case 3:
828
- this._warmupInFlight -= 1;
829
- this.stopWarmupTimerIfIdle();
830
- return [7 /*endfinally*/];
831
- case 4: return [2 /*return*/];
832
- }
833
- });
834
- });
835
- };
836
- SubscriptionManager.prototype.getWarmupLimits = function (nowMs) {
837
- var basePerTick = Math.max(1, this._warmupMaxPerTick);
838
- var baseInFlight = Math.max(1, this._warmupMaxInFlight);
839
- var targetPerTick = Math.max(basePerTick, this._warmupRampMaxPerTick || basePerTick);
840
- var targetInFlight = Math.max(baseInFlight, this._warmupRampMaxInFlight || baseInFlight);
841
- if (!this._warmupAdaptiveEnabled) {
842
- return { maxPerTick: basePerTick, maxInFlight: baseInFlight, rampActive: false, rampProgress: 0 };
843
- }
844
- var metrics = this.sampleWarmupAdaptiveMetrics(nowMs);
845
- var targetScale = this.clampNumber(1 - metrics.pressure, 0, 1);
846
- var scale = this.updateAdaptiveScale(targetScale);
847
- var maxPerTick = Math.max(1, (0, common_1.round)(basePerTick + (targetPerTick - basePerTick) * scale));
848
- var maxInFlight = Math.max(1, (0, common_1.round)(baseInFlight + (targetInFlight - baseInFlight) * scale));
849
- return { maxPerTick: maxPerTick, maxInFlight: maxInFlight, rampActive: true, rampProgress: scale, adaptiveScale: scale, metrics: metrics };
850
- };
851
- SubscriptionManager.prototype.requeueWarmupItem = function (item) {
852
- if (this._warmupQueueKeys.has(item.key)) {
853
- return;
854
- }
855
- item.enqueuedAt = Date.now();
856
- this._warmupQueue.push(item);
857
- this._warmupQueueKeys.add(item.key);
858
- };
859
- SubscriptionManager.prototype.logWarmupStatus = function (reason, limits) {
860
- var _a, _b, _c;
861
- if (!this._enableDebug) {
862
- return;
863
- }
864
- if (!this._warmupLogIntervalMs) {
865
- return;
866
- }
867
- var now = Date.now();
868
- if (now - this._warmupLastLogMs < this._warmupLogIntervalMs) {
869
- return;
870
- }
871
- this._warmupLastLogMs = now;
872
- var metrics = limits === null || limits === void 0 ? void 0 : limits.metrics;
873
- console.log(new Date(), 'Sub Manager', 'Warmup status', {
874
- reason: reason,
875
- enabled: this._warmupEnabled,
876
- endsInMs: this._warmupEnabled ? Math.max(0, this._warmupEndsAtMs - now) : 0,
877
- queued: this._warmupQueue.length,
878
- inFlight: this._warmupInFlight,
879
- maxPerTick: (_a = limits === null || limits === void 0 ? void 0 : limits.maxPerTick) !== null && _a !== void 0 ? _a : this._warmupMaxPerTick,
880
- maxInFlight: (_b = limits === null || limits === void 0 ? void 0 : limits.maxInFlight) !== null && _b !== void 0 ? _b : this._warmupMaxInFlight,
881
- rampActive: (_c = limits === null || limits === void 0 ? void 0 : limits.rampActive) !== null && _c !== void 0 ? _c : false,
882
- rampProgress: typeof (limits === null || limits === void 0 ? void 0 : limits.rampProgress) === 'number' ? (0, common_1.round)(limits.rampProgress * 100) : 0,
883
- adaptiveScale: typeof (limits === null || limits === void 0 ? void 0 : limits.adaptiveScale) === 'number' ? (0, common_1.round)(limits.adaptiveScale * 100) : 0,
884
- cpuPct: metrics ? (0, common_1.round)(metrics.cpuPct) : undefined,
885
- memPct: metrics ? (0, common_1.round)(metrics.memPct) : undefined,
886
- rssMB: metrics ? (0, common_1.round)(metrics.rssMB) : undefined,
887
- lagMs: metrics ? (0, common_1.round)(metrics.lagMs) : undefined,
888
- pressure: metrics ? (0, common_1.round)(metrics.pressure * 100) : undefined
889
- });
890
- };
891
427
  SubscriptionManager.prototype.invalidatePubsCache = function (collection, type, documentId, document) {
892
428
  return __awaiter(this, void 0, void 0, function () {
893
429
  var queue, debounceKey, now, firstInvalidationTime, waitedTooLong;
@@ -1112,9 +648,6 @@ var SubscriptionManager = /** @class */ (function () {
1112
648
  : null;
1113
649
  };
1114
650
  SubscriptionManager.prototype.shouldUsePublicationWorkers = function () {
1115
- if (!this._publicationWorkersEnabled) {
1116
- return false;
1117
- }
1118
651
  var dispatcher = this.getPublicationWorkerDispatcher();
1119
652
  if (!dispatcher || !dispatcher.hasWorkers()) {
1120
653
  return false;
@@ -1129,7 +662,7 @@ var SubscriptionManager = /** @class */ (function () {
1129
662
  };
1130
663
  SubscriptionManager.prototype.runPublicationViaWorker = function (sub, userId) {
1131
664
  return __awaiter(this, void 0, void 0, function () {
1132
- var dispatcher, response, meta, snapshot, error_3;
665
+ var dispatcher, response, meta, snapshot, error_1;
1133
666
  return __generator(this, function (_a) {
1134
667
  switch (_a.label) {
1135
668
  case 0:
@@ -1165,9 +698,9 @@ var SubscriptionManager = /** @class */ (function () {
1165
698
  workerUsed: true
1166
699
  }];
1167
700
  case 3:
1168
- error_3 = _a.sent();
701
+ error_1 = _a.sent();
1169
702
  if (this._enableDebug) {
1170
- console.log(new Date(), 'Sub Manager', 'Worker publication failed, falling back', sub.publication, (error_3 === null || error_3 === void 0 ? void 0 : error_3.message) || error_3);
703
+ console.log(new Date(), 'Sub Manager', 'Worker publication failed, falling back', sub.publication, (error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || error_1);
1171
704
  }
1172
705
  return [2 /*return*/, null];
1173
706
  case 4: return [2 /*return*/];
@@ -1339,7 +872,7 @@ var SubscriptionManager = /** @class */ (function () {
1339
872
  // Method to flush buffered latency updates in bulk
1340
873
  SubscriptionManager.prototype.flushThrottledLatencyUpdates = function () {
1341
874
  return __awaiter(this, void 0, void 0, function () {
1342
- var pendingEntries, updates, error_4, pendingEntries_1, pendingEntries_1_1, _a, id_ws, payload, current;
875
+ var pendingEntries, updates, error_2, pendingEntries_1, pendingEntries_1_1, _a, id_ws, payload, current;
1343
876
  var e_3, _b;
1344
877
  var _this = this;
1345
878
  return __generator(this, function (_c) {
@@ -1374,7 +907,7 @@ var SubscriptionManager = /** @class */ (function () {
1374
907
  }
1375
908
  return [3 /*break*/, 5];
1376
909
  case 3:
1377
- error_4 = _c.sent();
910
+ error_2 = _c.sent();
1378
911
  try {
1379
912
  for (pendingEntries_1 = __values(pendingEntries), pendingEntries_1_1 = pendingEntries_1.next(); !pendingEntries_1_1.done; pendingEntries_1_1 = pendingEntries_1.next()) {
1380
913
  _a = __read(pendingEntries_1_1.value, 2), id_ws = _a[0], payload = _a[1];
@@ -1391,7 +924,7 @@ var SubscriptionManager = /** @class */ (function () {
1391
924
  }
1392
925
  finally { if (e_3) throw e_3.error; }
1393
926
  }
1394
- console.error(new Date(), 'Sub Manager', 'Throttled latency batch update failed', error_4);
927
+ console.error(new Date(), 'Sub Manager', 'Throttled latency batch update failed', error_2);
1395
928
  return [3 /*break*/, 5];
1396
929
  case 4:
1397
930
  this._latencyFlushInProgress = false;
@@ -1408,7 +941,7 @@ var SubscriptionManager = /** @class */ (function () {
1408
941
  // Subscribe to publication
1409
942
  SubscriptionManager.prototype.subscribe = function (messageRoute, messageDate, ws, messageId, publication, subscriptionData) {
1410
943
  return __awaiter(this, void 0, void 0, function () {
1411
- var pub, valObj, valKeys, rootKeys, i, staleRouteMessage, urlData, urlModule_1, urlNext, otherRouteSubs, normalizedSubscriptionData, subscriptionKey_1, sub, queued;
944
+ var pub, valObj, valKeys, rootKeys, i, staleRouteMessage, urlData, urlModule_1, urlNext, otherRouteSubs, normalizedSubscriptionData, subscriptionKey_1, sub;
1412
945
  var _this = this;
1413
946
  return __generator(this, function (_a) {
1414
947
  switch (_a.label) {
@@ -1521,19 +1054,6 @@ var SubscriptionManager = /** @class */ (function () {
1521
1054
  if (this._enableDebug) {
1522
1055
  console.log(new Date(), 'New Sub', sub.publication, sub.running, sub.runAgain, sub.clients.length);
1523
1056
  }
1524
- if (this.shouldQueueWarmup(publication, messageRoute)) {
1525
- queued = this.queueWarmupSubscription({
1526
- ws: ws,
1527
- messageRoute: messageRoute,
1528
- messageDate: messageDate,
1529
- messageId: messageId,
1530
- publication: publication,
1531
- subscriptionData: normalizedSubscriptionData
1532
- });
1533
- if (queued) {
1534
- return [2 /*return*/];
1535
- }
1536
- }
1537
1057
  // Immediately send data to the new client
1538
1058
  return [4 /*yield*/, this.processSubscription(sub, ws, messageId)];
1539
1059
  case 2:
@@ -1817,7 +1337,7 @@ var SubscriptionManager = /** @class */ (function () {
1817
1337
  };
1818
1338
  SubscriptionManager.prototype.loadResumeToken = function () {
1819
1339
  return __awaiter(this, void 0, void 0, function () {
1820
- var db, doc, error_5;
1340
+ var db, doc, error_3;
1821
1341
  return __generator(this, function (_a) {
1822
1342
  switch (_a.label) {
1823
1343
  case 0:
@@ -1831,8 +1351,8 @@ var SubscriptionManager = /** @class */ (function () {
1831
1351
  doc = _a.sent();
1832
1352
  return [2 /*return*/, (doc === null || doc === void 0 ? void 0 : doc.token) || null];
1833
1353
  case 2:
1834
- error_5 = _a.sent();
1835
- console.log(new Date(), 'Sub Manager', 'Failed to load oplog resume token', error_5);
1354
+ error_3 = _a.sent();
1355
+ console.log(new Date(), 'Sub Manager', 'Failed to load oplog resume token', error_3);
1836
1356
  return [2 /*return*/, null];
1837
1357
  case 3: return [2 /*return*/];
1838
1358
  }
@@ -1841,7 +1361,7 @@ var SubscriptionManager = /** @class */ (function () {
1841
1361
  };
1842
1362
  SubscriptionManager.prototype.saveResumeToken = function (token) {
1843
1363
  return __awaiter(this, void 0, void 0, function () {
1844
- var db, error_6;
1364
+ var db, error_4;
1845
1365
  return __generator(this, function (_a) {
1846
1366
  switch (_a.label) {
1847
1367
  case 0:
@@ -1860,8 +1380,8 @@ var SubscriptionManager = /** @class */ (function () {
1860
1380
  _a.sent();
1861
1381
  return [3 /*break*/, 4];
1862
1382
  case 3:
1863
- error_6 = _a.sent();
1864
- console.log(new Date(), 'Sub Manager', 'Failed to persist oplog resume token', error_6);
1383
+ error_4 = _a.sent();
1384
+ console.log(new Date(), 'Sub Manager', 'Failed to persist oplog resume token', error_4);
1865
1385
  return [3 /*break*/, 4];
1866
1386
  case 4: return [2 /*return*/];
1867
1387
  }
@@ -1870,7 +1390,7 @@ var SubscriptionManager = /** @class */ (function () {
1870
1390
  };
1871
1391
  SubscriptionManager.prototype.clearResumeToken = function () {
1872
1392
  return __awaiter(this, void 0, void 0, function () {
1873
- var db, error_7;
1393
+ var db, error_5;
1874
1394
  return __generator(this, function (_a) {
1875
1395
  switch (_a.label) {
1876
1396
  case 0:
@@ -1884,8 +1404,8 @@ var SubscriptionManager = /** @class */ (function () {
1884
1404
  _a.sent();
1885
1405
  return [3 /*break*/, 3];
1886
1406
  case 2:
1887
- error_7 = _a.sent();
1888
- console.log(new Date(), 'Sub Manager', 'Failed to clear oplog resume token', error_7);
1407
+ error_5 = _a.sent();
1408
+ console.log(new Date(), 'Sub Manager', 'Failed to clear oplog resume token', error_5);
1889
1409
  return [3 /*break*/, 3];
1890
1410
  case 3: return [2 /*return*/];
1891
1411
  }
@@ -1956,7 +1476,7 @@ var SubscriptionManager = /** @class */ (function () {
1956
1476
  };
1957
1477
  SubscriptionManager.prototype.fullResyncSubscriptions = function (reason) {
1958
1478
  return __awaiter(this, void 0, void 0, function () {
1959
- var subs, subs_1, subs_1_1, sub, pub, _a, _b, client, ws, _c, e_4_1, e_5_1, error_8;
1479
+ var subs, subs_1, subs_1_1, sub, pub, _a, _b, client, ws, _c, e_4_1, e_5_1, error_6;
1960
1480
  var e_5, _d, e_4, _e;
1961
1481
  var _f, _g, _h, _j, _k;
1962
1482
  return __generator(this, function (_l) {
@@ -2054,8 +1574,8 @@ var SubscriptionManager = /** @class */ (function () {
2054
1574
  return [7 /*endfinally*/];
2055
1575
  case 19: return [3 /*break*/, 21];
2056
1576
  case 20:
2057
- error_8 = _l.sent();
2058
- console.log(new Date(), 'Sub Manager', 'Full resync failed', reason, error_8);
1577
+ error_6 = _l.sent();
1578
+ console.log(new Date(), 'Sub Manager', 'Full resync failed', reason, error_6);
2059
1579
  return [3 /*break*/, 21];
2060
1580
  case 21: return [2 /*return*/];
2061
1581
  }
@@ -2065,7 +1585,7 @@ var SubscriptionManager = /** @class */ (function () {
2065
1585
  // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
2066
1586
  SubscriptionManager.prototype.tailOpLog = function (resumeToken) {
2067
1587
  return __awaiter(this, void 0, void 0, function () {
2068
- var watchDatabases, pipeline, lastResumeToken_1, startedWithResumeToken, error_9, innerError_1, error_10;
1588
+ var watchDatabases, pipeline, lastResumeToken_1, startedWithResumeToken, error_7, innerError_1, error_8;
2069
1589
  var _this = this;
2070
1590
  return __generator(this, function (_a) {
2071
1591
  switch (_a.label) {
@@ -2126,9 +1646,9 @@ var SubscriptionManager = /** @class */ (function () {
2126
1646
  startedWithResumeToken = true;
2127
1647
  return [3 /*break*/, 16];
2128
1648
  case 5:
2129
- error_9 = _a.sent();
2130
- if (!this.isChangeStreamUnsupported(error_9)) return [3 /*break*/, 7];
2131
- return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_9)];
1649
+ error_7 = _a.sent();
1650
+ if (!this.isChangeStreamUnsupported(error_7)) return [3 /*break*/, 7];
1651
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_7)];
2132
1652
  case 6:
2133
1653
  _a.sent();
2134
1654
  return [2 /*return*/];
@@ -2144,7 +1664,7 @@ var SubscriptionManager = /** @class */ (function () {
2144
1664
  case 10:
2145
1665
  _a.sent();
2146
1666
  lastResumeToken_1 = null;
2147
- console.log(new Date(), 'oplog resumeAfter failed, starting fresh', error_9);
1667
+ console.log(new Date(), 'oplog resumeAfter failed, starting fresh', error_7);
2148
1668
  _a.label = 11;
2149
1669
  case 11:
2150
1670
  _a.trys.push([11, 12, , 15]);
@@ -2168,13 +1688,13 @@ var SubscriptionManager = /** @class */ (function () {
2168
1688
  this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { fullDocument: 'updateLookup' });
2169
1689
  return [3 /*break*/, 21];
2170
1690
  case 18:
2171
- error_10 = _a.sent();
2172
- if (!this.isChangeStreamUnsupported(error_10)) return [3 /*break*/, 20];
2173
- return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_10)];
1691
+ error_8 = _a.sent();
1692
+ if (!this.isChangeStreamUnsupported(error_8)) return [3 /*break*/, 20];
1693
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_8)];
2174
1694
  case 19:
2175
1695
  _a.sent();
2176
1696
  return [2 /*return*/];
2177
- case 20: throw error_10;
1697
+ case 20: throw error_8;
2178
1698
  case 21:
2179
1699
  console.log(new Date(), 'oplog started', startedWithResumeToken ? '(resumeAfter)' : '');
2180
1700
  this._oplog$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {