fix: prevent mindspace space endpoint optional data timeout
This commit is contained in:
+36
-14
@@ -43,6 +43,20 @@ function asNumber(value) {
|
||||
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) {
|
||||
return {
|
||||
id: row.id,
|
||||
@@ -139,7 +153,11 @@ export function createMindSpaceService(pool, options = {}) {
|
||||
const scheduleService = options.scheduleService ?? null;
|
||||
const resolvePublicPageLimit = async () => {
|
||||
try {
|
||||
const config = await loadMindSpaceConfig(pool);
|
||||
const config = await withOptionalTimeout(
|
||||
loadMindSpaceConfig(pool),
|
||||
1000,
|
||||
{ publicPageLimit: publicPageLimitFallback },
|
||||
);
|
||||
return Number(config.publicPageLimit ?? publicPageLimitFallback);
|
||||
} catch {
|
||||
return publicPageLimitFallback;
|
||||
@@ -195,19 +213,23 @@ export function createMindSpaceService(pool, options = {}) {
|
||||
let schedule = null;
|
||||
if (scheduleService) {
|
||||
try {
|
||||
const [todayTodoItems, digestSubscriptions] = await Promise.all([
|
||||
typeof scheduleService.listTodayTodoItems === 'function'
|
||||
? scheduleService.listTodayTodoItems({ userId })
|
||||
: Promise.resolve([]),
|
||||
typeof scheduleService.listDigestSubscriptions === 'function'
|
||||
? scheduleService.listDigestSubscriptions({
|
||||
userId,
|
||||
digestType: 'todo_day',
|
||||
status: ['active', 'locked'],
|
||||
limit: 10,
|
||||
})
|
||||
: Promise.resolve([]),
|
||||
]);
|
||||
const [todayTodoItems, digestSubscriptions] = await withOptionalTimeout(
|
||||
Promise.all([
|
||||
typeof scheduleService.listTodayTodoItems === 'function'
|
||||
? scheduleService.listTodayTodoItems({ userId })
|
||||
: Promise.resolve([]),
|
||||
typeof scheduleService.listDigestSubscriptions === 'function'
|
||||
? scheduleService.listDigestSubscriptions({
|
||||
userId,
|
||||
digestType: 'todo_day',
|
||||
status: ['active', 'locked'],
|
||||
limit: 10,
|
||||
})
|
||||
: Promise.resolve([]),
|
||||
]),
|
||||
1000,
|
||||
[[], []],
|
||||
);
|
||||
schedule = { todayTodoItems, digestSubscriptions };
|
||||
} catch {
|
||||
schedule = null;
|
||||
|
||||
Reference in New Issue
Block a user