Skip to content
Snippets Groups Projects
Unverified Commit 706561aa authored by Diego Sampaio's avatar Diego Sampaio Committed by GitHub
Browse files

chore: throttle exceptions counter increment (#29719)

parent 18c55b0b
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,11 @@ import { Settings, Users, Rooms } from '@rocket.chat/models'; ...@@ -3,6 +3,11 @@ import { Settings, Users, Rooms } from '@rocket.chat/models';
import { settings } from '../../../settings/server'; import { settings } from '../../../settings/server';
import { sendMessage } from '../../../lib/server'; import { sendMessage } from '../../../lib/server';
import { throttledCounter } from '../../../../lib/utils/throttledCounter';
const incException = throttledCounter((counter) => {
Settings.incrementValueById('Uncaught_Exceptions_Count', counter).catch(console.error);
}, 10000);
class ErrorHandler { class ErrorHandler {
reporting: boolean; reporting: boolean;
...@@ -40,7 +45,7 @@ class ErrorHandler { ...@@ -40,7 +45,7 @@ class ErrorHandler {
async registerHandlers() { async registerHandlers() {
process.on('uncaughtException', async (error) => { process.on('uncaughtException', async (error) => {
await Settings.incrementValueById('Uncaught_Exceptions_Count'); incException();
if (!this.reporting) { if (!this.reporting) {
return; return;
} }
......
import { MongoInternals } from 'meteor/mongo'; import { MongoInternals } from 'meteor/mongo';
import { Settings } from '@rocket.chat/models'; import { Settings } from '@rocket.chat/models';
import { throttledCounter } from '../../../../lib/utils/throttledCounter';
const timeoutQuery = parseInt(process.env.OBSERVERS_CHECK_TIMEOUT) || 2 * 60 * 1000; const timeoutQuery = parseInt(process.env.OBSERVERS_CHECK_TIMEOUT) || 2 * 60 * 1000;
const interval = parseInt(process.env.OBSERVERS_CHECK_INTERVAL) || 60 * 1000; const interval = parseInt(process.env.OBSERVERS_CHECK_INTERVAL) || 60 * 1000;
const debug = Boolean(process.env.OBSERVERS_CHECK_DEBUG); const debug = Boolean(process.env.OBSERVERS_CHECK_DEBUG);
...@@ -44,6 +46,10 @@ setInterval(() => { ...@@ -44,6 +46,10 @@ setInterval(() => {
}); });
}, interval); }, interval);
const incException = throttledCounter((counter) => {
Settings.incrementValueById('Uncaught_Exceptions_Count', counter).catch(console.error);
}, 10000);
/** /**
* If some promise is rejected and doesn't have a catch (unhandledRejection) it may cause this finally * If some promise is rejected and doesn't have a catch (unhandledRejection) it may cause this finally
* here https://github.com/meteor/meteor/blob/be6e529a739f47446950e045f4547ee60e5de7ae/packages/mongo/oplog_tailing.js#L348 * here https://github.com/meteor/meteor/blob/be6e529a739f47446950e045f4547ee60e5de7ae/packages/mongo/oplog_tailing.js#L348
...@@ -58,8 +64,8 @@ setInterval(() => { ...@@ -58,8 +64,8 @@ setInterval(() => {
* we will start respecting this and exit the process to prevent these kind of problems. * we will start respecting this and exit the process to prevent these kind of problems.
*/ */
process.on('unhandledRejection', async (error) => { process.on('unhandledRejection', (error) => {
await Settings.incrementValueById('Uncaught_Exceptions_Count'); incException();
console.error('=== UnHandledPromiseRejection ==='); console.error('=== UnHandledPromiseRejection ===');
console.error(error); console.error(error);
......
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import type { IUser } from '@rocket.chat/core-typings'; import type { IUser } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models'; import { Users } from '@rocket.chat/models';
import { throttle } from 'underscore';
import { callbacks } from '../../../lib/callbacks'; import { callbacks } from '../../../lib/callbacks';
import { canAddNewUser, getMaxActiveUsers, onValidateLicenses } from '../../app/license/server/license'; import { canAddNewUser, getMaxActiveUsers, onValidateLicenses } from '../../app/license/server/license';
...@@ -79,7 +80,7 @@ callbacks.add( ...@@ -79,7 +80,7 @@ callbacks.add(
'check-max-user-seats', 'check-max-user-seats',
); );
async function handleMaxSeatsBanners() { const handleMaxSeatsBanners = throttle(async function _handleMaxSeatsBanners() {
const maxActiveUsers = getMaxActiveUsers(); const maxActiveUsers = getMaxActiveUsers();
if (!maxActiveUsers) { if (!maxActiveUsers) {
...@@ -106,7 +107,7 @@ async function handleMaxSeatsBanners() { ...@@ -106,7 +107,7 @@ async function handleMaxSeatsBanners() {
} else { } else {
await enableDangerBanner(); await enableDangerBanner();
} }
} }, 10000);
callbacks.add('afterCreateUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners'); callbacks.add('afterCreateUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners');
......
import { throttle } from 'underscore';
export function throttledCounter(fn: (counter: number) => unknown, wait: number) {
let counter = 0;
const throttledFn = throttle(
() => {
fn(counter);
counter = 0;
},
wait,
{ leading: false },
);
return () => {
counter++;
throttledFn();
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment