From f217c65169f5783c8e6bcbd62c5fecbc93d1fc0e Mon Sep 17 00:00:00 2001 From: Mason Fish Date: Wed, 17 Nov 2021 18:00:14 -0800 Subject: [PATCH] Query timeout extension (#1969) * extend timeout for query endpoint Signed-off-by: Mason Fish * fix spectron link Signed-off-by: Mason Fish Co-authored-by: Mason Fish --- docs/Code-Base-Walkthrough.md | 2 +- test/integration/README.md | 1 - zealot/api/query.ts | 3 ++- zealot/config/query-args.ts | 2 ++ zealot/fetcher/fetcher.ts | 17 +++++++++++------ zealot/types.ts | 1 + 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/Code-Base-Walkthrough.md b/docs/Code-Base-Walkthrough.md index 5d7436469d..b929a8c353 100644 --- a/docs/Code-Base-Walkthrough.md +++ b/docs/Code-Base-Walkthrough.md @@ -66,7 +66,7 @@ Additionally, we rely heavily on the node modules listed here: **Testing** - [Jest ](https://jestjs.io/docs/en/getting-started) Unit tests are run with `npm run test` -- [Spectron](https://www.electronjs.org/spectron) Used for integration testing `npm run itest` +- [Spectron](https://github.com/electron-userland/spectron) Used for integration testing `npm run itest` ## Patterns diff --git a/test/integration/README.md b/test/integration/README.md index 06cb792c77..c564f34d7e 100644 --- a/test/integration/README.md +++ b/test/integration/README.md @@ -8,7 +8,6 @@ tests are better suited as Brim unit tests or tests in the Zed repository. The tests use Jest and Spectron. - https://jestjs.io -- https://www.electronjs.org/spectron - https://github.com/electron-userland/spectron ## Requirements diff --git a/zealot/api/query.ts b/zealot/api/query.ts index 9d446af94d..b2a116a264 100644 --- a/zealot/api/query.ts +++ b/zealot/api/query.ts @@ -8,7 +8,8 @@ export default function queryApi(zed: string, args: QueryArgs): FetchArgs { path: `/query?${getQueryParams(args)}`, body: JSON.stringify({query: zed}), headers: getHeaders(args), - signal: args.signal + signal: args.signal, + timeout: args.timeout } } diff --git a/zealot/config/query-args.ts b/zealot/config/query-args.ts index 28fa409853..41ed857854 100644 --- a/zealot/config/query-args.ts +++ b/zealot/config/query-args.ts @@ -2,6 +2,8 @@ import {QueryArgs} from "../types" export function getDefaultQueryArgs(): QueryArgs { return { + // 5min + timeout: 300000, format: "zjson", controlMessages: true, enhancers: [] diff --git a/zealot/fetcher/fetcher.ts b/zealot/fetcher/fetcher.ts index 5e69bb3dac..779f643363 100644 --- a/zealot/fetcher/fetcher.ts +++ b/zealot/fetcher/fetcher.ts @@ -17,14 +17,15 @@ export type FetchArgs = { enhancers?: Enhancer[] signal?: AbortSignal useNodeFetch?: boolean + timeout?: number } const fetchWithTimeout = async ( baseUrl: string, args: FetchArgs ): Promise => { - const {path, method, body, signal, headers, useNodeFetch} = args - const [wrappedSignal, clearTimeout] = useTimeoutSignal(signal) + const {path, method, body, signal, headers, useNodeFetch, timeout} = args + const [wrappedSignal, clearTimeout] = useTimeoutSignal({signal, timeout}) if (body instanceof stream.Readable) { body.once("data", () => clearTimeout()) body.once("start", () => clearTimeout()) @@ -49,9 +50,13 @@ const fetchWithTimeout = async ( } } -const useTimeoutSignal = ( - wrappedSignal?: AbortSignal -): [AbortSignal, () => void] => { +const useTimeoutSignal = ({ + signal: wrappedSignal, + timeout = 10000 +}: { + signal?: AbortSignal + timeout?: number +}): [AbortSignal, () => void] => { // TODO: once we upgrade to Node 16, we won't need this polyfill let timeoutController try { @@ -64,7 +69,7 @@ const useTimeoutSignal = ( const id = setTimeout(() => { if (timeoutController.signal.aborted) return timeoutController.abort() - }, 10000) + }, timeout) const clear = () => clearTimeout(id) if (wrappedSignal) { diff --git a/zealot/types.ts b/zealot/types.ts index ee5b7e3657..5ebd418600 100644 --- a/zealot/types.ts +++ b/zealot/types.ts @@ -33,6 +33,7 @@ export interface QueryArgs { format: QueryFormat controlMessages?: boolean enhancers?: Enhancer[] + timeout?: number signal?: AbortSignal }