Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 95 additions & 3 deletions packages/redis/src/__tests__/index.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 74 additions & 4 deletions packages/redis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,59 @@ export async function syncAutoStartChannelCacheBestEffort(params: {
}

let redis: Redis | null = null;
let bullMqRedis: Redis | null = null;

const REDIS_MAX_RETRIES_PER_REQUEST = 3;
const REDIS_MAX_RECONNECT_DELAY_MS = 2_000;
const REDIS_ERROR_LOG_INTERVAL_MS = 30_000;

function observeRedisConnectivity(client: Redis): void {
let outageStartedAt: number | null = null;
let lastErrorLoggedAt = 0;
let errorsSinceLastLog = 0;
let totalErrors = 0;

client.on('error', (error: Error & { code?: string }) => {
const now = Date.now();
outageStartedAt ??= now;
errorsSinceLastLog += 1;
totalErrors += 1;

if (
totalErrors > 1 &&
now - lastErrorLoggedAt < REDIS_ERROR_LOG_INTERVAL_MS
) {
return;
}

console.error(
'[redis] connection degraded; dependent operations may fail',
{
code: error.code,
message: error.message,
suppressedErrors: Math.max(0, errorsSinceLastLog - 1),
},
);
lastErrorLoggedAt = now;
errorsSinceLastLog = 0;
});

client.on('ready', () => {
if (outageStartedAt === null) {
return;
}

console.info('[redis] connection restored', {
outageDurationMs: Date.now() - outageStartedAt,
totalErrors,
suppressedErrors: errorsSinceLastLog,
});
outageStartedAt = null;
lastErrorLoggedAt = 0;
errorsSinceLastLog = 0;
totalErrors = 0;
});
}

function resolveRedisUrl(): string {
// In apps/web on Vercel, dotenvx decrypts into process.env at runtime after
Expand All @@ -67,17 +120,34 @@ function resolveRedisUrl(): string {
return redisUrl;
}

function createRedis(maxRetriesPerRequest: number | null): Redis {
const client = new Redis(resolveRedisUrl(), {
maxRetriesPerRequest,
connectTimeout: 5000,
retryStrategy: (attempt) =>
Math.min(attempt * 50, REDIS_MAX_RECONNECT_DELAY_MS),
});
observeRedisConnectivity(client);
return client;
}

export const getRedis = () => {
if (!redis) {
redis = new Redis(resolveRedisUrl(), {
maxRetriesPerRequest: null,
connectTimeout: 5000,
});
redis = createRedis(REDIS_MAX_RETRIES_PER_REQUEST);
}

return redis;
};

export const getBullMqRedis = () => {
if (!bullMqRedis) {
// BullMQ blocking connections reject clients with a finite request retry limit.
bullMqRedis = createRedis(null);
}

return bullMqRedis;
};

export { acquireRedisLock, withRedisLock, withContention } from './lock';
export type {
RedisLockOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Queue, QueueEvents } from 'bullmq';

import { getRedis } from '@roomote/redis';
import { getBullMqRedis, getRedis } from '@roomote/redis';
import { DOCKER_VALIDATION_QUEUE_NAME } from '@roomote/types';
import type { DockerEnvironmentValidationResult } from '@roomote/compute-providers';

Expand Down Expand Up @@ -39,7 +39,7 @@ function getValidationQueue() {
function getValidationQueueEvents(): QueueEvents {
if (!validationQueueEvents) {
validationQueueEvents = new QueueEvents(DOCKER_VALIDATION_QUEUE_NAME, {
connection: getRedis(),
connection: getBullMqRedis(),
});
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/sdk/src/server/lib/task-runs/enqueue-sleep.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Queue, QueueEvents } from 'bullmq';
import { z } from 'zod';

import { getRedis } from '@roomote/redis';
import { getBullMqRedis, getRedis } from '@roomote/redis';

export const TASK_SLEEP_QUEUE_NAME = 'task-sleep-jobs';
const TASK_SLEEP_RESULT_TIMEOUT_MS = 60_000;
Expand Down Expand Up @@ -42,7 +42,7 @@ function getTaskSleepQueue(): Queue<TaskSleepRequest> {
function getTaskSleepQueueEvents(): QueueEvents {
if (!taskSleepQueueEvents) {
taskSleepQueueEvents = new QueueEvents(TASK_SLEEP_QUEUE_NAME, {
connection: getRedis(),
connection: getBullMqRedis(),
});
}

Expand Down
Loading