Skip to content

Commit

Permalink
handler-fetch: implement queryTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Feb 8, 2024
1 parent 5c2288d commit b06cb0b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/handler-fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export const factory = async (trifid) => {
const { config, logger } = trifid
const { contentType, url, baseIri, graphName, unionDefaultGraph } = config

const queryTimeout = 30000

const workerUrl = new URL('./lib/worker.js', import.meta.url)
const worker = new Worker(workerUrl)
worker.unref()

let ready = false

worker.on('online', () => worker.unref())

worker.on('message', async (message) => {
const { type, data } = message
if (type === 'log') {
Expand All @@ -28,10 +28,12 @@ export const factory = async (trifid) => {
})

worker.on('error', (error) => {
ready = false
logger.error(`Error from worker: ${error.message}`)
})

worker.on('exit', (code) => {
ready = false
logger.info(`Worker exited with code ${code}`)
})

Expand All @@ -49,9 +51,18 @@ export const factory = async (trifid) => {
* @returns {Promise<{ response: string, contentType: string }>} The response and its content type
*/
const handleQuery = async (query) => {
return new Promise((resolve, _reject) => {
return new Promise((resolve, reject) => {
if (!ready) {
return reject(new Error('Worker is not ready'))
}

const queryId = uuidv4()

const timeoutId = setTimeout(() => {
worker.off('message', messageHandler)
reject(new Error(`Query timed out after ${queryTimeout / 1000} seconds`))
}, queryTimeout)

worker.postMessage({
type: 'query',
data: {
Expand All @@ -63,6 +74,7 @@ export const factory = async (trifid) => {
const messageHandler = (message) => {
const { type, data } = message
if (type === 'query' && data.queryId === queryId) {
clearTimeout(timeoutId)
worker.off('message', messageHandler)
resolve(data)
}
Expand Down

0 comments on commit b06cb0b

Please sign in to comment.