From 63c6a30ef70892e72f0e6b3c10a2632fa30fadb8 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Wed, 22 Jan 2025 19:37:00 +0700 Subject: [PATCH] Remove redundant map (#7763) Signed-off-by: Andrey Sobolev --- packages/core/src/__tests__/limits.test.ts | 8 ++++---- packages/core/src/utils.ts | 14 ++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/core/src/__tests__/limits.test.ts b/packages/core/src/__tests__/limits.test.ts index 5a7c15d63e..2fc323d9f2 100644 --- a/packages/core/src/__tests__/limits.test.ts +++ b/packages/core/src/__tests__/limits.test.ts @@ -72,7 +72,7 @@ describe('TimeRateLimiter', () => { const operations = Promise.all([limiter.exec(mockFn), limiter.exec(mockFn), limiter.exec(mockFn)]) expect(mockFn).toHaveBeenCalledTimes(2) - expect(limiter.processingQueue.size).toBe(2) + expect(limiter.active).toBe(2) jest.advanceTimersByTime(500) await Promise.resolve() @@ -84,7 +84,7 @@ describe('TimeRateLimiter', () => { await Promise.resolve() await Promise.resolve() - expect(limiter.processingQueue.size).toBe(0) + expect(limiter.active).toBe(0) expect(mockFn).toHaveBeenCalledTimes(3) @@ -104,7 +104,7 @@ describe('TimeRateLimiter', () => { console.log('wait complete') }) - expect(limiter.processingQueue.size).toBe(1) + expect(limiter.active).toBe(1) jest.advanceTimersByTime(1001) await Promise.resolve() @@ -113,6 +113,6 @@ describe('TimeRateLimiter', () => { await waitPromise await operation - expect(limiter.processingQueue.size).toBe(0) + expect(limiter.active).toBe(0) }) }) diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 45e5a8b943..022aefd8fb 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -846,7 +846,7 @@ export function pluginFilterTx ( export class TimeRateLimiter { idCounter: number = 0 - processingQueue = new Map>() + active: number = 0 last: number = 0 rate: number period: number @@ -866,9 +866,7 @@ export class TimeRateLimiter { } async exec = any>(op: (args?: B) => Promise, args?: B): Promise { - const processingId = this.idCounter++ - - while (this.processingQueue.size >= this.rate || this.executions.length >= this.rate) { + while (this.active >= this.rate || this.executions.length >= this.rate) { this.cleanupExecutions() if (this.executions.length < this.rate) { break @@ -882,11 +880,11 @@ export class TimeRateLimiter { try { this.executions.push(v) const p = op(args) - this.processingQueue.set(processingId, p as Promise) + this.active++ return await p } finally { v.running = false - this.processingQueue.delete(processingId) + this.active-- this.cleanupExecutions() const n = this.notify.shift() if (n !== undefined) { @@ -896,8 +894,8 @@ export class TimeRateLimiter { } async waitProcessing (): Promise { - while (this.processingQueue.size > 0) { - console.log('wait', this.processingQueue.size) + while (this.active > 0) { + console.log('wait', this.active) await new Promise((resolve) => { this.notify.push(resolve) })