Connection Timeout for mongodb using mongoose
Asked Answered
T

3

21

I have a web application running on Node, express and MongoDB. I use mongoose as the ODM. When i tested my application with mongodb version v3.0.1 it runs fine and throws no errors. But when i run the same code v3.2.10 i get a connection timeout after some time.

I get the following Error :

Error: connection timeout at null.<anonymous> (/webapp/node_module/mongoose/lib/drivers/node-mongodb-native/connection.js:186:17)

I use mongoose.connect for the db connection to the local mongodb instance. Has anything changed in the way of connection ?

Trysail answered 14/11, 2016 at 9:42 Comment(5)
the connection timeout at 'null' makes me suspect the proper port number isn't being passed to mongoose's connection.js module - have you considered this?Instantaneous
it connects fine runs fine for some time. So i think the connection works fine but this error comes up after 20-30 mins.Trysail
@Trysail I have the same issue. If you could solve it, let me know how.Britnibrito
did you ever find a solution to this?Aisha
For more information please visit to: mongoosejs.com/docs/connections.html#optionsDunc
N
20

I had this problem a while ago. It all depends on which version of mongoose and mongodb-core you are using. Right now, you have to specify the following parameters:

mongoose.connect("mongodb://user:password@address/db", {
  server: {
    socketOptions: {
      socketTimeoutMS: 0,
      connectionTimeout: 0
    }
  }
});

However, just yesterday, the correct parameters where

mongoose.connect("mongodb://user:password@address/db", {
  server: {
    socketOptions: {
      socketTimeoutMS: 0,
      connectTimeoutMS: 0
    }
  }
});

I don't really know what to believe in anymore..

Nestling answered 23/12, 2016 at 16:6 Comment(1)
Also see this: github.com/christkv/mongodb-core/issues/153 There is an issue when setting the timeout in the latest mongodb-core .Nestling
E
8

I realize that this is an old question, but the accepted answer now contains deprecated code. To set a connection timeout with Mongoose v5+, you now need to put all of the options at the top level of the options object, not nested like before:

mongoose.connect(uri, {
  useUnifiedTopology: true, // For Mongoose 5 only. Remove for Mongoose 6+
  serverSelectionTimeoutMS: 1000, // Defaults to 30000 (30 seconds)
})
Esparto answered 27/4, 2021 at 20:56 Comment(5)
Is 1000 enough to prevent timeouts? Is there any side effect if I set it to 30.000? @EspartoImpend
@GökhanPolat That depends on what you're trying to do. Set it to whatever you want the timeout to be. I just used 1000 as an example.Esparto
socketTimeoutMS is independent of useUnifiedTopology. It is not a deprecated/defunct option.Eaglestone
@Eaglestone It's been a while since I wrote this answer, but I believe this is what I was referring to. Now, options are declared at the top level, not nested.Esparto
@Eaglestone I've edited the answer for clarification.Esparto
K
0

RTFM ^^ - The only way to get up-to-date working option for a given product is to have a look at the relacted product version official documentation.

As today speaking, for the 8.3.x version here is a sample with link to the official documentation, that override default timeout (30 sec) to 3 seconds.

// set 3sec timeout to avoid waiting 30 seconds on misconfiguration:
// warn: this may produce flaky issues on some host with network latency

// doc: https://mongoosejs.com/docs/connections.html#serverselectiontimeoutms
const serverSelectionTimeoutMS = 3000;
// https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connect()
const socketTimeoutMS = 3000;

const MONGOOSE_CONNECT_OPTIONS = {serverSelectionTimeoutMS, socketTimeoutMS};

// example in use
export const countDocumentsInCollection = async uri => {
    try {
        await mongoose.connect(uri, MONGOOSE_CONNECT_OPTIONS);
        return await myCollection.countDocuments();
    } finally {
        await mongoose.disconnect();
    }
}

Doc extract:

The serverSelectionTimeoutMS option is extremely important: it controls how long the MongoDB Node.js driver will attempt to retry any operation before erroring out. This includes initial connection, like await mongoose.connect(), as well as any operations that make requests to MongoDB, like save() or find().

and

[options.socketTimeoutMS=0] «Number» How long the MongoDB driver will wait before killing a socket due to inactivity after initial connection

Kenelm answered 3/5 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.