New logging if we get lots of redux actions in small time

This commit is contained in:
automated-signal 2024-08-29 13:13:32 -05:00 committed by GitHub
parent 37fa0b3466
commit 320347a90a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 3 deletions

View file

@ -3,16 +3,19 @@
/* eslint-disable no-console */
import type { Store } from 'redux';
import type { Middleware, Store } from 'redux';
import { applyMiddleware, createStore as reduxCreateStore } from 'redux';
import promise from 'redux-promise-middleware';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import * as log from '../logging/log';
import type { StateType } from './reducer';
import { reducer } from './reducer';
import { dispatchItemsMiddleware } from '../shims/dispatchItemsMiddleware';
import { isOlderThan } from '../util/timestamp';
import { SECOND } from '../util/durations';
declare global {
// We want to extend `window`'s properties, so we need an interface.
@ -47,10 +50,49 @@ const logger = createLogger({
},
});
const ACTION_COUNT_THRESHOLD = 25;
type ActionStats = {
timestamp: number;
names: Array<string>;
};
const actionStats: ActionStats = {
timestamp: Date.now(),
names: [],
};
export const actionRateLogger: Middleware = () => next => action => {
const name = action.type;
const lastTimestamp = actionStats.timestamp;
let count = actionStats.names.length;
if (isOlderThan(lastTimestamp, SECOND)) {
if (count > 0) {
actionStats.names = [];
}
actionStats.timestamp = Date.now();
return next(action);
}
actionStats.names.push(name);
count += 1;
if (count >= ACTION_COUNT_THRESHOLD) {
log.warn(
`ActionRateLogger: got ${count} events since ${lastTimestamp}: ${actionStats.names.join(',')}`
);
actionStats.names = [];
actionStats.timestamp = Date.now();
}
return next(action);
};
const middlewareList = [
promise,
thunk,
dispatchItemsMiddleware,
actionRateLogger,
...(env === 'production' ? [] : [logger]),
];

View file

@ -12,11 +12,10 @@ import { getEmptyState as composerEmptyState } from './ducks/composer';
import { getEmptyState as conversationsEmptyState } from './ducks/conversations';
import { getEmptyState as crashReportsEmptyState } from './ducks/crashReports';
import { getEmptyState as emojiEmptyState } from './ducks/emojis';
import { getEmptyState as itemsEmptyState } from './ducks/items';
import { getEmptyState as stickersEmptyState } from './ducks/stickers';
import { getEmptyState as expirationEmptyState } from './ducks/expiration';
import { getEmptyState as globalModalsEmptyState } from './ducks/globalModals';
import { getEmptyState as inboxEmptyState } from './ducks/inbox';
import { getEmptyState as itemsEmptyState } from './ducks/items';
import { getEmptyState as lightboxEmptyState } from './ducks/lightbox';
import { getEmptyState as linkPreviewsEmptyState } from './ducks/linkPreviews';
import { getEmptyState as mediaGalleryEmptyState } from './ducks/mediaGallery';
@ -25,6 +24,7 @@ import { getEmptyState as networkEmptyState } from './ducks/network';
import { getEmptyState as preferredReactionsEmptyState } from './ducks/preferredReactions';
import { getEmptyState as safetyNumberEmptyState } from './ducks/safetyNumber';
import { getEmptyState as searchEmptyState } from './ducks/search';
import { getEmptyState as stickersEmptyState } from './ducks/stickers';
import { getEmptyState as storiesEmptyState } from './ducks/stories';
import { getEmptyState as storyDistributionListsEmptyState } from './ducks/storyDistributionLists';
import { getEmptyState as toastEmptyState } from './ducks/toast';