core: judge: add concurrency control to task consumer (#719)

Co-authored-by: undefined <i@undefined.moe>
This commit is contained in:
Yang Gaoandundefined authored and GitHub committed 2024-01-28 01:23:05 +08:00
1 parent 8b10f51430
commit 6b759cbe63
9 files changed
+130 -83

No files matched your search

+16 -3
View File
@@ -253,6 +253,8 @@ class RecordMainConnectionHandler extends ConnectionHandler {
pretest = false;
tdoc: Tdoc;
applyProjection = false;
queue: Map<string, () => Promise<any>> = new Map();
throttleQueueClear: () => void;
@param('tid', Types.ObjectId, true)
@param('pid', Types.ProblemId, true)
@@ -301,6 +303,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
this.checkPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN);
this.allDomain = true;
}
this.throttleQueueClear = throttle(this.queueClear, 100, { trailing: true });
}
async message(msg: { rids: string[] }) {
@@ -338,15 +341,25 @@ class RecordMainConnectionHandler extends ConnectionHandler {
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)) pdoc = null;
}
if (this.applyProjection && typeof rdoc.input !== 'string') rdoc = contest.applyProjection(tdoc, rdoc, this.user);
if (this.pretest) this.send({ rdoc: omit(rdoc, ['code', 'input']) });
if (this.pretest) this.queueSend(rdoc._id.toHexString(), async () => ({ rdoc: omit(rdoc, ['code', 'input']) }));
else {
this.send({
this.queueSend(rdoc._id.toHexString(), async () => ({
html: await this.renderHTML('record_main_tr.html', {
rdoc, udoc, pdoc, tdoc, allDomain: this.allDomain,
}),
});
}));
}
}
queueSend(rid: string, fn: () => Promise<any>) {
this.queue.set(rid, fn);
this.throttleQueueClear();
}
async queueClear() {
await Promise.all([...this.queue.values()].map(async (fn) => this.send(await fn())));
this.queue.clear();
}
}
class RecordDetailConnectionHandler extends ConnectionHandler {