I'm using AWS EC2 instance where my node server is running
node - v10.16.3
,
npm - v6.11.3
,
mongo - v4.2.0
,
Initially, I had these issues with my server where it logged me this error
{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:198:13)
at Pool.EventEmitter.emit (domain.js:448:20)
at connect (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at makeConnection (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connect.js:39:11)
at callback (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connect.js:261:5)
at Socket.err (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connect.js:286:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket.EventEmitter.emit (domain.js:448:20)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
I restarted my server after seeing this log and then my server worked perfectly fine with no issues for a few weeks. But then I saw other issues with Mongo which logged me that the Topology was Destroyed.
{ MongoError: Topology was destroyed
at initializeCursor (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/cursor.js:596:25)
at nextFunction (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/cursor.js:456:12)
at Cursor.next (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/cursor.js:766:3)
at Cursor._next (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:216:36)
at nextObject (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/operations/cursor_ops.js:192:10)
at next (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/operations/cursor_ops.js:171:3)
at executeOperation (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/utils.js:416:24)
at Cursor.next (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:258:10)
at findOne (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/operations/collection_ops.js:596:10)
at executeOperation (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/utils.js:416:24)
at Collection.<anonymous> (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/collection.js:989:12)
at Collection.deprecated [as findOne] (/var/www/html/node_modules/mongoose/node_modules/mongodb/lib/utils.js:677:15)
at NativeCollection.(anonymous function) [as findOne] (/var/www/html/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:158:28)
at NodeCollection.findOne (/var/www/html/node_modules/mquery/lib/collection/node.js:42:19)
at model.Query.Query.findOne (/var/www/html/node_modules/mquery/lib/mquery.js:2025:20)
at model.Query.<anonymous> (/var/www/html/node_modules/mongoose/lib/query.js:2058:22) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }
I made some changes to the Mongo connection options setting like this
const options = { socketTimeoutMS: 30000, keepAlive: true, connectTimeoutMS: 30000, useNewUrlParser: true, useFindAndModify: false, useUnifiedTopology: true };
After changing the settings my server worked fine for some time and then then it goes unstable and unresponsive. I need to restart it manually every time to keep it alive
(node:28639) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 31 finish listeners added. Use emitter.setMaxListeners() to increase limit
I have also had this memory leak issue in between these error logs.
{ MongoNetworkError: connection 1 to localhost:27017 timed out
at Socket.<anonymous> (/var/www/html/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:335:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket.EventEmitter.emit (domain.js:448:20)
at Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
When my server gets unresponsive or unstable and if an API is called this is what I get in response
BadRequestError: request aborted
at IncomingMessage.onAborted (/var/www/html/node_modules/raw-body/index.js:231:10)
at IncomingMessage.emit (events.js:198:13)
at IncomingMessage.EventEmitter.emit (domain.js:448:20)
at abortIncoming (_http_server.js:448:9)
at socketOnClose (_http_server.js:441:3)
at Socket.emit (events.js:203:15)
at Socket.EventEmitter.emit (domain.js:448:20)
at TCP._handle.close (net.js:606:12)
I'm not sure where's the issue exactly came from or what do I need to change to keep it alive. Please help me out with this issue since I cannot understand whether it is memory leak issue or server overloaded.