Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember the pool options for a given connection. #175

Open
sauvainr opened this issue Dec 21, 2017 · 7 comments
Open

Remember the pool options for a given connection. #175

sauvainr opened this issue Dec 21, 2017 · 7 comments

Comments

@sauvainr
Copy link
Contributor

To use pooling the current usage is:

{:ok, conn} = Mongo.start_link(database: "test", pool: DBConnection.Poolboy)

Mongo.find(:mongo, "collection", %{}, limit: 20, pool: DBConnection.Poolboy)

Having to repeat pool: DBConnection.Poolboy at query time should not be necessary.

@ankhers
Copy link
Collaborator

ankhers commented Dec 21, 2017

This is a limitation of the db_connection package. It expects that we pass the database specific options for each query.

I have been toying with the idea of providing our own behaviour for DBConnection.Pool which would essentially just wrap a connection for the topology. Unfortunately I just have not gotten around to doing trying it yet.

@sauvainr
Copy link
Contributor Author

How about using a global configuration?

Application could configure:
config/config.exs

config mongodb:
  pool: DBConnection.Poolboy

In https://github.com/ankhers/mongodb/blob/master/lib/mongo.ex

@pool Application.get_env(:mongodb, :pool, DBConnection.Connection)

& in https://github.com/ankhers/mongodb/blob/master/lib/mongo.ex#L887

Keyword.put_new(opts, :timeout, @timeout)
|> Keyword.put_new(opts, :pool, @pool)

@ankhers
Copy link
Collaborator

ankhers commented Dec 23, 2017

I do not think that would be the best option. A number of people I have spoken with use multiple mongo connections in their application(s) (think multi tenant). I am unsure if they are actually using the same pool type for every connection.

I will try to speak to the db_connection team about this.

@sauvainr
Copy link
Contributor Author

I dont see a conflict unless I am missing something about db_connection. If different databases are used each of them needs a dedicated pool.

The pool: DBConnection.Poolboy specific the pool module, not the pool itself which is unique per conn.
So in a multi database case it would still be separated pools using the same pooling logic.

In my proposal, if the application (for any reason) wants different pulling options for different database, the application could still define the default pooling module in the configuration file and defines the specific pool module for the exception specific case.

And again if the application always defines the pool option as the case currently it would still work the same.

@theevangelista
Copy link

I also think that specifying a pool per connection is better, but in cases that are necessary could be an option on the query to execute it on another pool. For example if I start a mongo connection :foo with poolboy all mongo operations under the :foo connection would use the pool set at init. Otherwise we could maintain the pool option on the API for comparability and edge cases but not require.

I'll try to look into it this weekend

@theevangelista
Copy link

I looked at the codebase, here are my findings:

We need somehow get the :pool from the Mongo.start_link/2 function to be available as a default. I think it is stored inside the Mongo.Topology's GenServer. So we get that and merge with the options given to the query, if present on the query we accept the one from it otherwise we fetch from the Topology. I would implement this, but after some tries I think I lack the ability to do it :(

@CyrusNuevoDia
Copy link

Hey all, I'm not sure how useful this is, but I found a workaround for this.

Check out this Gist.

It's not self-sufficient to copy/paste into your project, but it should give you an idea of how to go about doing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants