Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

socketOptions are not passed to underlying drivers if mongos router is used #5293

Closed amit777 closed 7 years ago

amit777 commented 7 years ago

Do you want to request a feature or report a bug? bug

What is the current behavior? when using mongos in sharded cluster configuration, socketOptions are not passed to underlying drivers correctly

If the current behavior is a bug, please provide the steps to reproduce.

  var dbOptions = {
    "db": {"native_parser": true},
    "server": {"poolSize": 3, "socketOptions": { socketTimeoutMS: 5000, connectTimeoutMS: 1000, keepAlive: 120}}, 
    "mongos": true
  }

What is the expected behavior?

socketTimeout (or socketTimeoutMS or connectionTimeout or connectionTimeoutMS) should be set in mongos: true is set in dbOptions

as a side note, all the namings of the various timeout options are very confusing and seem inconsistent. I've tried all kinds of combinations and it would be good to know what the recommended setting names are.

Please mention your node.js, mongoose and MongoDB version. node 7.9.0, mongoose 4.10.2, mongodb 3.4.4

amit777 commented 7 years ago

everything seems to work if I do not set "mongos: true"

Is there any gotcha regarding topologies or reconnection logic that I missing?

vkarpov15 commented 7 years ago

Try setting mongos: { socketOptions: { /* ... */ } instead of mongos: true. The mongos: true hack is for people who don't want to set any mongos options.

amit777 commented 7 years ago

Got it! thanks. Out of curiousity, are there any behavioral differences in the mongo client if it's connecting to a mongos vs a regular mongod?

vkarpov15 commented 7 years ago

"Behavioral differences" is pretty vague. As far as mongoose is concerned, the only difference between sharded and not sharded is the built-in sharding plugin, which prevents you from changing a document's shard key values if you set the shardKey option. That's the only sharding-specific functionality mongoose offers, modulo any community plugins you might add.

amit777 commented 7 years ago

sorry to re-open. I think the example configuration in the documentation at http://mongoosejs.com/docs/connections.html is a bit misleading:

var options = {
  db: { native_parser: true },
  server: { poolSize: 5 },
  replset: { rs_name: 'myReplicaSetName' },
  user: 'myUserName',
  pass: 'myPassword'
}

The poolSize parameter is set on the server attribute rather than the replset attribute. Is my understanding correct that you should use only 1 of the following attributes: 'server', 'replset', 'mongos'. Or is it appropriate to have some configurations under 'server' and other configs under 'replset' etc? I can create a pull request for the documentation if it's better to have 3 different example configs depending on your mongo environment.

vkarpov15 commented 7 years ago

It's somewhat confusing but there are cases when you want to use all of these attributes, because if you're connecting to a sharded cluster you're connecting to one or more replica sets, and in turn to individual servers. poolSize is configured on the individual server level regardless of whether you're connecting to a standalone, a replica set, or a sharded cluster.

marcmoo commented 5 years ago

for latest version of mongodb, I think settings should be:

mongoose.connect( mongodb://${username}:${password}@ds016000-a0.mlab.com:16000,ds016000-a1.mlab.com:16084/azure_rn_paid?replicaSet=rs-ds016000, { useNewUrlParser: true, auto_reconnect: true, connectTimeoutMS: 3600000, keepAlive: 3600000, socketTimeoutMS: 3600000 } );