Remove redundant map (#7763)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2025-01-22 19:37:00 +07:00 committed by GitHub
parent 90e8ca4e97
commit 63c6a30ef7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 12 deletions

View File

@ -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)
})
})

View File

@ -846,7 +846,7 @@ export function pluginFilterTx (
export class TimeRateLimiter {
idCounter: number = 0
processingQueue = new Map<number, Promise<void>>()
active: number = 0
last: number = 0
rate: number
period: number
@ -866,9 +866,7 @@ export class TimeRateLimiter {
}
async exec<T, B extends Record<string, any> = any>(op: (args?: B) => Promise<T>, args?: B): Promise<T> {
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<void>)
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<void> {
while (this.processingQueue.size > 0) {
console.log('wait', this.processingQueue.size)
while (this.active > 0) {
console.log('wait', this.active)
await new Promise<void>((resolve) => {
this.notify.push(resolve)
})