mirror of
https://activitypub.software/TransFem-org/Sharkey
synced 2024-12-19 23:10:10 +00:00
fix NaN from extremely high rate limits
This commit is contained in:
parent
2781f53d6b
commit
a7a1edc92e
2 changed files with 15 additions and 1 deletions
|
@ -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),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
Loading…
Reference in a new issue