I am using node cluster
module, and each worker loads a database connection.
index.js
const cluster = require('cluster');
const database = require('./db.js');
if (cluster.isMaster) {
cluster.fork();
cluster.fork();
} else {...}
db.js
const mysql = require('mysql');
const pool = new mysql.pool(config);
module.exports = function(query){
return pool.query(query);
}
My understanding is that each time a worker is spawned, it will initialize db.js
, which will create a new pool/connections to mysql.
Is there another way to structure this so that all workers share the same mysql pool?