Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CreateConnections Only Slows Down the Server #3188

Closed
bailejl opened this issue Jul 22, 2015 · 3 comments
Closed

Using CreateConnections Only Slows Down the Server #3188

bailejl opened this issue Jul 22, 2015 · 3 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@bailejl
Copy link

bailejl commented Jul 22, 2015

I am trying to restrict model access to one or more connections with the hope of limiting access based on user permissions. I have the code working, but when I remove the mongoose.connect, it slows everything down. The times are in the 20-50 seconds on my local desktop in development mode. However, If I add mongoose.connect, everything is under a second. Any help is appreciated.

db.js

"use strict";

var mongoose = require('mongoose');
var logger = require('winston');
var nconf = require('nconf');


var dbUrl = nconf.get("database:acl:url");

mongoose.connect(dbUrl, function (err) {
    if (err) {
        logger.error('Error connecting to DB from mongoose', err);
        throw err;
    }
    logger.info('Mongoose is connected to ' + dbUrl);
});

module.exports = mongoose;

var hostReadOnly = nconf.get("database:read-only:host");
var databaseReadOnly = nconf.get("database:read-only:database");
var portReadOnly = nconf.get("database:read-only:port");
var optionsReadOnly = nconf.get("database:read-only:options");

var readOnlyConnection = mongoose.createConnection(hostReadOnly, databaseReadOnly, portReadOnly, optionsReadOnly);
readOnlyConnection.on('error', function (err) {
    if (err) {
        logger.error('Error with Read-Only DB from mongoose', err);
        throw err;
    }
});
readOnlyConnection.on('connected', function(err){
    if (err) {
        logger.error('Error connecting with Read-Only DB from mongoose', err);
        throw err;
    }

    logger.info('Mongoose is connected to Read-Only DB via ' + hostReadOnly + ':' + portReadOnly +
        '/' + databaseReadOnly);
});


var hostReadWrite = nconf.get("database:read-write:host");
var databaseReadWrite = nconf.get("database:read-write:database");
var portReadWrite = nconf.get("database:read-write:port");
var optionsReadWrite = nconf.get("database:read-write:options");
var readWriteConnection = mongoose.createConnection(hostReadWrite, databaseReadWrite, portReadWrite, optionsReadWrite);
readWriteConnection.on('error', function (err) {
    if (err) {
        logger.error('Error with Read-Write DB from mongoose', err);
        throw err;
    }
});
readWriteConnection.on('connected', function(err){
    if (err) {
        logger.error('Error connecting with Read-Write DB from mongoose', err);
        throw err;
    }
    logger.info('Mongoose is connected to Read-Write DB via ' + hostReadWrite + ':' + portReadWrite + '/' +
        databaseReadWrite );
});

var hostAdmin = nconf.get("database:admin:host");
var databaseAdmin = nconf.get("database:admin:database");
var portAdmin = nconf.get("database:admin:port");
var optionsAdmin = nconf.get("database:admin:options");
var adminConnection = mongoose.createConnection(hostAdmin, databaseAdmin, portAdmin, optionsAdmin);
adminConnection.on('error', function (err) {
    if (err) {
        logger.error('Error with Admin DB from mongoose', err);
        throw err;
    }
});
adminConnection.on('connected', function(err){
    if (err) {
        logger.error('Error connecting with Admin DB from mongoose', err);
        throw err;
    }
    logger.info('Mongoose is connected to Admin DB via ' + hostAdmin + ':' + portAdmin + '/' +
        databaseAdmin );
});

module.exports.readOnlyConnection = readOnlyConnection;
module.exports.readWriteConnection = readWriteConnection;
module.exports.adminConnection = adminConnection;

permission.js

var mongoose = require('mongoose');
var createInfo = require('./plugins/creationInfo');
var modifiedOn = require('./plugins/modifiedOn');
var versionInfo = require('./plugins/versionInfo');
var db = require('../db');

var schema = new mongoose.Schema({
        name: { type: String, required: true },
        description: { type: String, required: false }
    }
);

schema.plugin(modifiedOn);
schema.plugin(createInfo);
schema.plugin(versionInfo);

db.readOnlyConnection.model('Permission', schema);
db.readWriteConnection.model('Permission', schema);
db.adminConnection.model('Permission', schema);

module.exports.schema = schema;

I access the model with the following code.

var db = require('../db'),
    User = db.readOnlyConnection.model('User');
@vkarpov15
Copy link
Collaborator

What times are you measuring?

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jul 22, 2015
@bailejl
Copy link
Author

bailejl commented Jul 22, 2015

@vkarpov15 I appreciate you tagging it with help and question, but I found the issue. Express in our code is using the default Mongoose connection to store session info, which was causing the problem. After creating its own connection, the times went back to normal. I hope people can benefit from this problem.

@bailejl bailejl closed this as completed Jul 22, 2015
@vkarpov15
Copy link
Collaborator

I'd recommend you use connect-mongodb-session for storage instead of connect-mongo: https://github.com/mongodb-js/connect-mongodb-session . I've seen far too many problems with the connection pool reuse with connect-mongo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants