summaryrefslogtreecommitdiff
path: root/packages/backend/src/server
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2024-12-07 12:15:38 -0500
committerHazelnoot <acomputerdog@gmail.com>2024-12-07 12:15:38 -0500
commit32635ecc25d328314988db3c3f91e3d1c7d9d6c2 (patch)
treee03976cd66a2a978c88db76ee9f8843ec0a35e02 /packages/backend/src/server
parentfix incorrect X-RateLimit-Remaining header (diff)
downloadsharkey-32635ecc25d328314988db3c3f91e3d1c7d9d6c2.tar.gz
sharkey-32635ecc25d328314988db3c3f91e3d1c7d9d6c2.tar.bz2
sharkey-32635ecc25d328314988db3c3f91e3d1c7d9d6c2.zip
fix rate limit storage in redis
Diffstat (limited to 'packages/backend/src/server')
-rw-r--r--packages/backend/src/server/api/SkRateLimiterService.ts21
1 files changed, 9 insertions, 12 deletions
diff --git a/packages/backend/src/server/api/SkRateLimiterService.ts b/packages/backend/src/server/api/SkRateLimiterService.ts
index 3e4b125e79..763de0029b 100644
--- a/packages/backend/src/server/api/SkRateLimiterService.ts
+++ b/packages/backend/src/server/api/SkRateLimiterService.ts
@@ -209,7 +209,7 @@ export class SkRateLimiterService extends RateLimiterService {
// Update the limit counter, but not if blocked
if (!blocked) {
// Don't await, or we will slow down the API.
- this.setLimitCounter(limit, actor, counter, resetMs, 'min')
+ this.setLimitCounter(limit, actor, counter, fullResetSec, 'min')
.catch(err => this.logger.error(`Failed to update limit ${limit.key}:min for ${actor}:`, err));
}
@@ -217,7 +217,7 @@ export class SkRateLimiterService extends RateLimiterService {
}
private async limitBucket(limit: RateLimit, actor: string, factor: number): Promise<LimitInfo> {
- const counter = await this.getLimitCounter(limit, actor);
+ const counter = await this.getLimitCounter(limit, actor, 'bucket');
const dripRate = (limit.dripRate ?? 1000);
const dripSize = (limit.dripSize ?? 1);
const bucketSize = (limit.size * factor);
@@ -245,14 +245,14 @@ export class SkRateLimiterService extends RateLimiterService {
// Update the limit counter, but not if blocked
if (!blocked) {
// Don't await, or we will slow down the API.
- this.setLimitCounter(limit, actor, counter, fullResetMs)
+ this.setLimitCounter(limit, actor, counter, fullResetSec, 'bucket')
.catch(err => this.logger.error(`Failed to update limit ${limit.key} for ${actor}:`, err));
}
return limitInfo;
}
- private async getLimitCounter(limit: SupportedRateLimit, actor: string, subject?: string): Promise<LimitCounter> {
+ private async getLimitCounter(limit: SupportedRateLimit, actor: string, subject: string): Promise<LimitCounter> {
const key = createLimitKey(limit, actor, subject);
const value = await this.redisClient.get(key);
@@ -263,19 +263,16 @@ export class SkRateLimiterService extends RateLimiterService {
return JSON.parse(value);
}
- private async setLimitCounter(limit: SupportedRateLimit, actor: string, counter: LimitCounter, expirationMs: number, subject?: string): Promise<void> {
+ private async setLimitCounter(limit: SupportedRateLimit, actor: string, counter: LimitCounter, expiration: number, subject: string): Promise<void> {
const key = createLimitKey(limit, actor, subject);
const value = JSON.stringify(counter);
- await this.redisClient.set(key, value, 'PX', expirationMs);
+ const expirationSec = Math.max(expiration, 1);
+ await this.redisClient.set(key, value, 'EX', expirationSec);
}
}
-function createLimitKey(limit: SupportedRateLimit, actor: string, subject?: string): string {
- if (subject) {
- return `rl_${actor}_${limit.key}_${subject}`;
- } else {
- return `rl_${actor}_${limit.key}`;
- }
+function createLimitKey(limit: SupportedRateLimit, actor: string, subject: string): string {
+ return `rl_${actor}_${limit.key}_${subject}`;
}
export interface LimitCounter {