fix(admin): ioredis 5.x username/password in redisOptions, add logging
This commit is contained in:
parent
779aaba575
commit
eba75ee359
@ -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(),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -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,
|
||||||
|
redisOptions: {
|
||||||
|
username,
|
||||||
|
password,
|
||||||
retryStrategy: (times: number) => Math.min(times * 100, 3000),
|
retryStrategy: (times: number) => Math.min(times * 100, 3000),
|
||||||
// 从第一个 URL 提取认证信息(所有节点共用相同密码)
|
},
|
||||||
username: firstUrl.username || undefined,
|
|
||||||
password: firstUrl.password || undefined,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user