mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2025-08-21 05:44:48 +00:00
scale rate limit dripRate with factor
This commit is contained in:
parent
51ad31b5a4
commit
9ac58e6107
3 changed files with 28 additions and 12 deletions
|
@ -81,7 +81,7 @@ The Atomic Leaky Bucket algorithm is described here, in pseudocode:
|
||||||
# * Delta Timestamp - Difference between current and expected timestamp value
|
# * Delta Timestamp - Difference between current and expected timestamp value
|
||||||
|
|
||||||
# 0 - Calculations
|
# 0 - Calculations
|
||||||
dripRate = ceil(limit.dripRate ?? 1000);
|
dripRate = ceil((limit.dripRate ?? 1000) * factor);
|
||||||
dripSize = ceil(limit.dripSize ?? 1);
|
dripSize = ceil(limit.dripSize ?? 1);
|
||||||
bucketSize = max(ceil(limit.size / factor), 1);
|
bucketSize = max(ceil(limit.size / factor), 1);
|
||||||
maxExpiration = max(ceil((dripRate * ceil(bucketSize / dripSize)) / 1000), 1);;
|
maxExpiration = max(ceil((dripRate * ceil(bucketSize / dripSize)) / 1000), 1);;
|
||||||
|
|
|
@ -206,7 +206,7 @@ export class SkRateLimiterService {
|
||||||
// 0 - Calculate
|
// 0 - Calculate
|
||||||
const now = this.timeService.now;
|
const now = this.timeService.now;
|
||||||
const bucketSize = Math.max(Math.ceil(limit.size / factor), 1);
|
const bucketSize = Math.max(Math.ceil(limit.size / factor), 1);
|
||||||
const dripRate = Math.ceil(limit.dripRate ?? 1000);
|
const dripRate = Math.ceil((limit.dripRate ?? 1000) * factor);
|
||||||
const dripSize = Math.ceil(limit.dripSize ?? 1);
|
const dripSize = Math.ceil(limit.dripSize ?? 1);
|
||||||
const fullResetMs = dripRate * Math.ceil(bucketSize / dripSize);
|
const fullResetMs = dripRate * Math.ceil(bucketSize / dripSize);
|
||||||
const fullResetSec = Math.max(Math.ceil(fullResetMs / 1000), 1);
|
const fullResetSec = Math.max(Math.ceil(fullResetMs / 1000), 1);
|
||||||
|
|
|
@ -303,9 +303,12 @@ describe(SkRateLimiterService, () => {
|
||||||
|
|
||||||
const i1 = await serviceUnderTest().limit(limit, actor); // 1 + 1 = 2
|
const i1 = await serviceUnderTest().limit(limit, actor); // 1 + 1 = 2
|
||||||
const i2 = await serviceUnderTest().limit(limit, actor); // 2 + 1 = 3
|
const i2 = await serviceUnderTest().limit(limit, actor); // 2 + 1 = 3
|
||||||
|
mockTimeService.now += 500; // 3 - 1 = 2 (at 1/2 time)
|
||||||
|
const i3 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
|
||||||
expect(i1.blocked).toBeFalsy();
|
expect(i1.blocked).toBeFalsy();
|
||||||
expect(i2.blocked).toBeTruthy();
|
expect(i2.blocked).toBeTruthy();
|
||||||
|
expect(i3.blocked).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set counter expiration', async () => {
|
it('should set counter expiration', async () => {
|
||||||
|
@ -563,11 +566,15 @@ describe(SkRateLimiterService, () => {
|
||||||
mockDefaultUserPolicies.rateLimitFactor = 0.5;
|
mockDefaultUserPolicies.rateLimitFactor = 0.5;
|
||||||
limitCounter = 1;
|
limitCounter = 1;
|
||||||
limitTimestamp = 0;
|
limitTimestamp = 0;
|
||||||
|
|
||||||
|
const i1 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
const i2 = await serviceUnderTest().limit(limit, actor);
|
||||||
mockTimeService.now += 500;
|
mockTimeService.now += 500;
|
||||||
|
const i3 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
|
||||||
const info = await serviceUnderTest().limit(limit, actor);
|
expect(i1.blocked).toBeFalsy();
|
||||||
|
expect(i2.blocked).toBeTruthy();
|
||||||
expect(info.blocked).toBeFalsy();
|
expect(i3.blocked).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set counter expiration', async () => {
|
it('should set counter expiration', async () => {
|
||||||
|
@ -738,12 +745,17 @@ describe(SkRateLimiterService, () => {
|
||||||
|
|
||||||
it('should scale limit by factor', async () => {
|
it('should scale limit by factor', async () => {
|
||||||
mockDefaultUserPolicies.rateLimitFactor = 0.5;
|
mockDefaultUserPolicies.rateLimitFactor = 0.5;
|
||||||
limitCounter = 10;
|
limitCounter = 1;
|
||||||
limitTimestamp = 0;
|
limitTimestamp = 0;
|
||||||
|
|
||||||
const info = await serviceUnderTest().limit(limit, actor); // 10 + 1 = 11
|
const i1 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
const i2 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
mockTimeService.now += 500;
|
||||||
|
const i3 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
|
||||||
expect(info.blocked).toBeTruthy();
|
expect(i1.blocked).toBeFalsy();
|
||||||
|
expect(i2.blocked).toBeTruthy();
|
||||||
|
expect(i3.blocked).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set counter expiration', async () => {
|
it('should set counter expiration', async () => {
|
||||||
|
@ -932,13 +944,17 @@ describe(SkRateLimiterService, () => {
|
||||||
|
|
||||||
it('should scale limit and interval by factor', async () => {
|
it('should scale limit and interval by factor', async () => {
|
||||||
mockDefaultUserPolicies.rateLimitFactor = 0.5;
|
mockDefaultUserPolicies.rateLimitFactor = 0.5;
|
||||||
limitCounter = 5;
|
limitCounter = 19;
|
||||||
limitTimestamp = 0;
|
limitTimestamp = 0;
|
||||||
|
|
||||||
|
const i1 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
const i2 = await serviceUnderTest().limit(limit, actor);
|
||||||
mockTimeService.now += 500;
|
mockTimeService.now += 500;
|
||||||
|
const i3 = await serviceUnderTest().limit(limit, actor);
|
||||||
|
|
||||||
const info = await serviceUnderTest().limit(limit, actor);
|
expect(i1.blocked).toBeFalsy();
|
||||||
|
expect(i2.blocked).toBeTruthy();
|
||||||
expect(info.blocked).toBeFalsy();
|
expect(i3.blocked).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set counter expiration', async () => {
|
it('should set counter expiration', async () => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue