How can we configure query timeouts? We're running into intermittent cases of queries not calling their callback in production. It doesn't look like the database is locked up, but we'd like to set up timeouts so that we can both get better diagnostics and clean up resources when timeouts do occur (e.g. respond to HTTP request that trigger the query and clean up driver state like returning the connection to the pool for others to use).
We're using version 1.3.0 of the driver against a 2.4.3 database. I wrote a little test rig below and simulated a hang using fsyncLock() and a write. When the write is attempted, queries obviously start blocking. My setTimeout() triggers and I get the message that the query took longer than 10s, but even after several minutes I don't get an error back from the driver to any of the callbacks I've registered. When I fsyncUnlock() both the pending write from the shell and the pending query from the test driver complete without error.
So, how do I pass a timeout to the driver? I feel like I must be missing something obvious.
Merci,
Adrien
---
Test driver.
var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://timeout:timeout@localhost/timeout', { server: { socketOptions: { socketTimeoutMS: 5000 } } }, function (err, db) {
if (err) {
console.log(err);
} else {
function test() {
var timeout = setTimeout(function () {
console.log('MongoDB call took longer than 10s');
}, 10000);
db.collection('stuff', function (err, c) {
if (err) {
clearTimeout(timeout);
console.log(err);
setTimeout(test, 1000);
} else {
c.find().toArray(function (err, stuff) {
clearTimeout(timeout);
if (err) {
console.log(err);
} else {
console.log('stuff = ' + JSON.stringify(stuff));
}
setTimeout(test, 1000);
});
}
});
}
test();
}
});
---
From the mongo shell.
cluster:PRIMARY> db.fsyncLock();
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"seeAlso" : "
http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
# in another shell
cluster:PRIMARY> db.stuff.save({foo:'bar'})
# back in the original shell
cluster:PRIMARY> db.fsyncUnlock();
{ "ok" : 1, "info" : "unlock completed" }
---
Log output from the test driver.
stuff = []
stuff = []
stuff = []
stuff = []
MongoDB call took longer than 10s
stuff = [{"_id":"518574ca8349abd5c5b118f2","foo":"bar"}]
stuff = [{"_id":"518574ca8349abd5c5b118f2","foo":"bar"}]
stuff = [{"_id":"518574ca8349abd5c5b118f2","foo":"bar"}]
stuff = [{"_id":"518574ca8349abd5c5b118f2","foo":"bar"}]