Hello all, we are using MongoDB Serverless and connecting to it from AWS Lambda. I realized that after a while (a few minutes) of idle, subsequent database queries returns this error:
MongoNetworkError: connection 1 to *IP*:27017 closed at Connection.onClose (.../node_modules/mongodb/lib/cmap/connection.js:134:19) at TLSSocket.<anonymous> (.../node_modules/mongodb/lib/cmap/connection.js:62:46) at TLSSocket.emit (node:events:513:28) at TLSSocket.emit (node:domain:489:12) at node:net:301:12 at TCP.done (node:_tls_wrap:588:7)
The next few requests will continue to fail as each connection within the pool fails and reconnects. At one point the failures will subside, until I leave it idle for X minutes, and the problem will surface again.
For each of my Lambda function, I have this set:
request.context.callbackWaitsForEmptyEventLoop = false;
with the mongodb client instance outside of the handler function, as suggested by the mongodb for lambda guide.
I can also confirm it is not a network access issue as it currently has 0.0.0.0 allowed and it works consistently at fresh start.
Any suggestions or help will be much appreciated! Thanks