Skip to content

Commit

Permalink
fix(rest): sublimit all requests on unhandled routes (#7366)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Feb 5, 2022
1 parent a7b80b9 commit 733ac82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 17 additions & 4 deletions packages/rest/__tests__/RequestHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
];
})
.get('/unexpected')
.times(2)
.times(3)
.reply((): nock.ReplyFnResult => {
if (unexpected429) {
unexpected429 = false;
Expand Down Expand Up @@ -310,9 +310,22 @@ test('Handle sublimits', async () => {
});

test('Handle unexpected 429', async () => {
const previous = Date.now();
expect(await api.get('/unexpected')).toStrictEqual({ test: true });
expect(Date.now()).toBeGreaterThanOrEqual(previous + 1000);
const previous = performance.now();
let firstResolvedTime: number;
let secondResolvedTime: number;
const unexepectedSublimit = api.get('/unexpected').then((res) => {
firstResolvedTime = performance.now();
return res;
});
const queuedSublimit = api.get('/unexpected').then((res) => {
secondResolvedTime = performance.now();
return res;
});

expect(await unexepectedSublimit).toStrictEqual({ test: true });
expect(await queuedSublimit).toStrictEqual({ test: true });
expect(performance.now()).toBeGreaterThanOrEqual(previous + 1000);
expect(secondResolvedTime).toBeGreaterThan(firstResolvedTime);
});

test('Handle unexpected 429 cloudflare', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/rest/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export function hasSublimit(bucketRoute: string, body?: unknown, method?: string
return ['name', 'topic'].some((key) => Reflect.has(castedBody, key));
}

return false;
// If we are checking if a request has a sublimit on a route not checked above, sublimit all requests to avoid a flood of 429s
return true;
}

0 comments on commit 733ac82

Please sign in to comment.