diff options
| author | dakkar <dakkar@thenautilus.net> | 2024-09-20 08:35:45 +0100 |
|---|---|---|
| committer | dakkar <dakkar@thenautilus.net> | 2024-09-20 08:35:45 +0100 |
| commit | 3f6beb97d2923abea3146290242423347629528d (patch) | |
| tree | 5ad95e2ec9b1847edc85533dd6de2784333e425d /packages/backend/src/server/api/RateLimiterService.ts | |
| parent | bump `happy-dom` (diff) | |
| download | sharkey-3f6beb97d2923abea3146290242423347629528d.tar.gz sharkey-3f6beb97d2923abea3146290242423347629528d.tar.bz2 sharkey-3f6beb97d2923abea3146290242423347629528d.zip | |
copy RateLimiterService from MisskeyIO
This implementation allocates fewer Promises, might help with the
memory leaks
Diffstat (limited to 'packages/backend/src/server/api/RateLimiterService.ts')
| -rw-r--r-- | packages/backend/src/server/api/RateLimiterService.ts | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/packages/backend/src/server/api/RateLimiterService.ts b/packages/backend/src/server/api/RateLimiterService.ts index e94160a657..e9afb9d05a 100644 --- a/packages/backend/src/server/api/RateLimiterService.ts +++ b/packages/backend/src/server/api/RateLimiterService.ts @@ -32,18 +32,11 @@ export class RateLimiterService { @bindThis public limit(limitation: IEndpointMeta['limit'] & { key: NonNullable<string> }, actor: string, factor = 1) { - { - if (this.disabled) { - return Promise.resolve(); - } - - // those lines with the "wrong" brace style / indentation are - // done that way so that the *other* lines stay identical to - // Misskey, simplifying merges + return new Promise<void>((ok, reject) => { + if (this.disabled) ok(); // Short-term limit - // eslint-disable-next-line brace-style - const minP = () => { return new Promise<void>((ok, reject) => { + const minP = (): void => { const minIntervalLimiter = new Limiter({ id: `${actor}:${limitation.key}:min`, duration: limitation.minInterval! * factor, @@ -62,18 +55,16 @@ export class RateLimiterService { return reject({ code: 'BRIEF_REQUEST_INTERVAL', info }); } else { if (hasLongTermLimit) { - return maxP().then(ok, reject); + return maxP(); } else { return ok(); } } }); - // eslint-disable-next-line brace-style - }); }; + }; // Long term limit - // eslint-disable-next-line brace-style - const maxP = () => { return new Promise<void>((ok, reject) => { + const maxP = (): void => { const limiter = new Limiter({ id: `${actor}:${limitation.key}`, duration: limitation.duration! * factor, @@ -94,8 +85,7 @@ export class RateLimiterService { return ok(); } }); - // eslint-disable-next-line brace-style - }); }; + }; const hasShortTermLimit = typeof limitation.minInterval === 'number'; @@ -104,12 +94,12 @@ export class RateLimiterService { typeof limitation.max === 'number'; if (hasShortTermLimit) { - return minP(); + minP(); } else if (hasLongTermLimit) { - return maxP(); + maxP(); } else { - return Promise.resolve(); + ok(); } - } + }); } } |