Files
pirate-corsaire-server/Worker/Statistics.js
T
2026-08-24 22:20:36 +02:00

89 lines
2.1 KiB
JavaScript

const { parentPort } = require("worker_threads");
const { MongoClient } = require("mongodb");
let mongoDB = null;
let collectionRewards = null;
let collectionFeatures = null
async function connect() {
const client = new MongoClient('mongodb://localhost:27017');
await client.connect({
maxPoolSize: 100,
minPoolSize: 20,
maxConnecting: 20,
waitQueueTimeoutMS: 0,
socketTimeoutMS: 30000,
connectTimeoutMS: 10000,
retryWrites: false,
directConnection: true
});
return client.db("test");
};
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
(async function() {
mongoDB = await connect();
collectionRewards = mongoDB.collection("Rewards");
collectionFeatures = mongoDB.collection("Features");
})();
const rewardsBatch = [];
const featuresBatch = [];
const fns = {
1: async function (data) {
rewardsBatch.push(data);
},
2: function (data) {
featuresBatch.push(data);
},
3: async function (data) {
const n = performance.now();
await collectionFeatures.insertMany(data, {
ordered: false,
writeConcern: { w: 0 },
bypassDocumentValidation: true
});
console.info("Written!", performance.now() - n);
}
};
const queue = [];
parentPort.on("message", data => {
const fn = fns[data.id];
if (!fn) return;
queue.push(data);
});
setInterval(async () => {
const n = performance.now();
for (let i = 0; i < queue.length; i++) {
const item = queue[i];
fns[item.id](item.data);
};
queue.length = 0;
if (rewardsBatch.length)
await collectionRewards.insertMany(rewardsBatch, {
ordered: false,
writeConcern: { w: 0 },
bypassDocumentValidation: true
});
if (featuresBatch.length)
await collectionFeatures.insertMany(featuresBatch, {
ordered: false,
writeConcern: { w: 0 },
bypassDocumentValidation: true
});
rewardsBatch.length = 0;
featuresBatch.length = 0;
}, 60000);