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

chore: throttle exceptions counter increment #29719

Merged
merged 4 commits into from
Jul 4, 2023

Conversation

sampaiodiego
Copy link
Member

Proposed changes (including videos or screenshots)

Throttle down uncaught exceptions counter to every 10 seconds. This is to avoid too many operations into the settings collection.

Issue(s)

Steps to test or reproduce

Further comments

@changeset-bot
Copy link

changeset-bot bot commented Jul 4, 2023

⚠️ No Changeset found

Latest commit: 5661935

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

rodrigok
rodrigok previously approved these changes Jul 4, 2023
Comment on lines 1 to 20
import throttle from 'lodash.throttle';

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();
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this should be placed here, its not quite useful outside that scope

not sure about the lodash necessity as well

@codecov
Copy link

codecov bot commented Jul 4, 2023

Codecov Report

Merging #29719 (5661935) into develop (c10137e) will increase coverage by 15.25%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff              @@
##           develop   #29719       +/-   ##
============================================
+ Coverage    31.33%   46.58%   +15.25%     
============================================
  Files          570      693      +123     
  Lines        10839    12946     +2107     
  Branches      1993     2242      +249     
============================================
+ Hits          3396     6031     +2635     
+ Misses        7229     6589      -640     
- Partials       214      326      +112     
Flag Coverage Δ
e2e 46.55% <ø> (+15.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.


import { settings } from '../../../settings/server';
import { sendMessage } from '../../../lib/server';

const incException = throttledCounter((counter) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt of ignoring this setting on the watch.settings watcher?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main issue is that we'd still receive it from mongo.. so we'd need to remove it on oplog/change stream query, which is not good 😬

Comment on lines 1 to 20
import throttle from 'lodash.throttle';

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();
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import throttle from 'lodash.throttle';
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();
};
}
export const throttle = <R, A extends any[]>(
fn: (...args: A) => R,
delay: number
) => {
let wait = false;
let timeout: undefined | number;
return Object.assign(
(...args: A) => {
if (wait) return undefined;
const val = fn(...args);
wait = true;
timeout = window.setTimeout(() => {
wait = false;
}, delay);
return val;
}, {
stop: () => {
clearTimeout(timeout);
},
});
};

@@ -1,9 +1,14 @@
import { Meteor } from 'meteor/meteor';
import { Settings, Users, Rooms } from '@rocket.chat/models';
import { throttledCounter } from '@rocket.chat/tools';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { throttledCounter } from '@rocket.chat/tools';
import { throttle } from '@rocket.chat/tools';

Comment on lines 8 to 11
const incException = throttledCounter((counter) => {
Settings.incrementValueById('Uncaught_Exceptions_Count', counter).catch(console.error);
}, 10000);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const incException = throttledCounter((counter) => {
Settings.incrementValueById('Uncaught_Exceptions_Count', counter).catch(console.error);
}, 10000);
const incException = (() => {
let counter = 0;
const commit = throttle((increment: number) => {
console.log(counter);
counter = 0;
}, 10000);
return () => {
counter++;
commit(counter);
};
})()

@sampaiodiego sampaiodiego force-pushed the throttle-exceptions-counter branch from 03be8c3 to 5661935 Compare July 4, 2023 20:12
@sampaiodiego sampaiodiego requested a review from rodrigok July 4, 2023 20:17
@sampaiodiego sampaiodiego merged commit 706561a into develop Jul 4, 2023
@sampaiodiego sampaiodiego deleted the throttle-exceptions-counter branch July 4, 2023 20:54
@sampaiodiego sampaiodiego modified the milestones: 6.2.10, 6.2.9 Jul 5, 2023
gabriellsh added a commit that referenced this pull request Jul 12, 2023
…/reportUser

* 'develop' of github.com:RocketChat/Rocket.Chat: (28 commits)
  feat: Add missing variants to UIKit button (#29654)
  refactor: Remove Accountbox usage (#29786)
  chore: stop importing action manager as global (#29766)
  chore: create FeaturePreview Component (#29759)
  regression: add missing translations on MenuV2 replace (#29777)
  test: create mock package (#29765)
  fix: Prevent app's bridges from overriding lastMsg prop on rooms col (#29756)
  regression: option to start video conferences disappearing for regular users (#29763)
  Release 6.2.9
  Revert "Release 6.2.9"
  fix(meteor): `room-opened` event not dispatching when navigating cached rooms (#29718)
  chore:  device management improve readability and performance (#29712)
  chore: throttle exceptions counter increment (#29719)
  chore: Improve performance of getActiveLocalUserCount (#29681)
  Release 6.2.9
  ci: update EE license (#29684)
  ci: Add missing `context` property on compose file (#29676)
  chore: add change stream option to get full doc (#29627)
  fix(meteor): Message composer keeps recording even though permission was denied (#29526)
  fix(meteor): Video Record button disabled on iOS browsers (#29649)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants