I have a nodejs/express backend api that when I get hundreds of requests within seconds I start to get mixed results where data is crossed between requests and leads to unexpected and incorrected results. Can someone point me to where I am incorrectly defining variables that when I get one of the await functions it causes the other variables to be overwritten with the next requests data and causing my issues.
router.post('/autoscan3', async(req,res,next) => {
let authrule = "autoscan"
console.log(req.body.companyDomain + " " + req.body.user + " for folder: " + req.body.folderid)
let totalscans = 0;
let client = await getDB();
let authorizationResults = await checkAuthorization(client, req.body.companyDomain);
const featureSet = authorizationResults.features;
let company = authorizationResults.company;
const clientid = authorizationResults.client_id;
const clientsecret = authorizationResults.secret;
let tenantid = await getTenantID(req.body.companyDomain, client);
let token = await msgraphservices.getClientCredentialsAccessToken(tenantid, clientid, clientsecret)
let scanResults = []
let folderscans = 0;
try{
for(let a = 0; a < req.body.messages.length; a++){
let senderAddress = req.body.messages[a].senderAddress;
let emailBody = req.body.messages[a].emailBody;
let messageid = req.body.messages[a].messageid;
let receivedDateTime = req.body.messages[a].receivedDateTime;
let user = req.body.messages[a].recieverAddress;
let subject = req.body.messages[a].subject;
let attachments = req.body.messages[a].attachments;
let links = req.body.messages[a].emailBodyLinks;
let headers = req.body.messages[a].headers
let knownaddress
if (senderAddress.includes(req.body.companyDomain)) {
knownaddress = 10
} else {
knownaddress = await searchForSender(client, senderAddress, req.body.user, token, "");}
let assessmentResults = await assessment(
messageid,
emailBody,
senderAddress,
user,
subject,
receivedDateTime,
company,
attachments,
links,
req.body.companyDomain,
client,
headers,
knownaddress,
featureSet,
authrule
)
console.log('adding to folderscans')
folderscans++
try {
await msgraphservices.updateUserCategory2(messageid, req.body.user, req.body.folderid, assessmentResults.riskFactor,token)
}
catch(e) {
console.log(`error on category tag for ${messageid} with user ${req.body.user}`);
console.log(e);
}
}
console.log(`folder scans ${folderscans}`);
totalscans = totalscans + folderscans
return res.status(200).json({status:"success", totalscans:totalscans});
}
catch(e) {
console.log(`error while trying to loop for user ${req.body.user}`)
console.log(e)
logapierror(e.stack, req.body.user, req.body)
return res.status(200).json({status:'error', totalscans:totalscans, error:e});
}
});
It's very likely let client = await getDB();
If multiple requests use the same snapshot of the database but don't know about each other, it's very likely that they're overwriting each other's data.