core: fix limit_rate

This commit is contained in:
undefined committed 2021-04-22 13:48:24 +08:00
1 parent 4c77bf9d05
commit f42393f7af
3 files changed
+9 -6

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.23.19", "version": "2.23.20",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+7 -3
View File
@@ -10,18 +10,22 @@ export async function inc(op: string, ident: string, periodSecs: number, maxOper
const expireAt = new Date(beginAt.getTime() + periodSecs * 1000); const expireAt = new Date(beginAt.getTime() + periodSecs * 1000);
try { try {
await coll.findOneAndUpdate({ await coll.findOneAndUpdate({
op,
ident, ident,
beginAt, beginAt,
expireAt, expireAt,
op: { $lt: maxOperations }, opcount: { $lt: maxOperations },
}, { $inc: { op: 1 } }, { upsert: true }); }, { $inc: { opcount: 1 } }, { upsert: true });
} catch (e) { } catch (e) {
throw new OpcountExceededError(op, periodSecs, maxOperations); throw new OpcountExceededError(op, periodSecs, maxOperations);
} }
} }
function ensureIndexes() { function ensureIndexes() {
return coll.createIndex('expireAt', { expireAfterSeconds: 0 }); return Promise.all([
coll.createIndex('expireAt', { expireAfterSeconds: 0 }),
coll.createIndex({ op: 1, ident: 1 }, { unique: true }),
]);
} }
bus.once('app/started', ensureIndexes); bus.once('app/started', ensureIndexes);
+1 -2
View File
@@ -296,7 +296,6 @@ export class HandlerCommon {
async limitRate(op: string, periodSecs: number, maxOperations: number) { async limitRate(op: string, periodSecs: number, maxOperations: number) {
if (this.user && this.user.hasPriv(PRIV.PRIV_UNLIMITED_ACCESS)) return; if (this.user && this.user.hasPriv(PRIV.PRIV_UNLIMITED_ACCESS)) return;
logger.info(op, periodSecs, maxOperations, this.request.ip);
await opcount.inc(op, this.request.ip, periodSecs, maxOperations); await opcount.inc(op, this.request.ip, periodSecs, maxOperations);
} }
@@ -484,7 +483,7 @@ export class Handler extends HandlerCommon {
} }
async init({ domainId }) { async init({ domainId }) {
if (!argv.benchmark) await this.limitRate('global', 10, 88); if (!argv.benchmark) await this.limitRate('global', 10, 1);
const [absoluteDomain, inferDomain] = await Promise.all([ const [absoluteDomain, inferDomain] = await Promise.all([
domain.get(domainId), domain.get(domainId),
domain.getByHost(this.request.host), domain.getByHost(this.request.host),