mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
core: handle sync delay in database cluster
This commit is contained in:
4 files changed
+16
-12
No files matched your search
@@ -925,14 +925,14 @@ export class ProblemSolutionHandler extends ProblemDetailHandler {
|
||||
|
||||
@param('psid', Types.ObjectId)
|
||||
async postUpvote(domainId: string, psid: ObjectId) {
|
||||
const [psdoc, pssdoc] = await solution.vote(domainId, psid, this.user._id, 1);
|
||||
this.back({ vote: psdoc.vote, user_vote: pssdoc.vote });
|
||||
const psdoc = await solution.vote(domainId, psid, this.user._id, 1);
|
||||
this.back({ vote: psdoc.vote, user_vote: 1 });
|
||||
}
|
||||
|
||||
@param('psid', Types.ObjectId)
|
||||
async postDownvote(domainId: string, psid: ObjectId) {
|
||||
const [psdoc, pssdoc] = await solution.vote(domainId, psid, this.user._id, -1);
|
||||
this.back({ vote: psdoc.vote, user_vote: pssdoc.vote });
|
||||
const psdoc = await solution.vote(domainId, psid, this.user._id, -1);
|
||||
this.back({ vote: psdoc.vote, user_vote: -1 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -299,7 +299,7 @@ export async function setStatus<K extends keyof DocStatusType>(
|
||||
): Promise<DocStatusType[K]> {
|
||||
return await collStatus.findOneAndUpdate(
|
||||
{ domainId, docType, docId, uid },
|
||||
{ $set: args, $inc: { counter: 1 } },
|
||||
{ $set: args },
|
||||
{
|
||||
upsert: true,
|
||||
returnDocument,
|
||||
|
||||
@@ -78,12 +78,9 @@ class SolutionModel {
|
||||
);
|
||||
let inc = value;
|
||||
if (before?.vote) inc -= before.vote;
|
||||
return [
|
||||
inc
|
||||
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
|
||||
: doc,
|
||||
await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid),
|
||||
];
|
||||
return inc
|
||||
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
|
||||
: doc;
|
||||
}
|
||||
|
||||
static async getListStatus(domainId: string, psids: ObjectId[], uid: number) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import pwhash from '../lib/hash.hydro';
|
||||
import bus from '../service/bus';
|
||||
import db from '../service/db';
|
||||
import { Value } from '../typeutils';
|
||||
import { ArgMethod, buildProjection, randomstring } from '../utils';
|
||||
import { ArgMethod, buildProjection, randomstring, sleep } from '../utils';
|
||||
import { PERM, PRIV } from './builtin';
|
||||
import domain from './domain';
|
||||
import * as setting from './setting';
|
||||
@@ -346,6 +346,13 @@ class UserModel {
|
||||
{ $set: { join: true } },
|
||||
{ upsert: true },
|
||||
);
|
||||
// make sure user is immediately available after creation
|
||||
// give some time for database to sync in replicas
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
const udoc = await UserModel.getById('system', uid); // eslint-disable-line no-await-in-loop
|
||||
if (udoc) break;
|
||||
await sleep(500); // eslint-disable-line no-await-in-loop
|
||||
}
|
||||
return uid;
|
||||
} catch (e) {
|
||||
if (e?.code === 11000) {
|
||||
|
||||
Reference in new issue
Block a user