Not reconnecting to replica set
Hello,
I am trying to figure out how to reconnect to a replica set when the connection is lost. You can find an example here.
You will need to initialize first the replica set with this config:
config = {
"_id" : "rs198765",
"members" : [
{
"_id" : 0,
"host" : "mongo1:27017"
},
{
"_id" : 1,
"host" : "mongo2:27017"
},
{
"_id" : 2,
"host" : "mongo3:27017"
}
]
}
I start everything in the docker-compose and once everything is running I stop all the databases and I see the disconnect event being executed. After that, if I start again the database instances, but my code is never connecting again.
Am I doing something wrong or missing any configuration param?
Thank you!
Try listening to the 'reconnected' and 'connected' events too
Thanks for the advice @vkarpov15. I added those to the test code and tried it again. The 'connected' event is executed when I start the app and it connects successfully but none of them are executed after stopping the database images and starting them again.
How long are you shutting the database images down for? Might be that reconnectTries somehow isn't getting passed down correctly
@vkarpov15 I'm actually looking into some issues I'm having. This is work made following an issue I added to the mongo driver: https://jira.mongodb.org/browse/NODE-893.
What I'm seeing is that replica set never emits a 'reconnect' nor 'open' events when connecting back. It just emits a 'connect' event. What's currently happening is that the replica set emits a 'close' event, that makes a 'buffer' variable to switch to true, further queries are buffered, and that never changes again.
There seems to be some code setup to listen for the 'open' event:
https://github.com/Automattic/mongoose/blob/master/lib/drivers/node-mongodb-native/connection.js#L171.
And then emit a 'reconnect' event. It seems that we no longer should need to listen to the 'open' event, and start listening to the 'connect' event instead (or maybe listen for both?). The driver code I'm talking about is around https://github.com/christkv/mongodb-core/blob/2.0/lib/topologies/replset.js#L622.
Looking forward for you giving me your thoughts about this, thanks..!!
@vkarpov15 I actually debugged the mongo core driver and saw that the ReplSet is not firing connect / reconnect events after having fired the close event, which is the responsible of having Mongoose to switch to buffer mode. See a detailed explanation in https://jira.mongodb.org/browse/NODE-893?focusedCommentId=1453800&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1453800
This should be solved by https://github.com/Automattic/mongoose/pull/4792.