fix race conditions

This commit is contained in:
HF 2022-07-01 23:45:25 +02:00
parent b9cb6afc0b
commit 6ad79250e5
3 changed files with 42 additions and 1 deletions

View File

@ -22,6 +22,28 @@ export function setCoolDownFactor(fac) {
coolDownFactor = fac;
}
/*
* IPs who are currently requesting pixels
* (have to log in order to avoid race conditions)
*/
const curReqIPs = new Map();
setInterval(() => {
// clean up old data
const ts = Date.now() - 20 * 1000;
const ips = [...curReqIPs.keys()];
for (let i = 0; i < ips.length; i += 1) {
const ip = ips[i];
const limiter = curReqIPs.get(ip);
if (limiter && ts > limiter) {
curReqIPs.delete(ip);
logger.warn(
`Pixel requests from ${ip} got stuck`,
);
}
}
}, 20 * 1000);
/**
*
* By Offset is prefered on server side
@ -47,10 +69,20 @@ export async function drawByOffsets(
let retCode = 0;
let pxlCnt = 0;
let rankedPxlCnt = 0;
const { ip } = user;
try {
const startTime = Date.now();
if (curReqIPs.has(ip)) {
// already setting a pixel somewhere
logger.warn(
`Got simultanious requests from ${user.ip}`,
);
throw new Error(13);
}
curReqIPs.set(ip, startTime);
const canvas = canvases[canvasId];
if (!canvas) {
// canvas doesn't exist
@ -182,6 +214,10 @@ export async function drawByOffsets(
}
}
if (retCode !== 13) {
curReqIPs.delete(ip);
}
if (pxlCnt) {
user.setWait(wait, canvasId);
if (rankedPxlCnt) {

View File

@ -99,9 +99,9 @@ class SocketClient extends EventEmitter {
if (this.canvasId !== null) {
this.ws.send(RegisterCanvas.dehydrate(this.canvasId));
}
this.processMsgQueue();
console.log(`Register ${chunks.length} chunks`);
this.ws.send(RegisterMultipleChunks.dehydrate(chunks));
this.processMsgQueue();
}
setCanvas(canvasId) {

View File

@ -246,6 +246,11 @@ export function receivePixelReturn(
errorTitle = t`Not allowed`;
msg = t`Just the Top10 of yesterday can place here`;
break;
case 13:
errorTitle = t`You are weird`;
// eslint-disable-next-line max-len
msg = t`Server got confused by your pixels. Are you playing on multiple devices?`;
break;
default:
errorTitle = t`Weird`;
msg = t`Couldn't set Pixel`;