I have a web server which takes XML files and store them at a SQL Server databse.
I am using multer middleware to recive the files and MSSQL package to save the XML content to the database.
I want each user to be able to send multiple files at once (many at once, like a 1000 or more).
The problem is that when a user send the files it cosumes all the pools specified on the mssql config and if another user send more files, it will have to wait until the first user is done sending all the files.
I was hoping to share the pools between all users sending files avoiding the starvation so they can send files simultaneously.
My mssql config looks like this:
{
"user": "username",
"password": "xxxxxx",
"database": "database_name",
"server": "host.host",
"dialect": "mssql",
"encrypt": true,
"dialectOptions": {
"encrypt": true
},
"pool": {
"max": 5,
"min": 0,
"idle": 10000,
"acquire": 40000,
}
}
My mssql connection file:
const mssql = require('mssql');
const pool = new mssql.ConnectionPool(require('config'));
pool.connect(err => {if (err) throw err});
module.exports = pool;
My controller file:
const fs = require('fs');
const async = require('async');
const mssql = require('@mssql');
exports.recive_xml_files = async (req, res) => {
const files = req.files;
const empresa = req.body.empresa;
let cnpj = empresa.split(':')[1];
async.each(files, (file, callback) => {
saveXmlDb(file, cnpj).then(() => {
callback();
});
}, (err) => {
let status = 200;
if (err) status = 500;
res.sendStatus(status);
});
};
const saveXmlDb = (file, cnpj) => {
return new Promise((resolve, reject) => {
fs.readFile(file.path, (err, data) => {
if (err) reject("Não foi possível ler o arquivo XML.");
let sXml = data.toString();
mssql.query(`INSERT into Arquivos_XML (Conteudo, Origem_Id) VALUES ('${sXml}', 6)`).then(res => {
console.log('SAAVING FOR THE FOLLOWING CNPJ------------>', cnpj);
resolve();
}).catch(err => {
reject();
});
});
});
}
Could anyone help me to achieve this?
Thank you.
Aucun commentaire:
Enregistrer un commentaire