Skip to content

Commit

Permalink
Query timeout extension (#1969)
Browse files Browse the repository at this point in the history
* extend timeout for query endpoint

Signed-off-by: Mason Fish <mason@brimsecurity.com>

* fix spectron link

Signed-off-by: Mason Fish <mason@brimsecurity.com>

Co-authored-by: Mason Fish <mason@brimsecurity.com>
  • Loading branch information
mason-fish and Mason Fish authored Nov 18, 2021
1 parent 3544409 commit f217c65
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/Code-Base-Walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion test/integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion zealot/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 2 additions & 0 deletions zealot/config/query-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {QueryArgs} from "../types"

export function getDefaultQueryArgs(): QueryArgs {
return {
// 5min
timeout: 300000,
format: "zjson",
controlMessages: true,
enhancers: []
Expand Down
17 changes: 11 additions & 6 deletions zealot/fetcher/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ export type FetchArgs = {
enhancers?: Enhancer[]
signal?: AbortSignal
useNodeFetch?: boolean
timeout?: number
}

const fetchWithTimeout = async (
baseUrl: string,
args: FetchArgs
): Promise<Response> => {
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())
Expand All @@ -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 {
Expand All @@ -64,7 +69,7 @@ const useTimeoutSignal = (
const id = setTimeout(() => {
if (timeoutController.signal.aborted) return
timeoutController.abort()
}, 10000)
}, timeout)
const clear = () => clearTimeout(id)

if (wrappedSignal) {
Expand Down
1 change: 1 addition & 0 deletions zealot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface QueryArgs {
format: QueryFormat
controlMessages?: boolean
enhancers?: Enhancer[]
timeout?: number
signal?: AbortSignal
}

Expand Down

0 comments on commit f217c65

Please sign in to comment.