Add query timeouts

This commit is contained in:
Alex Gleason
2024-06-29 22:26:51 +01:00
parent bdd3b2224e
commit 9ea6c7b00b
4 changed files with 23 additions and 16 deletions

View File

@@ -73,11 +73,15 @@ function connectStream(socket: WebSocket) {
const pubsub = await Storages.pubsub();
try {
for (const event of await store.query(filters, { limit: FILTER_LIMIT })) {
for (const event of await store.query(filters, { limit: FILTER_LIMIT, timeout: 500 })) {
send(['EVENT', subId, event]);
}
} catch (e) {
send(['CLOSED', subId, e.message]);
if (e instanceof RelayError) {
send(['CLOSED', subId, e.message]);
} else {
send(['CLOSED', subId, 'error: something went wrong']);
}
controllers.delete(subId);
return;
}
@@ -124,7 +128,7 @@ function connectStream(socket: WebSocket) {
/** Handle COUNT. Return the number of events matching the filters. */
async function handleCount([_, subId, ...filters]: NostrClientCOUNT): Promise<void> {
const store = await Storages.db();
const { count } = await store.count(filters);
const { count } = await store.count(filters, { timeout: 500 });
send(['COUNT', subId, { count, approximate: false }]);
}