zookzook / elixir-mongodb-driver

MongoDB driver for Elixir
Apache License 2.0
244 stars 61 forks source link

Stop topology process if idle #33

Closed ja-jimenez closed 4 years ago

ja-jimenez commented 4 years ago

Hi!

When I create a new topology process using Mongo.start_link(opts), I see the following logs each 10 seconds, because that command is sent (I think) to MongoDB server:

13:57:57.962 [debug] Executing cmd: [isMaster: 1]

So the connection with server is ever active. Is there any option to stop the topology process (and close the connection or pool connections with database) if there isn't any request from the client?

I mean, if the client doesn't call any function from Mongo or Session modules in X seconds, stop that topology process. An example:

{:ok, topology} = Mongo.start_link([url: "mongodb://localhost:27017/test"])
Mongo.limits(topology) # OK
Mongo.find(topology, ...) # OK
# We don't use the topology process from 1 minute ago, it is closed automatically.
Mongo.limits(topology) # This should launch an error about the process is not alive.

Regards.

zookzook commented 4 years ago

The driver specification for monitoring describes this behavior: Monitoring

It is used to keep the current deployment up to date, for example in case that the primary server was changed. The monitor detects the new primary by calling isMaster command.

ja-jimenez commented 4 years ago

Hi @zookzook, thanks for you reply.

I have reviewed the spec but I think it doesn't give me whatever I need to known when a topology is not been used to make clients request like CRUD operations, and to stop it after some time, such as 60 seconds, to release database resources.

But after to read the spec I'm thinking this kind of functionality should be implemented in my app, not in the driver... ¿What do you think? ¿Could be interesting to add some kind of configuration param to the driver to stop a topology if there is no calls from app to driver in X seconds, or it must be implemented in the app?

Regards.

zookzook commented 4 years ago

The monitoring process is always running and updates the topology descriptions, whatever kind of deployment (replica-set, standalone or mongos) is used, because the driver should follow the specification. I don't think, that releasing a database resources like network connection or socket resources has any advantages.

Do you have a special use case for this behaviour? What are the advantages?

Opening a new connection is more expensive than using an already established connection. And the isMaster command keeps the connection alive.

ja-jimenez commented 4 years ago

I am thinking on stage where you have different customers, everyone using different database connections, such us if you build a platform for online stores. Each customer will have its clients, products, etc stored in a separate database.

In this case could be interesting to close connections if some of them are not doing requests. But, like I commented previously, maybe this functionality should be implemented in app side.