thunk the min/max promises

this prevents generating promises we don't use, which would get logged
by the node vm and spam the logs
This commit is contained in:
dakkar 2024-08-19 19:13:37 +01:00
parent f5560783ea
commit 1b5bedc1d0

View file

@ -38,7 +38,7 @@ export class RateLimiterService {
} }
// Short-term limit // Short-term limit
const min = new Promise<void>((ok, reject) => { const minP = () => { return new Promise<void>((ok, reject) => {
const minIntervalLimiter = new Limiter({ const minIntervalLimiter = new Limiter({
id: `${actor}:${limitation.key}:min`, id: `${actor}:${limitation.key}:min`,
duration: limitation.minInterval! * factor, duration: limitation.minInterval! * factor,
@ -57,16 +57,16 @@ export class RateLimiterService {
return reject({ code: 'BRIEF_REQUEST_INTERVAL', info }); return reject({ code: 'BRIEF_REQUEST_INTERVAL', info });
} else { } else {
if (hasLongTermLimit) { if (hasLongTermLimit) {
return max.then(ok, reject); return maxP().then(ok, reject);
} else { } else {
return ok(); return ok();
} }
} }
}); });
}); }) };
// Long term limit // Long term limit
const max = new Promise<void>((ok, reject) => { const maxP = () => { return new Promise<void>((ok, reject) => {
const limiter = new Limiter({ const limiter = new Limiter({
id: `${actor}:${limitation.key}`, id: `${actor}:${limitation.key}`,
duration: limitation.duration! * factor, duration: limitation.duration! * factor,
@ -87,7 +87,7 @@ export class RateLimiterService {
return ok(); return ok();
} }
}); });
}); }) };
const hasShortTermLimit = typeof limitation.minInterval === 'number'; const hasShortTermLimit = typeof limitation.minInterval === 'number';
@ -96,9 +96,9 @@ export class RateLimiterService {
typeof limitation.max === 'number'; typeof limitation.max === 'number';
if (hasShortTermLimit) { if (hasShortTermLimit) {
return min; return minP();
} else if (hasLongTermLimit) { } else if (hasLongTermLimit) {
return max; return maxP();
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }