rewrite counter inrease queue
This commit is contained in:
parent
747bbced92
commit
922e5ecb5b
|
@ -180,19 +180,10 @@ class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
incrementPixelcount(amount = 1) {
|
incrementPixelcount(amount = 1) {
|
||||||
if (!this.id) {
|
const { id } = this;
|
||||||
return;
|
if (id) {
|
||||||
|
incrementPixelcount(id, amount);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* incrementation gets queued to be processed
|
|
||||||
* in batches
|
|
||||||
*/
|
|
||||||
if (!this.queuedPxlIncrement) {
|
|
||||||
this.queuedPxlIncrement = amount;
|
|
||||||
incrementPixelcount(this);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.queuedPxlIncrement += amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTotalPixels() {
|
async getTotalPixels() {
|
||||||
|
|
|
@ -208,58 +208,50 @@ export async function getNamesToIds(ids) {
|
||||||
* increment user pixelcount in batches sequentially
|
* increment user pixelcount in batches sequentially
|
||||||
* Queue directly accesses queuedPxlIncrement in user object
|
* Queue directly accesses queuedPxlIncrement in user object
|
||||||
*/
|
*/
|
||||||
const incrementQueue = [];
|
let incrementQueue = {};
|
||||||
let pushLoop = null;
|
let pushLoop = null;
|
||||||
const incrementLoop = async () => {
|
const incrementLoop = async () => {
|
||||||
if (!incrementQueue.length) {
|
const idKeys = Object.keys(incrementQueue);
|
||||||
|
if (!idKeys.length) {
|
||||||
pushLoop = null;
|
pushLoop = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
const queue = incrementQueue;
|
||||||
incrementQueue.sort((a, b) => {
|
incrementQueue = {};
|
||||||
const aa = a.queuedPxlIncrement;
|
const orderedCnts = {};
|
||||||
const ba = b.queuedPxlIncrement;
|
idKeys.forEach((id) => {
|
||||||
if (aa < ba) return -1;
|
const cnt = queue[id];
|
||||||
if (aa > ba) return 1;
|
const cntArr = orderedCnts[cnt];
|
||||||
return 0;
|
if (cntArr) {
|
||||||
});
|
cntArr.push(id);
|
||||||
let cnt = incrementQueue.length;
|
} else {
|
||||||
let user = incrementQueue.pop();
|
orderedCnts[cnt] = [id];
|
||||||
while (cnt) {
|
}
|
||||||
cnt -= 1;
|
});
|
||||||
const amount = user.queuedPxlIncrement;
|
Object.keys(orderedCnts).forEach(async (cnt) => {
|
||||||
user.queuedPxlIncrement = 0;
|
const ids = orderedCnts[cnt];
|
||||||
const ids = [user.id];
|
try {
|
||||||
while (cnt) {
|
|
||||||
cnt -= 1;
|
|
||||||
user = incrementQueue.pop();
|
|
||||||
if (user.queuedPxlIncrement !== amount) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
user.queuedPxlIncrement = 0;
|
|
||||||
ids.push(user.id);
|
|
||||||
}
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await RegUser.increment(['totalPixels', 'dailyTotalPixels'], {
|
await RegUser.increment(['totalPixels', 'dailyTotalPixels'], {
|
||||||
by: amount,
|
by: cnt,
|
||||||
where: {
|
where: {
|
||||||
id: ids,
|
id: ids,
|
||||||
},
|
},
|
||||||
silent: true,
|
silent: true,
|
||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(`Error on pixel increment: ${err.message}`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
logger.warn(`Error on pixel increment: ${err.message}`);
|
|
||||||
incrementQueue.forEach((q) => {
|
|
||||||
if (q) q.queuedPxlIncrement = 0;
|
|
||||||
});
|
|
||||||
incrementQueue.length = 0;
|
|
||||||
}
|
|
||||||
pushLoop = setTimeout(incrementLoop, 250);
|
pushLoop = setTimeout(incrementLoop, 250);
|
||||||
};
|
};
|
||||||
export async function incrementPixelcount(user) {
|
export async function incrementPixelcount(id, by) {
|
||||||
incrementQueue.push(user);
|
if (incrementQueue[id]) {
|
||||||
|
incrementQueue[id] += by;
|
||||||
|
} else {
|
||||||
|
incrementQueue[id] = by;
|
||||||
|
}
|
||||||
if (!pushLoop) {
|
if (!pushLoop) {
|
||||||
pushLoop = setTimeout(incrementLoop, 0);
|
pushLoop = setTimeout(incrementLoop, 0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user