Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Refactor redis key names

Hakan Shehu53ef5d6dd639
diff --git a/apps/server/src/lib/configuration.ts b/apps/server/src/lib/configuration.ts
index ab698edb..7e27f08a 100644
--- a/apps/server/src/lib/configuration.ts
+++ b/apps/server/src/lib/configuration.ts
@@ -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: {
diff --git a/apps/server/src/services/account-service.ts b/apps/server/src/services/account-service.ts
index 28e7c8e5..eec62cb5 100644
--- a/apps/server/src/services/account-service.ts
+++ b/apps/server/src/services/account-service.ts
@@ -220,7 +220,7 @@ class AccountService {
}
private getOtpDataRedisKey(otpId: string): string {
- return `otp_${otpId}`;
+ return `otp:${otpId}`;
}
private async generateOtpCode(): Promise<string> {
diff --git a/apps/server/src/services/job-service.ts b/apps/server/src/services/job-service.ts
index dc40e207..0e3a410f 100644
--- a/apps/server/src/services/job-service.ts
+++ b/apps/server/src/services/job-service.ts
@@ -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,
diff --git a/apps/server/src/services/rate-limit-service.ts b/apps/server/src/services/rate-limit-service.ts
index 38dcaa05..a25b3160 100644
--- a/apps/server/src/services/rate-limit-service.ts
+++ b/apps/server/src/services/rate-limit-service.ts
@@ -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
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 475b5408..a4e10a44 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -98,6 +98,7 @@ services:
REDIS_DB: '0'
# Optional variables:
REDIS_JOBS_QUEUE_NAME: 'jobs'
+ REDIS_JOBS_QUEUE_PREFIX: 'colanode'
REDIS_EVENTS_CHANNEL: 'events'
# ───────────────────────────────────────────────────────────────