Refactor redis key names

This commit is contained in:
Hakan Shehu
2025-01-13 11:17:32 +01:00
parent 6a4f14f8b8
commit 53ef5d6dd6
5 changed files with 20 additions and 10 deletions

View File

@@ -40,8 +40,11 @@ export interface PostgresConfiguration {
export interface RedisConfiguration {
url: string;
db: number;
jobs: {
prefix: string;
name: string;
};
eventsChannel: string;
jobsQueueName: string;
}
export interface S3Configuration {
@@ -130,7 +133,10 @@ export const configuration: Configuration = {
redis: {
url: getRequiredEnv('REDIS_URL'),
db: parseInt(getOptionalEnv('REDIS_DB') || '0'),
jobsQueueName: getOptionalEnv('REDIS_JOBS_QUEUE_NAME') || 'jobs',
jobs: {
name: getOptionalEnv('REDIS_JOBS_QUEUE_NAME') || 'jobs',
prefix: getOptionalEnv('REDIS_JOBS_QUEUE_PREFIX') || 'colanode',
},
eventsChannel: getOptionalEnv('REDIS_EVENTS_CHANNEL') || 'events',
},
avatarS3: {

View File

@@ -220,7 +220,7 @@ class AccountService {
}
private getOtpDataRedisKey(otpId: string): string {
return `otp_${otpId}`;
return `otp:${otpId}`;
}
private async generateOtpCode(): Promise<string> {

View File

@@ -12,12 +12,13 @@ class JobService {
private jobWorker: Worker | undefined;
// Bullmq performs atomic operations across different keys, which can cause
// issues with Redis clusters, so we wrap the queue name in curly braces to
// issues with Redis clusters, so we wrap the prefix in curly braces to
// ensure that all keys are in the same slot (Redis node)
// for more information, see: https://docs.bullmq.io/bull/patterns/redis-cluster
private readonly queueName = `{${configuration.redis.jobsQueueName}}`;
private readonly queueName = configuration.redis.jobs.name;
private readonly prefix = `{${configuration.redis.jobs.prefix}}`;
public initQueue() {
if (this.jobQueue) {
@@ -25,6 +26,7 @@ class JobService {
}
this.jobQueue = new Queue(this.queueName, {
prefix: this.prefix,
connection: {
db: configuration.redis.db,
url: configuration.redis.url,
@@ -45,6 +47,7 @@ class JobService {
}
this.jobWorker = new Worker(this.queueName, this.handleJobJob, {
prefix: this.prefix,
connection: {
url: configuration.redis.url,
db: configuration.redis.db,

View File

@@ -14,7 +14,7 @@ class RateLimitService {
};
public async isAuthIpRateLimitted(ip: string): Promise<boolean> {
return await this.isRateLimited(`ai_${ip}`, {
return await this.isRateLimited(`ai:${ip}`, {
limit: 100,
window: 600, // 10 minutes
});
@@ -22,21 +22,21 @@ class RateLimitService {
public async isAuthEmailRateLimitted(email: string): Promise<boolean> {
const emailHash = sha256(email);
return await this.isRateLimited(`ae_${emailHash}`, {
return await this.isRateLimited(`ae:${emailHash}`, {
limit: 10,
window: 600, // 10 minutes
});
}
public async isDeviceApiRateLimitted(deviceId: string): Promise<boolean> {
return await this.isRateLimited(`da_${deviceId}`, {
return await this.isRateLimited(`da:${deviceId}`, {
limit: 100,
window: 60, // 1 minute
});
}
public async isDeviceSocketRateLimitted(deviceId: string): Promise<boolean> {
return await this.isRateLimited(`ds_${deviceId}`, {
return await this.isRateLimited(`ds:${deviceId}`, {
limit: 20,
window: 60, // 1 minute
});
@@ -46,7 +46,7 @@ class RateLimitService {
key: string,
config: RateLimitConfig = this.defaultConfig
): Promise<boolean> {
const redisKey = `rt_${key}`;
const redisKey = `rt:${key}`;
const attempts = await redis.incr(redisKey);
// Set expiry on first attempt