修正getAllUsers(),修正启动时自动迁移执行多次

This commit is contained in:
mtvpls
2025-12-24 23:43:28 +08:00
parent c7b9097447
commit e26c412c47
3 changed files with 71 additions and 60 deletions

View File

@@ -55,6 +55,7 @@ export const API_CONFIG = {
// 在模块加载时根据环境决定配置来源
let cachedConfig: AdminConfig;
let configInitPromise: Promise<AdminConfig> | null = null;
// 从配置文件补充管理员配置
@@ -303,6 +304,13 @@ export async function getConfig(): Promise<AdminConfig> {
return cachedConfig;
}
// 如果正在初始化,等待初始化完成
if (configInitPromise) {
return configInitPromise;
}
// 创建初始化 Promise
configInitPromise = (async () => {
// 读 db
let adminConfig: AdminConfig | null = null;
let dbReadFailed = false;
@@ -353,7 +361,12 @@ export async function getConfig(): Promise<AdminConfig> {
}
}
// 清除初始化 Promise
configInitPromise = null;
return cachedConfig;
})();
return configInitPromise;
}
export function configSelfCheck(adminConfig: AdminConfig): AdminConfig {

View File

@@ -645,13 +645,12 @@ export abstract class BaseRedisStorage implements IStorage {
// ---------- 获取全部用户 ----------
async getAllUsers(): Promise<string[]> {
const keys = await this.withRetry(() => this.client.keys('u:*:pwd'));
return keys
.map((k) => {
const match = k.match(/^u:(.+?):pwd$/);
return match ? ensureString(match[1]) : undefined;
})
.filter((u): u is string => typeof u === 'string');
// 从新版用户列表获取
const userListKey = this.userListKey();
const users = await this.withRetry(() =>
this.client.zRange(userListKey, 0, -1)
);
return users.map(u => ensureString(u));
}
// ---------- 管理员配置 ----------

View File

@@ -673,13 +673,12 @@ export class UpstashRedisStorage implements IStorage {
// ---------- 获取全部用户 ----------
async getAllUsers(): Promise<string[]> {
const keys = await withRetry(() => this.client.keys('u:*:pwd'));
return keys
.map((k) => {
const match = k.match(/^u:(.+?):pwd$/);
return match ? ensureString(match[1]) : undefined;
})
.filter((u): u is string => typeof u === 'string');
// 从新版用户列表获取
const userListKey = this.userListKey();
const users = await withRetry(() =>
this.client.zrange(userListKey, 0, -1)
);
return users.map(u => ensureString(u));
}
// ---------- 管理员配置 ----------