Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions core/src/utils/throttler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ export class Throttler {
}

async throttle(cost: number = 1): Promise<void> {
return new Promise<void>((resolve) => {
return new Promise<void>((resolve, reject) => {
if (this.queue.length >= this.maxQueueDepth) {
const dropped = this.queue.shift();
if (dropped) {
dropped.resolve();
}
reject(new Error(`Throttler queue full (max depth ${this.maxQueueDepth})`));
return;
}
this.queue.push({ resolve, cost });
if (!this.running) {
Expand Down
24 changes: 24 additions & 0 deletions core/test/utils/throttler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Throttler } from '../../src/utils/throttler';

describe('Throttler', () => {
it('rejects new requests instead of silently resolving the oldest queued waiter', async () => {
const throttler = new Throttler({
refillRate: 1,
capacity: 1,
delay: 1,
maxQueueDepth: 1,
});

let oldestResolved = false;
(throttler as any).queue.push({
resolve: () => {
oldestResolved = true;
},
cost: 1,
});

await expect(throttler.throttle(1)).rejects.toThrow(/queue full/i);
expect(oldestResolved).toBe(false);
expect((throttler as any).queue).toHaveLength(1);
});
});
Loading