-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Feature/pool #12
Conversation
* 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 |
There was a problem hiding this comment.
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
* select keyword hangs so Channel.select is used still
@bcardiff can't wait to have this merged in 👍 |
remove old connections from PoolStatement::@connections either disposed or closed connections
@bcardiff Do you have an example of how to use it please? |
@Xosmond you just use the library as usual and it works in the background |
@Xosmond also check https://crystal-lang.org/docs/database/connection_pool.html it offers some code snippets and description of behavior. |
@bcardiff Thanks mate |
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 aDB::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.