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

Feature/pool #12

Merged
merged 21 commits into from
Sep 13, 2016
Merged

Feature/pool #12

merged 21 commits into from
Sep 13, 2016

Conversation

bcardiff
Copy link
Member

@bcardiff bcardiff commented Sep 1, 2016

Fixes #7

Adds a connection pool to manage connection. All connection pool options are configurable by URI query string. Example: DB.open("mysql://user:pass@host/dbname?initial_pool_size=2&max_pool_size=10")

Options:

  • initial_pool_size initial number of connections to be opened (defaults 1)
  • max_pool_size maximum number of connections to be opened (defaults 1)
  • max_idle_pool_size keep no more than this amount of idle/not used connections in the pool (defaults 1)
  • checkout_timeout fail to connect to the db if no connection turns to be available after this amount of seconds. (defaults 5.0 seconds)
  • retry_attempts if an established connection is lost allow the pool to reconnect, retry only this number of times before failing (defaults 1)
  • retry_delay amount of seconds to wait before each retry attempt to reconnect (defaults 1.0 seconds)

Drivers implementors can override how parameters are fetch. Or force them for example if only single connection is supported. See Driver#connection_pool_options.

A PoolStatement is introduced that represents statements that the user does not care in which actual connection will be executed. Hence, it can try to reconnect based on reconnect policy of the pool.

Pool#retry receives a block that will be re-executed if the block raises a DB::ConnectionLost.
This is used in PoolStatement#exec and #query to ensure an actual statement over a working connection is returned.

There are some outstanding issues regarding releasing connection reference once they are closed that will require callbacks or weak references.

Implementation assumes single thread since there is no mutex added in the pool.

It might be worth adding an overload to connect to the DB like DB#connect(config_uri) that will yield a connection (not pooled) in order to allow quick single connection access to a database. Probably in another PR.

Brian J. Cardiff and others added 17 commits July 5, 2016 15:21
* max_pool_size
* initial_pool_size
* max_idle_pool_size
* checkout_timeout
configuration options
allow Driver to parse connection pool options for extensibility.
fix waiting_resource counter after a timeout was generated.
add posibility to inspect availability of a resource in a pool (testing only)
allow access to internal connection pool of db (testing only)
create StatementMethods for common interface between Statement and PoolStatment.
refactor/reuse connection statement cache
Only the connections where it was prepared need to be cached.
The connections already have a statement cache based on the query
@statements_cache.each do |_, stmt|
stmt.close
end
@statements_cache.each_value &.close
@statements_cache.clear
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove connection from pool when explicitly closing

Brian J. Cardiff added 2 commits September 2, 2016 17:27
@sdogruyol
Copy link
Member

@bcardiff can't wait to have this merged in 👍

Brian J. Cardiff added 2 commits September 12, 2016 14:35
remove old connections from PoolStatement::@connections either disposed or closed connections
@bcardiff bcardiff merged commit a62c90d into master Sep 13, 2016
@bcardiff bcardiff deleted the feature/pool branch November 8, 2017 02:15
@Xosmond
Copy link

Xosmond commented Aug 12, 2018

@bcardiff Do you have an example of how to use it please?

@RX14
Copy link
Contributor

RX14 commented Aug 12, 2018

@Xosmond you just use the library as usual and it works in the background

@bcardiff
Copy link
Member Author

@Xosmond also check https://crystal-lang.org/docs/database/connection_pool.html it offers some code snippets and description of behavior.

@Xosmond
Copy link

Xosmond commented Aug 13, 2018

@bcardiff Thanks mate

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

Successfully merging this pull request may close these issues.

Connection pool
5 participants