mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-22 08:26:16 -04:00
pad_utils: Rate limit identical warnDeprecated
calls
This commit is contained in:
parent
908175d1ca
commit
cfb68e5725
2 changed files with 32 additions and 0 deletions
|
@ -93,6 +93,9 @@ const padutils = {
|
|||
* Prints a warning message followed by a stack trace (to make it easier to figure out what code
|
||||
* is using the deprecated function).
|
||||
*
|
||||
* Identical deprecation warnings (as determined by the stack trace, if available) are rate
|
||||
* limited to avoid log spam.
|
||||
*
|
||||
* Most browsers include UI widget to examine the stack at the time of the warning, but this
|
||||
* includes the stack in the log message for a couple of reasons:
|
||||
* - This makes it possible to see the stack if the code runs in Node.js.
|
||||
|
@ -106,6 +109,18 @@ const padutils = {
|
|||
const err = new Error();
|
||||
if (Error.captureStackTrace) Error.captureStackTrace(err, padutils.warnDeprecated);
|
||||
err.name = '';
|
||||
// Rate limit identical deprecation warnings (as determined by the stack) to avoid log spam.
|
||||
if (typeof err.stack === 'string') {
|
||||
if (padutils.warnDeprecated._rl == null) {
|
||||
padutils.warnDeprecated._rl =
|
||||
{prevs: new Map(), now: () => Date.now(), period: 10 * 60 * 1000};
|
||||
}
|
||||
const rl = padutils.warnDeprecated._rl;
|
||||
const now = rl.now();
|
||||
const prev = rl.prevs.get(err.stack);
|
||||
if (prev != null && now - prev < rl.period) return;
|
||||
rl.prevs.set(err.stack, now);
|
||||
}
|
||||
if (err.stack) args.push(err.stack);
|
||||
(padutils.warnDeprecated.logger || console).warn(...args);
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue