Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle toast storms related to saving/restoring URL state #154792

Merged
merged 5 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { withNotifyOnErrors } from './errors';
import { notificationServiceMock } from '@kbn/core-notifications-browser-mocks';

describe('state management URL errors', () => {
const notifications = notificationServiceMock.createStartContract();

beforeEach(() => {
notifications.toasts.addError.mockClear();
});

test('notifies on restore error only once', () => {
const { onGetError } = withNotifyOnErrors(notifications.toasts);
const error = new Error();
onGetError(error);
onGetError(error);
expect(notifications.toasts.addError).toBeCalledTimes(1);
});

test('notifies on save error only once', () => {
const { onSetError } = withNotifyOnErrors(notifications.toasts);
const error = new Error();
onSetError(error);
onSetError(error);
expect(notifications.toasts.addError).toBeCalledTimes(1);
});
});
31 changes: 21 additions & 10 deletions src/plugins/kibana_utils/public/state_management/url/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { throttle } from 'lodash';
import { i18n } from '@kbn/i18n';
import { NotificationsStart } from '@kbn/core/public';

Expand All @@ -23,6 +24,24 @@ export const saveStateInUrlErrorTitle = i18n.translate(
}
);

// Prevent toast storms by throttling. See https://github.com/elastic/kibana/issues/153073
const throttledOnRestoreError = throttle((toasts: NotificationsStart['toasts'], e: Error) => {
toasts.addError(e, {
title: restoreUrlErrorTitle,
});
}, 10000);
const throttledOnSaveError = throttle((toasts: NotificationsStart['toasts'], e: Error) => {
toasts.addError(e, {
title: saveStateInUrlErrorTitle,
});
}, 10000);

// Helper to bypass throttling if consumers need to handle errors right away
export const flushNotifyOnErrors = () => {
throttledOnRestoreError.flush();
throttledOnSaveError.flush();
};

/**
* Helper for configuring {@link IKbnUrlStateStorage} to notify about inner errors
*
Expand All @@ -37,15 +56,7 @@ export const saveStateInUrlErrorTitle = i18n.translate(
*/
export const withNotifyOnErrors = (toasts: NotificationsStart['toasts']) => {
return {
onGetError: (error: Error) => {
toasts.addError(error, {
title: restoreUrlErrorTitle,
});
},
onSetError: (error: Error) => {
toasts.addError(error, {
title: saveStateInUrlErrorTitle,
});
},
onGetError: (e: Error) => throttledOnRestoreError(toasts, e),
onSetError: (e: Error) => throttledOnSaveError(toasts, e),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ export {
} from './kbn_url_storage';
export { createKbnUrlTracker } from './kbn_url_tracker';
export { createUrlTracker } from './url_tracker';
export { withNotifyOnErrors, saveStateInUrlErrorTitle, restoreUrlErrorTitle } from './errors';
export {
withNotifyOnErrors,
flushNotifyOnErrors,
saveStateInUrlErrorTitle,
restoreUrlErrorTitle,
} from './errors';
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { History, createBrowserHistory } from 'history';
import { takeUntil, toArray } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { CoreScopedHistory } from '@kbn/core/public';
import { withNotifyOnErrors } from '../../state_management/url';
import { withNotifyOnErrors, flushNotifyOnErrors } from '../../state_management/url';
import { coreMock } from '@kbn/core/public/mocks';

describe('KbnUrlStateStorage', () => {
Expand Down Expand Up @@ -123,6 +123,7 @@ describe('KbnUrlStateStorage', () => {
const key = '_s';
history.replace(`/#?${key}=(ok:2,test:`); // malformed rison
expect(() => urlStateStorage.get(key)).not.toThrow();
flushNotifyOnErrors();
expect(toasts.addError).toBeCalled();
});
});
Expand Down Expand Up @@ -304,6 +305,7 @@ describe('KbnUrlStateStorage', () => {
const key = '_s';
history.replace(`/?${key}=(ok:2,test:`); // malformed rison
expect(() => urlStateStorage.get(key)).not.toThrow();
flushNotifyOnErrors();
expect(toasts.addError).toBeCalled();
});
});
Expand Down
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@kbn/test-jest-helpers",
"@kbn/rison",
"@kbn/crypto-browser",
"@kbn/core-notifications-browser-mocks",
],
"exclude": [
"target/**/*",
Expand Down