mirror of
https://activitypub.software/TransFem-org/Sharkey
synced 2024-11-24 23:15:13 +00:00
merge: thunk the min/max promises (!603)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/603 Approved-by: Marie <github@yuugi.dev> Approved-by: fEmber <acomputerdog@gmail.com>
This commit is contained in:
commit
9cf40ef452
1 changed files with 15 additions and 7 deletions
|
@ -37,8 +37,13 @@ export class RateLimiterService {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// those lines with the "wrong" brace style / indentation are
|
||||||
|
// done that way so that the *other* lines stay identical to
|
||||||
|
// Misskey, simplifying merges
|
||||||
|
|
||||||
// Short-term limit
|
// Short-term limit
|
||||||
const min = new Promise<void>((ok, reject) => {
|
// eslint-disable-next-line brace-style
|
||||||
|
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 +62,18 @@ 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
// eslint-disable-next-line brace-style
|
||||||
|
}); };
|
||||||
|
|
||||||
// Long term limit
|
// Long term limit
|
||||||
const max = new Promise<void>((ok, reject) => {
|
// eslint-disable-next-line brace-style
|
||||||
|
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 +94,8 @@ export class RateLimiterService {
|
||||||
return ok();
|
return ok();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
// eslint-disable-next-line brace-style
|
||||||
|
}); };
|
||||||
|
|
||||||
const hasShortTermLimit = typeof limitation.minInterval === 'number';
|
const hasShortTermLimit = typeof limitation.minInterval === 'number';
|
||||||
|
|
||||||
|
@ -96,9 +104,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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue