@glimt/record 0.0.69 → 0.0.70

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.
package/dist/record.cjs CHANGED
@@ -10330,9 +10330,13 @@ const stormSnapshotManager = new StormSnapshotManager();
10330
10330
  const _MutationRateLimiter = class _MutationRateLimiter2 {
10331
10331
  constructor() {
10332
10332
  __publicField(this, "mutTracker");
10333
+ __publicField(this, "exitMutTracker");
10333
10334
  __publicField(this, "interval", 50);
10334
- __publicField(this, "limit", 100);
10335
+ __publicField(this, "exitInterval", 100);
10336
+ __publicField(this, "mutThreshold", 100);
10335
10337
  __publicField(this, "inGlobalStorm", false);
10338
+ __publicField(this, "currentStormStartedAt", -1);
10339
+ __publicField(this, "stormTimeLimit", 5e3);
10336
10340
  if (_MutationRateLimiter2.instance) {
10337
10341
  return _MutationRateLimiter2.instance;
10338
10342
  }
@@ -10340,24 +10344,75 @@ const _MutationRateLimiter = class _MutationRateLimiter2 {
10340
10344
  debugLog(`MutationRateLimiter, init`);
10341
10345
  this.reset();
10342
10346
  }
10343
- reset() {
10347
+ resetTracker() {
10344
10348
  this.mutTracker = {
10345
10349
  muts: 0,
10346
10350
  ts: -1
10347
10351
  };
10348
10352
  }
10353
+ resetExitTracker() {
10354
+ this.exitMutTracker = {
10355
+ muts: 0,
10356
+ requested: -1
10357
+ };
10358
+ }
10359
+ reset() {
10360
+ this.resetTracker();
10361
+ this.resetExitTracker();
10362
+ this.currentStormStartedAt = -1;
10363
+ }
10364
+ stormStopped() {
10365
+ this.inGlobalStorm = false;
10366
+ this.reset();
10367
+ }
10368
+ handleStormExit(muts) {
10369
+ const now = Date.now();
10370
+ if (this.exitMutTracker.requested === -1) {
10371
+ this.exitMutTracker = {
10372
+ requested: now,
10373
+ muts
10374
+ };
10375
+ } else {
10376
+ this.exitMutTracker.muts += muts;
10377
+ if (now - this.exitMutTracker.requested > this.exitInterval) {
10378
+ if (this.exitMutTracker.muts >= this.mutThreshold) {
10379
+ debugLog(
10380
+ `MutationRateLimiter, exit cooldown failed, continuing with storm`,
10381
+ {
10382
+ mutTracker: this.mutTracker,
10383
+ exitMutTracker: this.exitMutTracker
10384
+ }
10385
+ );
10386
+ this.mutTracker.ts = now;
10387
+ this.mutTracker.muts += this.exitMutTracker.muts;
10388
+ this.resetExitTracker();
10389
+ return true;
10390
+ } else {
10391
+ debugLog(`MutationRateLimiter, detected global storm exit.`, {
10392
+ mutTracker: this.mutTracker,
10393
+ exitMutTracker: this.exitMutTracker
10394
+ });
10395
+ this.stormStopped();
10396
+ return false;
10397
+ }
10398
+ }
10399
+ }
10400
+ return true;
10401
+ }
10349
10402
  isStorming(muts) {
10350
10403
  const now = Date.now();
10351
10404
  if (this.inGlobalStorm) {
10352
- this.mutTracker.muts += muts;
10353
- if (now - this.mutTracker.ts > this.interval) {
10354
- this.inGlobalStorm = false;
10405
+ if (now - this.currentStormStartedAt > this.stormTimeLimit) {
10355
10406
  debugLog(
10356
- `MutationRateLimiter, detected global storm over. Total mutations stormed: ${this.mutTracker.muts}`
10407
+ `MutationRateLimiter, storm time limit reached, stopping storm`
10357
10408
  );
10358
- this.reset();
10409
+ this.stormStopped();
10359
10410
  return false;
10360
10411
  }
10412
+ if (now - this.mutTracker.ts > this.interval) {
10413
+ return this.handleStormExit(muts);
10414
+ }
10415
+ this.mutTracker.muts += muts;
10361
10416
  this.mutTracker.ts = now;
10362
10417
  return true;
10363
10418
  }
@@ -10366,9 +10421,10 @@ const _MutationRateLimiter = class _MutationRateLimiter2 {
10366
10421
  } else {
10367
10422
  if (now - this.mutTracker.ts <= this.interval) {
10368
10423
  this.mutTracker.muts += muts;
10369
- if (this.mutTracker.muts >= this.limit) {
10424
+ if (this.mutTracker.muts >= this.mutThreshold) {
10370
10425
  this.inGlobalStorm = true;
10371
10426
  debugLog(`MutationRateLimiter, detected global rolling storm`);
10427
+ this.currentStormStartedAt = now;
10372
10428
  return true;
10373
10429
  }
10374
10430
  }