fix(admin): ioredis 5.x username/password in redisOptions, add logging
Some checks are pending
CI / Rust Lint & Check (push) Waiting to run
CI / Rust Tests (push) Waiting to run
CI / Frontend Lint & Type Check (push) Waiting to run
CI / Frontend Build (push) Blocked by required conditions

This commit is contained in:
ZhenYi 2026-04-20 15:50:51 +08:00
parent 779aaba575
commit eba75ee359
2 changed files with 20 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import type { NextConfig } from "next"; import type { NextConfig } from "next";
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
serverExternalPackages: ["bcrypt"], serverExternalPackages: ["bcrypt", "ioredis"],
turbopack: { turbopack: {
root: process.cwd(), root: process.cwd(),
}, },

View File

@ -35,16 +35,20 @@ function createClusterClient(): Redis {
}); });
const firstUrl = new URL(REDIS_CLUSTER_URLS[0]); const firstUrl = new URL(REDIS_CLUSTER_URLS[0]);
const username = firstUrl.username || undefined;
const password = firstUrl.password || undefined;
// ioredis 5.x: Cluster 是 default export, redisOptions 展开到顶层, 无 clusterRetryStrategy // ioredis 5.x: username/password 必须放在 redisOptions 里,不能放顶层
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const cluster = new (Cluster as any)(nodes, { const cluster = new (Cluster as any)(nodes, {
lazyConnect: true, lazyConnect: true,
enableReadyCheck: true,
maxRetriesPerRequest: 3, maxRetriesPerRequest: 3,
retryStrategy: (times: number) => Math.min(times * 100, 3000), redisOptions: {
// 从第一个 URL 提取认证信息(所有节点共用相同密码) username,
username: firstUrl.username || undefined, password,
password: firstUrl.password || undefined, retryStrategy: (times: number) => Math.min(times * 100, 3000),
},
}); });
return cluster as Redis; return cluster as Redis;
@ -52,6 +56,16 @@ function createClusterClient(): Redis {
export function getRedis(): Redis { export function getRedis(): Redis {
if (!redis) { if (!redis) {
const mode = REDIS_CLUSTER_URLS.length > 1 ? "cluster" : "single";
const clusterNodes = REDIS_CLUSTER_URLS.map((url) => {
const u = new URL(url);
return `${u.hostname}:${u.port}`;
});
console.log("[Redis] Initializing", {
mode,
clusterNodes,
singleUrl: mode === "single" ? REDIS_URL : undefined,
});
redis = redis =
REDIS_CLUSTER_URLS.length > 1 ? createClusterClient() : createSingleClient(); REDIS_CLUSTER_URLS.length > 1 ? createClusterClient() : createSingleClient();
} }