Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RateLimitController aggressive timeout issue #716

Merged
merged 1 commit into from
Mar 10, 2022
Merged
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
24 changes: 24 additions & 0 deletions src/ratelimit/RateLimitController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,28 @@ describe('RateLimitController', () => {
message,
);
});

it('timeout is only applied once per window', async () => {
const messenger = getRestrictedMessenger();
const controller = new RateLimitController({
implementations,
messenger,
rateLimitCount: 2,
});
expect(
await controller.call(origin, 'showNativeNotification', origin, message),
).toBeUndefined();
jest.advanceTimersByTime(2500);
expect(
await controller.call(origin, 'showNativeNotification', origin, message),
).toBeUndefined();
expect(controller.state.requests.showNativeNotification[origin]).toBe(2);
jest.advanceTimersByTime(2500);
expect(controller.state.requests.showNativeNotification[origin]).toBe(0);
expect(
await controller.call(origin, 'showNativeNotification', origin, message),
).toBeUndefined();
jest.advanceTimersByTime(2500);
expect(controller.state.requests.showNativeNotification[origin]).toBe(1);
});
});
16 changes: 9 additions & 7 deletions src/ratelimit/RateLimitController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ export class RateLimitController<
*/
private recordRequest(api: keyof RateLimitedApis, origin: string) {
this.update((state) => {
(state as any).requests[api][origin] =
((state as any).requests[api][origin] ?? 0) + 1;

setTimeout(
() => this.resetRequestCount(api, origin),
this.rateLimitTimeout,
);
const previous = (state as any).requests[api][origin] ?? 0;
(state as any).requests[api][origin] = previous + 1;

if (previous === 0) {
setTimeout(
() => this.resetRequestCount(api, origin),
this.rateLimitTimeout,
);
}
Comment on lines +172 to +180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intended behavior is that the first request initiates a timeout, during which the requesting origin can make no more than rateLimitCount number of requests, then my late-night brain assesses that this is correct.

Previously, every request would initiate a new timeout, regardless of whether the previous one had expired, and that's definitely not correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly the conclusion that I came to as well, your described behaviour was the intention, but it may been altered throughout the implementation process without a good test to remind me. That should be corrected with this PR. Feel free to wait for tomorrow to do an actual review 😄

});
}

Expand Down