@glimt/record 0.0.65 → 0.0.66

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.
@@ -10369,6 +10369,49 @@ const _StormSnapshotManager = class _StormSnapshotManager2 {
10369
10369
  __publicField(_StormSnapshotManager, "instance");
10370
10370
  let StormSnapshotManager = _StormSnapshotManager;
10371
10371
  const stormSnapshotManager = new StormSnapshotManager();
10372
+ class MutationRateLimiter {
10373
+ constructor() {
10374
+ __publicField(this, "mutTracker");
10375
+ __publicField(this, "interval", 50);
10376
+ __publicField(this, "limit", 150);
10377
+ __publicField(this, "inGlobalStorm", false);
10378
+ debugLog(`MutationRateLimiter, init`);
10379
+ this.reset();
10380
+ }
10381
+ reset() {
10382
+ this.mutTracker = {
10383
+ muts: 0,
10384
+ ts: -1
10385
+ };
10386
+ }
10387
+ isStorming(muts) {
10388
+ const now = Date.now();
10389
+ if (this.inGlobalStorm) {
10390
+ if (now - this.mutTracker.ts > this.interval) {
10391
+ this.inGlobalStorm = false;
10392
+ debugLog(`MutationRateLimiter, detected global storm over`);
10393
+ return false;
10394
+ }
10395
+ return true;
10396
+ }
10397
+ if (this.mutTracker.ts === -1) {
10398
+ this.mutTracker.muts = muts;
10399
+ } else {
10400
+ if (now - this.mutTracker.ts <= this.interval) {
10401
+ this.mutTracker.muts += muts;
10402
+ if (this.mutTracker.muts >= this.limit) {
10403
+ this.inGlobalStorm = true;
10404
+ this.reset();
10405
+ debugLog(`MutationRateLimiter, detected global rolling storm`);
10406
+ return true;
10407
+ }
10408
+ }
10409
+ }
10410
+ this.mutTracker.ts = now;
10411
+ return false;
10412
+ }
10413
+ }
10414
+ const mutationRateLimiter = new MutationRateLimiter();
10372
10415
  function isNodeInLinkedList(n2) {
10373
10416
  return "__ln" in n2;
10374
10417
  }
@@ -10510,7 +10553,7 @@ class MutationBuffer {
10510
10553
  ts: -1,
10511
10554
  interval: 50
10512
10555
  });
10513
- __publicField(this, "handleStormMutations", (muts) => {
10556
+ __publicField(this, "handleStormMutations", (muts, canFinishStorm = true) => {
10514
10557
  const time = Date.now();
10515
10558
  if (this.stormInfo == null) {
10516
10559
  debugLog(
@@ -10536,7 +10579,7 @@ class MutationBuffer {
10536
10579
  });
10537
10580
  }
10538
10581
  clearTimeout(this.stormInfo.timeout);
10539
- if (muts.length < this.stormSettings.batchSize) {
10582
+ if (canFinishStorm && muts.length < this.stormSettings.batchSize) {
10540
10583
  this.handleStormFinish();
10541
10584
  } else {
10542
10585
  this.stormInfo.timeout = setTimeout(
@@ -10581,28 +10624,14 @@ class MutationBuffer {
10581
10624
  JSON.parse(JSON.stringify(this.rollingMutTracker))
10582
10625
  );
10583
10626
  if (!overrideStorm) {
10627
+ const isStorming = mutationRateLimiter.isStorming(muts.length);
10628
+ if (isStorming) {
10629
+ this.handleStormMutations(muts, false);
10630
+ return;
10631
+ }
10584
10632
  if (this.stormInfo != null || muts.length >= this.stormSettings.batchSize) {
10585
10633
  this.handleStormMutations(muts);
10586
10634
  }
10587
- if (this.stormInfo == null) {
10588
- const now = Date.now();
10589
- if (this.rollingMutTracker.ts === -1) {
10590
- this.rollingMutTracker.accumlativeMuts = muts.length;
10591
- } else {
10592
- if (now - this.rollingMutTracker.ts <= this.rollingMutTracker.interval) {
10593
- this.rollingMutTracker.accumlativeMuts += muts.length;
10594
- if (this.rollingMutTracker.accumlativeMuts >= this.stormSettings.batchSize) {
10595
- debugLog(`Mutation storm through rolling detected.`);
10596
- this.handleStormMutations(muts);
10597
- this.rollingMutTracker.accumlativeMuts = 0;
10598
- this.rollingMutTracker.ts = -1;
10599
- }
10600
- } else {
10601
- this.rollingMutTracker.accumlativeMuts = 0;
10602
- }
10603
- }
10604
- this.rollingMutTracker.ts = now;
10605
- }
10606
10635
  return;
10607
10636
  }
10608
10637
  for (const mut of muts) {