Skip to content

Commit 727710d

Browse files
committed
fix(discord): avoid stale rate limit requeues
1 parent 1297c94 commit 727710d

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎extensions/discord/src/internal/rest-scheduler.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type ScheduledRequest<TData> = {
66
method: string;
77
path: string;
88
data?: TData;
9+
generation: number;
910
query?: RequestQuery;
1011
routeKey: string;
1112
retryCount: number;
@@ -39,6 +40,7 @@ export class RestScheduler<TData> {
3940
private drainTimer: NodeJS.Timeout | undefined;
4041
private globalRateLimitUntil = 0;
4142
private invalidRequestTimestamps: Array<{ at: number; status: number }> = [];
43+
private queueGeneration = 0;
4244
private queuedRequests = 0;
4345
private routeBuckets = new Map<string, string>();
4446

@@ -60,7 +62,14 @@ export class RestScheduler<TData> {
6062
const bucket = this.getBucket(this.routeBuckets.get(routeKey) ?? routeKey);
6163
return new Promise((resolve, reject) => {
6264
this.queuedRequests += 1;
63-
bucket.pending.push({ ...params, routeKey, retryCount: 0, resolve, reject });
65+
bucket.pending.push({
66+
...params,
67+
generation: this.queueGeneration,
68+
routeKey,
69+
retryCount: 0,
70+
resolve,
71+
reject,
72+
});
6473
this.drainQueues();
6574
});
6675
}
@@ -71,6 +80,7 @@ export class RestScheduler<TData> {
7180
}
7281

7382
clearQueue(): void {
83+
this.queueGeneration += 1;
7484
if (this.drainTimer) {
7585
clearTimeout(this.drainTimer);
7686
this.drainTimer = undefined;
@@ -79,6 +89,7 @@ export class RestScheduler<TData> {
7989
}
8090

8191
abortPending(): void {
92+
this.queueGeneration += 1;
8293
this.rejectPending(new DOMException("Aborted", "AbortError"));
8394
}
8495

@@ -370,7 +381,10 @@ export class RestScheduler<TData> {
370381
}
371382

372383
private requeueRateLimitedRequest(queued: ScheduledRequest<TData>): boolean {
373-
if (queued.retryCount >= this.maxRateLimitRetries) {
384+
if (
385+
queued.generation !== this.queueGeneration ||
386+
queued.retryCount >= this.maxRateLimitRetries
387+
) {
374388
return false;
375389
}
376390
const bucketKey = this.routeBuckets.get(queued.routeKey) ?? queued.routeKey;

‎extensions/discord/src/internal/rest.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,41 @@ describe("RequestClient", () => {
230230
expect(client.queueSize).toBe(0);
231231
});
232232

233+
it("does not requeue an active rate limit after the queue is cleared", async () => {
234+
const response = createDeferred<Response>();
235+
const fetchSpy = vi.fn(async () => {
236+
if (fetchSpy.mock.calls.length > 1) {
237+
throw new Error("unexpected retry after clearQueue");
238+
}
239+
return await response.promise;
240+
});
241+
const client = new RequestClient("test-token", { fetch: fetchSpy });
242+
243+
const request = client.get("/channels/c1/messages");
244+
await vi.waitFor(() => expect(fetchSpy).toHaveBeenCalledTimes(1));
245+
expect(client.queueSize).toBe(1);
246+
247+
client.clearQueue();
248+
expect(client.queueSize).toBe(1);
249+
250+
response.resolve(
251+
createJsonResponse(
252+
{ message: "Rate limited", retry_after: 0, global: false },
253+
{
254+
status: 429,
255+
headers: { "X-RateLimit-Bucket": "channel-messages" },
256+
},
257+
),
258+
);
259+
260+
await expect(request).rejects.toMatchObject({
261+
name: "RateLimitError",
262+
retryAfter: 0,
263+
});
264+
expect(fetchSpy).toHaveBeenCalledTimes(1);
265+
expect(client.queueSize).toBe(0);
266+
});
267+
233268
it("retries queued global rate limits after Retry-After", async () => {
234269
vi.useFakeTimers();
235270
vi.setSystemTime(0);

0 commit comments

Comments
 (0)