nswbmw / koa-mongo

MongoDB middleware for koa, support connection pool.
145 stars 31 forks source link

create a new pool when mongo server instance pool is destroyed #27

Closed arlindash closed 7 years ago

arlindash commented 7 years ago

When mongo is down and the MongoClient has gone through the reconnectTries, the mongo server instance pool gets destroyed. This means that the server using the koa-mongo middleware will have to get restarted in order for koa-mongo to create a new connection pool. With this change, the acquired connection object is first checked if it is connected with the mongo server. If it is not, the middleware waits for the reconnect tries interval before creating a new pool and acquiring a new connection object.

nswbmw commented 7 years ago

Thanks for your pr, it has two problems:

  1. 4 spaces -> 2 spaces
  2. DO NOT USE while (now - start < ms), you may need promise-retry
nswbmw commented 7 years ago

I found that mongodb-core has reconnect, and i tried it works well. so is this pr(handle reconnect by user) necessary?

arlindash commented 7 years ago

MongoClient does have the reconnect mechanism. However, when the reconnect interval passes the mongo server instance pool gets destroyed. After that even if mongo is running again, when we acquire a new connection from the created pool, we will face the error of server instance pool being destroyed. To fix this issue we would have to restart the server, so that the mongoPool gets created again. What this code does is after acquiring a connection object, checks if the resource is connected to the mongo server. If it is not, it gives the MongoClient time to perform the retries, then checks the resource again. Only after the retry interval has passed and we did not get a connection, it creates a new pool.

tb01923 commented 7 years ago

Is there an update on this?

eriknyk commented 7 years ago

this PR seems very interesting and useful, do we will have soon a merge of it?