-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
request_utils.ts
55 lines (51 loc) · 2.11 KB
/
request_utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import type { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport';
import type { Search } from '@elastic/elasticsearch/api/requestParams';
import type { IUiSettingsClient, SharedGlobalConfig } from 'kibana/server';
import { UI_SETTINGS } from '../../../common';
export function getShardTimeout(config: SharedGlobalConfig): Pick<Search, 'timeout'> {
const timeout = config.elasticsearch.shardTimeout.asMilliseconds();
return timeout ? { timeout: `${timeout}ms` } : {};
}
export async function getDefaultSearchParams(
uiSettingsClient: IUiSettingsClient
): Promise<
Pick<Search, 'max_concurrent_shard_requests' | 'ignore_unavailable' | 'track_total_hits'>
> {
const maxConcurrentShardRequests = await uiSettingsClient.get<number>(
UI_SETTINGS.COURIER_MAX_CONCURRENT_SHARD_REQUESTS
);
return {
max_concurrent_shard_requests:
maxConcurrentShardRequests > 0 ? maxConcurrentShardRequests : undefined,
ignore_unavailable: true, // Don't fail if the index/indices don't exist
track_total_hits: true,
};
}
/**
* Temporary workaround until https://github.com/elastic/elasticsearch-js/issues/1297 is resolved.
* Shims the `AbortSignal` behavior so that, if the given `signal` aborts, the `abort` method on the
* `TransportRequestPromise` is called, actually performing the cancellation.
* @internal
*/
export const shimAbortSignal = <T>(promise: TransportRequestPromise<T>, signal?: AbortSignal) => {
if (!signal) return promise;
const abortHandler = () => {
promise.abort();
cleanup();
};
const cleanup = () => signal.removeEventListener('abort', abortHandler);
if (signal.aborted) {
promise.abort();
} else {
signal.addEventListener('abort', abortHandler);
promise.then(cleanup, cleanup);
}
return promise;
};