I
I
Ivan Chernyshev2020-02-25 17:25:07
MongoDB
Ivan Chernyshev, 2020-02-25 17:25:07

Why is Mongoose destroying the connection pool?

Hello!
Faced such a problem: at an unpredictable moment, the Node.js process, when requested to search in the database, mongoosecrashes with an error

MongoError: pool destroyed
    at Pool.write (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:853:8)
    at _command (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/wireprotocol/command.js:127:10)
    at command (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/wireprotocol/command.js:27:5)
    at Object.query (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/wireprotocol/query.js:57:3)
    at Server.query (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:639:16)
    at FindOperation.execute (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/operations/find.js:24:12)
    at topology.selectServer (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/operations/execute_operation.js:164:17)
    at Server.selectServer (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:827:3)
    at Server.selectServer (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/topologies/topology_base.js:363:32)
    at executeWithServerSelection (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/operations/execute_operation.js:150:12)
    at executeOperation (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/operations/execute_operation.js:81:16)
    at Cursor._initializeCursor (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:545:7)
    at Cursor._initializeCursor (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:191:11)
    at nextFunction (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:748:10)
    at Cursor._next (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
    at fetchDocs (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/operations/to_array.js:27:14) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }


Itself is MongoDBalive and healthcheckreports on the status The healthy
bug is repeated in different applications, at different times, in different places. After restarting the process, Node.jsreconnection occurs normally. After a while, the process crashes again.

The settings mongooseare all default, except for poolSizewhich is set to 250.

Service setup: Node.jsand MongoDBin Dockerusing docker-compose.
The service- image Node.jsis built from node:10
Image MongoDB-- mongo:latest(currently: 4.2.3) Version
usedmongoose5.7.5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2020-03-11
@IvanBlacky

Ivan Chernyshev , there is a suspicion that you are doing db.close() somewhere before the operation is completed.
This is how the pool works https://mongodb.github.io/node-mongodb-native/core...
After reading the sources https://github.com/mongodb-js/mongodb-core/blob/ma... you can understand that the pool is destroyed when the sockets run out or the limit of reconnect attempts occurs.
Mongoose is quite old, they fixed bugs there. Try updating it.
Also enable debugging in mongus.
mongoose.set('debug', true)

A
Alexander Cheremkhin, 2020-02-29
@Che603000

poolSize which is set to 250.

The default value is 5. You have set a value 50 times higher than recommended. Apparently you had good reasons for this.
If it's not a secret, what are they? Is it possible to reduce the number of connections? Does pool fall in this case?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question