fix: prevent mindspace space endpoint optional data timeout

This commit is contained in:
john
2026-06-29 22:37:30 +08:00
parent a40e340a41
commit 4fcb48395e
+36 -14
View File
@@ -43,6 +43,20 @@ function asNumber(value) {
return Number(value ?? 0); return Number(value ?? 0);
} }
async function withOptionalTimeout(promise, timeoutMs, fallback) {
let timeout;
try {
return await Promise.race([
promise,
new Promise((resolve) => {
timeout = setTimeout(() => resolve(fallback), timeoutMs);
}),
]);
} finally {
if (timeout) clearTimeout(timeout);
}
}
function categoryResponse(row) { function categoryResponse(row) {
return { return {
id: row.id, id: row.id,
@@ -139,7 +153,11 @@ export function createMindSpaceService(pool, options = {}) {
const scheduleService = options.scheduleService ?? null; const scheduleService = options.scheduleService ?? null;
const resolvePublicPageLimit = async () => { const resolvePublicPageLimit = async () => {
try { try {
const config = await loadMindSpaceConfig(pool); const config = await withOptionalTimeout(
loadMindSpaceConfig(pool),
1000,
{ publicPageLimit: publicPageLimitFallback },
);
return Number(config.publicPageLimit ?? publicPageLimitFallback); return Number(config.publicPageLimit ?? publicPageLimitFallback);
} catch { } catch {
return publicPageLimitFallback; return publicPageLimitFallback;
@@ -195,19 +213,23 @@ export function createMindSpaceService(pool, options = {}) {
let schedule = null; let schedule = null;
if (scheduleService) { if (scheduleService) {
try { try {
const [todayTodoItems, digestSubscriptions] = await Promise.all([ const [todayTodoItems, digestSubscriptions] = await withOptionalTimeout(
typeof scheduleService.listTodayTodoItems === 'function' Promise.all([
? scheduleService.listTodayTodoItems({ userId }) typeof scheduleService.listTodayTodoItems === 'function'
: Promise.resolve([]), ? scheduleService.listTodayTodoItems({ userId })
typeof scheduleService.listDigestSubscriptions === 'function' : Promise.resolve([]),
? scheduleService.listDigestSubscriptions({ typeof scheduleService.listDigestSubscriptions === 'function'
userId, ? scheduleService.listDigestSubscriptions({
digestType: 'todo_day', userId,
status: ['active', 'locked'], digestType: 'todo_day',
limit: 10, status: ['active', 'locked'],
}) limit: 10,
: Promise.resolve([]), })
]); : Promise.resolve([]),
]),
1000,
[[], []],
);
schedule = { todayTodoItems, digestSubscriptions }; schedule = { todayTodoItems, digestSubscriptions };
} catch { } catch {
schedule = null; schedule = null;