fix NaN from extremely high rate limits

This commit is contained in:
Hazelnoot 2024-12-08 09:22:38 -05:00
parent 2781f53d6b
commit a7a1edc92e
2 changed files with 15 additions and 1 deletions

View file

@ -155,7 +155,7 @@ export class SkRateLimiterService {
type: 'bucket',
key: limit.key,
size: limit.max,
dripRate: Math.round(limit.duration / limit.max),
dripRate: Math.max(Math.round(limit.duration / limit.max), 1),
}, actor, factor),
);
}

View file

@ -564,6 +564,20 @@ describe(SkRateLimiterService, () => {
expect(counter?.c).toBe(1);
expect(counter?.t).toBe(0);
});
it('should not allow dripRate to be lower than 0', async () => {
// real-world case; taken from StreamingApiServerService
limit.max = 4096;
limit.duration = 2000;
counter = { c: 4096, t: 0 };
const i1 = await serviceUnderTest().limit(limit, actor);
mockTimeService.now = 1;
const i2 = await serviceUnderTest().limit(limit, actor);
expect(i1.blocked).toBeTruthy();
expect(i2.blocked).toBeFalsy();
});
});
describe('with legacy limit and min interval', () => {