Try setting a timeout for context endpoint

This commit is contained in:
Alex Gleason 2023-04-30 17:12:51 -05:00
parent 7bb8821b1b
commit 3da9ffab9e
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 10 additions and 3 deletions

View file

@ -116,7 +116,7 @@ async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise
} }
function getDescendants(eventId: string): Promise<SignedEvent<1>[]> { function getDescendants(eventId: string): Promise<SignedEvent<1>[]> {
return getFilter({ kinds: [1], '#e': [eventId] }) as Promise<SignedEvent<1>[]>; return getFilter({ kinds: [1], '#e': [eventId], limit: 200 }) as Promise<SignedEvent<1>[]>;
} }
export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFollows, pool }; export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFollows, pool };

View file

@ -42,14 +42,21 @@ const createStatusController = validator('json', async (value, c: AppContext) =>
} }
}); });
// https://stackoverflow.com/a/64820881/8811886
function wait(ms: number) {
return new Promise((_, reject) => {
setTimeout(() => reject(new Error('timeout succeeded')), ms);
});
}
const contextController: AppController = async (c) => { const contextController: AppController = async (c) => {
const id = c.req.param('id'); const id = c.req.param('id');
const event = await getEvent(id, 1); const event = await getEvent(id, 1);
if (event) { if (event) {
const ancestorEvents = await getAncestors(event); const ancestorEvents = await Promise.race([wait(1000), getAncestors(event)]) as Event<1>[];
const descendantEvents = await getDescendants(event.id); const descendantEvents = await Promise.race([wait(1000), getDescendants(event.id)]) as Event<1>[];
return c.json({ return c.json({
ancestors: (await Promise.all((ancestorEvents).map(toStatus))).filter(Boolean), ancestors: (await Promise.all((ancestorEvents).map(toStatus))).filter(Boolean),