Mongoose stops responding after an indefinite period of time

I was very tearing my hair over this problem since I started to develop my project (12 months ago!), Always believing that I would find an answer before I was ready for release ... Unfortunately, this was not the case!

Basically, I have a pretty simple node.js server running on Azure connecting to the MongoLab database (now MLab) using mongoose.

The connection code is as follows:

// Connect to DB
//mongoose.set('debug', true);
mongoose.connect(envConfig.app.db, {
    server: {
        auto_reconnect: true,
        socketOptions: {
            keepAlive: 1,
            connectTimeoutMS: 30000,
            socketTimeoutMS : 30000,
        }
    },
    replset: {
        auto_reconnect: true,
        socketOptions: {
            keepAlive: 1,
            connectTimeoutMS: 30000,
            socketTimeoutMS : 30000,
        }
    }
}, function (err) {
    if (err) winstonLogger.error(err);
});
mongoose.connection.on('connecting', function () {
    console.log('Connecting to MongoDB...');
});
mongoose.connection.on('connected', function () {
    console.log('MongoDB connected!');
});
mongoose.connection.on('open', function () {
    console.log('MongoDB connection opened!');
});
mongoose.connection.on('error', function (err) {
    console.error('Error in MongoDb connection: ' + err.stack);
    winstonLogger.error(err);
    mongoose.disconnect();
});
mongoose.connection.on('disconnected', function () {
    winstonLogger.error('MongoDB disconnected!');
    mongoose.connect(envConfig.app.db, {
        server: {
            auto_reconnect: true,
            socketOptions: {
                keepAlive: 1,
                connectTimeoutMS: 30000,
                socketTimeoutMS : 30000,
            }
        },
        replset: {
            auto_reconnect: true,
            socketOptions: {
                keepAlive: 1,
                connectTimeoutMS: 30000,
                socketTimeoutMS : 30000,
            }
        }
    });
});
mongoose.connection.on('reconnected', function () {
    console.log('MongoDB reconnected!');
});
mongoose.connection.on('close', function () {
    console.log('MongoDB closed');
});

All the additional timeout options, etc. that you see are part of my attempts to fix this problem (to no avail).

Here is a typical query:

AccessToken.findOne({ token: token })
            .maxTime(10000)
            .exec(function (err, accessToken) {
                // If I even got to here I would be happy
                if (err) return done(err);
                // If I could consistently get to here, my project would be finished and I would enjoy being alive again
            });

, , . ... , , . - , ... . , . . - , - , . -, , , - :

POST /api/auth/verify - - ms - -

, - , db ( - ) .

, , , , ... ... . Azure MongoLab. . , Azure , . , Mongoose , maxTime...

MongoLab , , : http://docs.mlab.com/connecting/#known-issues, , , , , , .

, , .

- , ...

!

+4
3

, ( NodeJs Azure, Mongoose, MongoLab). " , ". , , ...

, . Azure , MongoLab, Mongolab ( , NodeJs MongoDB , - ).

, Mongoose , . - - , Mongoose, mongodb-core.

, , - Mongoose . , , , .

, !

My Mongoose:

// Options
mongooseOptions.replset = {
  ha: true, // Make sure the high availability checks are on
  haInterval: 5000, // Run every 5 seconds
  socketOptions : {
    ... // nothing special here
  }
}

mongoose.connect(mongodb_uri, mongooseOptions);

// Make sure the high availability checks are occuring
// Should see "replset ha start" "replset ha end" pairs every 5 secs
// Well, hopefully.
if (mongoose.connection.db.serverConfig.s.replset) {
  mongoose.connection.db.serverConfig.s.replset.on('ha', function(type, data) {
    console.log('replset ha ' + type);
  })
}
+1

, - . , , ha ( ), haInterval, , , .js.

:

// Connect to DB
var connectionOptions = {
    replset: {
        socketOptions: {
            connectTimeoutMS: 300000, // 5 minutes
            keepAlive: 120
        },
        ha: true, // Make sure the high availability checks are on
        haInterval: 10000, // Run every 10 seconds
    }
};
//mongoose.set('debug', true);
mongoose.connect(envConfig.app.db, connectionOptions, function (err) {
    if (err) winstonLogger.error(err);
});
mongoose.connection.on('connecting', function () {
    console.log('Connecting to MongoDB...');
});
mongoose.connection.on('connected', function () {
    console.log('MongoDB connected!');
});
mongoose.connection.on('open', function () {
    console.log('MongoDB connection opened!');
});
mongoose.connection.on('error', function (err) {
    winstonLogger.error(err);
    mongoose.disconnect();
});
mongoose.connection.on('disconnected', function () {
    winstonLogger.error('MongoDB disconnected!');
    mongoose.connect(envConfig.app.db, connectionOptions, function (err) {
        if (err) winstonLogger.error(err);
    });
});
mongoose.connection.on('reconnected', function () {
    console.log('MongoDB reconnected!');
});
mongoose.connection.on('close', function () {
    winstonLogger.error('MongoDB closed');
});
if (mongoose.connection.db.serverConfig.s.replset) {
    mongoose.connection.db.serverConfig.s.replset.on('ha', function(type, data) {
        console.log('replset ha ' + type);
    });
    mongoose.connection.db.serverConfig.s.replset.on('timeout', function () {
        winstonLogger.error('MongoDB timeout');
    });
}

AlwaysOn Azure WebApp (, , , , !).

, , 100% .

, - , ... , , , ( , !).

, , :)

+3

, , mongodb Mlab, , - -, webapp .

, mongoose, @BlakesSeven. . Connection pools Mongoose Connections.

0

All Articles