Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

Example of using the connection pool #139

Closed
freiguy1 opened this issue Mar 17, 2016 · 3 comments
Closed

Example of using the connection pool #139

freiguy1 opened this issue Mar 17, 2016 · 3 comments

Comments

@freiguy1
Copy link

This isn't an issue per se, more of a request. I'm working on an api using mongo and this library and as of now, I've a connection instantiated when the server starts. Every time a request comes in (as of now all requests communicate w/ mongo), the connection is cloned. Is this the correct way to do this? Should I be using a mongodb::pool::ConnectionPool instead? If so, I'm not sure how to query using a PooledStream.

Thanks!

@kyeah
Copy link
Contributor

kyeah commented Mar 26, 2016

Hey, I didn't see this issue until now! The connection pool is maintained internally by a MongoDB Client, so you shouldn't ever have to deal with the pool directly. MongoDB apps are designed to have one Client across the entire application, so developers can clone Client (which is actually an alias of an Arc type), allowing them to use multiple references of the same connection pool.

This is discussed a little bit in #135.

... Client is actually just an alias of an Arc type, which means you can open the connection at the top of the function and then just use .clone() to get an (owned) atomic reference to it inside each of your routes that need it.

It sounds like you are doing this in your application, which is great! If you're using cloned references to a single Client, the internal connection pool will automatically handle connection recycling for you and create new connections under heavy load.

@freiguy1
Copy link
Author

Very good, thanks!

@YjyJeff
Copy link

YjyJeff commented May 13, 2017

Can I set the pool size?

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

No branches or pull requests

3 participants