Skip to content

Commit

Permalink
[Discussion] Increase timeout but add another warning timeout for slo…
Browse files Browse the repository at this point in the history
…w servers

- per recommendation/convo with Brandon
  • Loading branch information
cee-chen committed Jul 1, 2020
1 parent e813abb commit 98ddbd3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/enterprise_search/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const plugin = (initializerContext: PluginInitializerContext) => {
export const configSchema = schema.object({
host: schema.maybe(schema.string()),
enabled: schema.boolean({ defaultValue: true }),
accessCheckTimeout: schema.number({ defaultValue: 200 }),
accessCheckTimeout: schema.number({ defaultValue: 5000 }),
accessCheckTimeoutWarning: schema.number({ defaultValue: 300 }),
});

type ConfigType = TypeOf<typeof configSchema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('callEnterpriseSearchConfigAPI', () => {
const mockConfig = {
host: 'http://localhost:3002',
accessCheckTimeout: 200,
accessCheckTimeoutWarning: 100,
};
const mockRequest = {
url: { path: '/app/kibana' },
Expand Down Expand Up @@ -90,11 +91,18 @@ describe('callEnterpriseSearchConfigAPI', () => {
it('handles timeouts', async () => {
jest.useFakeTimers();

// Warning
callEnterpriseSearchConfigAPI(mockDependencies);
jest.advanceTimersByTime(150);
expect(mockDependencies.log.warn).toHaveBeenCalledWith(
'Enterprise Search access check took over 100ms. Please ensure your Enterprise Search server is respondingly normally and not adversely impacting Kibana load speeds.'
);

// Timeout
fetchMock.mockImplementationOnce(async () => {
jest.advanceTimersByTime(250);
return Promise.reject({ name: 'AbortError' });
});

expect(await callEnterpriseSearchConfigAPI(mockDependencies)).toEqual({});
expect(mockDependencies.log.warn).toHaveBeenCalledWith(
"Exceeded 200ms timeout while checking http://localhost:3002. Please consider increasing your enterpriseSearch.accessCheckTimeout value so that users aren't prevented from accessing Enterprise Search plugins due to slow responses."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface IReturn {
publicUrl?: string;
access?: IAccess;
}
type TTimeout = ReturnType<typeof setTimeout>;

/**
* Calls an internal Enterprise Search API endpoint which returns
Expand All @@ -36,14 +35,20 @@ export const callEnterpriseSearchConfigAPI = async ({
}: IParams): Promise<IReturn> => {
if (!config.host) return {};

let timeout: TTimeout;
try {
// Timeout if the remote call to Enterprise Search takes too long
const controller = new AbortController();
timeout = setTimeout(() => {
controller.abort();
}, config.accessCheckTimeout);
const TIMEOUT_WARNING = `Enterprise Search access check took over ${config.accessCheckTimeoutWarning}ms. Please ensure your Enterprise Search server is respondingly normally and not adversely impacting Kibana load speeds.`;
const TIMEOUT_MESSAGE = `Exceeded ${config.accessCheckTimeout}ms timeout while checking ${config.host}. Please consider increasing your enterpriseSearch.accessCheckTimeout value so that users aren't prevented from accessing Enterprise Search plugins due to slow responses.`;
const CONNECTION_ERROR = 'Could not perform access check to Enterprise Search';

const warningTimeout = setTimeout(() => {
log.warn(TIMEOUT_WARNING);
}, config.accessCheckTimeoutWarning);

const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, config.accessCheckTimeout);

try {
const enterpriseSearchUrl = encodeURI(`${config.host}${ENDPOINT}`);
const response = await fetch(enterpriseSearchUrl, {
headers: { Authorization: request.headers.authorization as string },
Expand All @@ -60,15 +65,14 @@ export const callEnterpriseSearchConfigAPI = async ({
};
} catch (err) {
if (err.name === 'AbortError') {
const message = `Exceeded ${config.accessCheckTimeout}ms timeout while checking ${config.host}. Please consider increasing your enterpriseSearch.accessCheckTimeout value so that users aren't prevented from accessing Enterprise Search plugins due to slow responses.`;
log.warn(message);
log.warn(TIMEOUT_MESSAGE);
} else {
const message = `Could not perform access check to Enterprise Search: ${err.toString()}`;
log.error(message);
log.error(`${CONNECTION_ERROR}: ${err.toString()}`);
if (err instanceof Error) log.debug(err.stack as string);
}
return {};
} finally {
clearTimeout(timeout! as TTimeout);
clearTimeout(warningTimeout);
clearTimeout(timeout);
}
};
1 change: 1 addition & 0 deletions x-pack/plugins/enterprise_search/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ServerConfigType {
host?: string;
enabled: boolean;
accessCheckTimeout: number;
accessCheckTimeoutWarning: number;
}

export interface IRouteDependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
export const mockConfig = {
enabled: true,
host: 'http://localhost:3002',
accessCheckTimeout: 200,
accessCheckTimeout: 5000,
accessCheckTimeoutWarning: 300,
};

0 comments on commit 98ddbd3

Please sign in to comment.