-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3012 from OpenNeuroOrg/dataset-query-timeout
Add 30 second timeout to dataset resolver
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class PromiseTimeoutError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
this.name = "PromiseTimeoutError" | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/openneuro-server/src/utils/__tests__/promiseTimeout.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { promiseTimeout } from "../promiseTimeout" | ||
|
||
describe("withTimeout", () => { | ||
it("rejects with null when the promise exceeds timeout milliseconds", async () => { | ||
const slowPromise = new Promise((resolve) => | ||
setTimeout(() => resolve("Slow Result"), 500) | ||
) | ||
expect(await promiseTimeout(slowPromise, 100)).toBeNull() | ||
}) | ||
it("resolves when the promise returns before timeout", async () => { | ||
const slowPromise = new Promise((resolve) => | ||
setTimeout(() => resolve("Fast Result"), 10) | ||
) | ||
expect(await promiseTimeout(slowPromise, 500)).toBe("Fast Result") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PromiseTimeoutError } from "../types/promiseTimeoutError.ts" | ||
|
||
/** | ||
* Add a timeout to a promise and return null if timeout occurs before the promise resolves | ||
* @param promise Promise to timeout | ||
* @param timeout Timeout in milliseconds | ||
*/ | ||
export async function promiseTimeout<T>( | ||
promise: Promise<T>, | ||
timeout: number, | ||
): Promise<T | null> { | ||
try { | ||
const result = await Promise.race([ | ||
promise, | ||
new Promise((_, reject) => | ||
setTimeout( | ||
() => reject(new PromiseTimeoutError("Operation timed out")), | ||
timeout, | ||
) | ||
), | ||
]) | ||
|
||
return result | ||
} catch (error) { | ||
if (error instanceof PromiseTimeoutError) { | ||
return null | ||
} else { | ||
throw error // Re-throw other errors | ||
} | ||
} | ||
} |