I tried to use keep-alive connection in mongoose and noticed it is sometimes hanging.
How can I solve this?
Related
When users hit multiple requests at the same time. The execution time of the api is taking lot of time and the mongoose connection is getting disconnected for apparently no reason and then later getting the error mongooseServerSelectionError timeout after 3000ms. How to stop the mongoose connection from getting disconnected?
From what I have read online the mongoose connection gets disconnected due to inactivity link. But I am not sure how to stop it from disconnecting. I tried setting keep-alive: true and keepAliveInitialDelay:10ms and also played around with connectTimeoutMS and socketTimeoutMS but none stopped the disconnection of the mongoose connection.
Please help me out here.
If I try to create a new connection and close the older one when the mongoose connection gets disconnected, what kind of impact does that have on the mongoDB transactions and the sessions that are currently ongoing?
Edit: The existing mongoose connections are disconnecting when the no of tcp/http connections are high/ we are internally calling external api's. So how to stop the existing mongoose connection from disconnecting? Is there any hard limit on nodejs that we cannot use more than x no of connections open at a time?
I am using mongoose in one of my projects. So when I start the server I connect to mongodb using mongoose.connect(url)
I have a 3 questions here
Does mongodb closes the connection after a certain amount of time if the connection is not used?
Will mongoose automatically reconnect if the connection gets closes/disconnected?
How to make sure if any time connection gets closed, it will automatically reconnects?
I have seen some old question answers where they had a options object which contains how many time mongoose will try to reconnect if connection closes, but those options aren't supported right now.
I am using arangojs v6.14.1 and arangodb version 3.6.4.. I also have the nodejs express app which is intended to serve client requests.
I am experiencing an issue while executing concurrent requests. The database connection hangup when I concurrently process client requests:
What the app will do when it receives a request?
Open a database connection -
db = new Database(dbConfig.url);
db.useDatabase(dbConfig.name);
db.useBasicAuth(dbConfig.username, dbConfig.password);
There are multiple middleware functions that need to access to perform various functions and accessibility checks. And for each middleware I tried to
open a new database connection ->
perform the action ->
close the connection ->
return the result to next middleware.
This works fine with single request at a time. But if I tried to call two APIs at same time, I am getting CONNECTIONRESET error. And also throwing socket hangup error.
I tried to commend out the close connection method and it started working fine for a while. But when I increased the number of connections, it again showing the same error as "CONNECTIONRESET".
I have searched the documentation of arangojs regarding the connection manipulation. But I haven't found any information regarding the same.
Any help would be deeply appreciated.
This might be too late for you, but I had a similar issue. After talking with ArangoDB support, it turned out to be an issue with sockets.
Looking at the output from netstat -aplnt I was seeing a lot of CLOSE_WAIT and TIME_WAIT messages, indicating that there were A LOT of connections that were not being closed properly.
The solution was to enable persistent connections (see arangojs config for keep-alive and maxSockets) and then re-using a single connection whenever possible. This was more than just a settings change, requiring some code modifications as well.
I'm trying to send back an error to the client if the database couldn't connect after 3 seconds.
I have came across those ways from the mongoose docs.
1)
mongoose.connect("mongodb://localhost/smslist?connectTimeoutMS=1000"
2) setting this option :
connectTimeoutMS: 1000
I have closed the server in order to make sure that it does not respond, I expected the client to receive an error after one second but unfortunately both of those did not work, and the client receives an error only after 30 seconds which is the default for node sockets as far as I know. Do you have any clue how I can fix this issue?
If anyone comes across this post, I have fixed it by using serverSelectionTimeoutMS=1000 instead of connectTimeoutMS=1000
Very simple and stupid question I came up with today when was playing around with sails and mongo db (adapter of the waterline odm). I just shut down the mongo while my sails server still has been running and saw nothing, neither in logs of sails or in the browser. How I could handle such situation?
If sails.js attempts a connection to the DB you will most certainly get a message in your console if the Mongo server is shut down, but if no request is made that requires a DB connection, then you will not see any error because no error as of yet exists.
It sounds like you might require a process that will monitor your DB and check to make sure it is still running? There are Sail.js options where you could create a CRON job to check the connection. Or you could use other application monitoring services like New Relic to monitor your DB.