Advanced Connection
AdvancedConnection is a rails (~> 4.1) plugin that provides advanced management features for Rails' ActiveRecord connection pool. Features include:
- Idle Connection Manager
- Connection-less Code Blocks
- Statement Pooling (EXPERIMENTAL)
Version
Installation
You need Gulp installed globally:
$ gem install advanced_connection
or in your Gemfile, add:
gem 'advanced_connection', '~> 0.5.6'
Then generate your AdvancedConnection
configuration by executing:
bundle exec rails generate advanced_connection:install
Usage and Configuration
Idle Connection Manager
Enabling this will enable idle connection management. This allows you to specify settings to enable automatic idle connection reaping.
enable_idle_connection_manager = true | false
Pool queue type determines both how free connections will be checkout out of the pool, as well as how idle connections will be culled. The options are:
- :fifo
- All connections will have an equal opportunity to be used and culled (default)
- :lifo, :stack
- More frequently used connections will be reused, leaving less frequently used connections to be culled
- :prefer_older
- Longer lived connections will tend to stick around longer, with younger connections being culled
- :prefer_younger
- Younger lived connections will tend to stick around longer, with older connections being culled
connection_pool_queue_type = :fifo | :lifo | :stack | :prefer_older | :prefer_younger
Maximum number of connections that can remain idle without being culled. If you have more idle conections than this, only the difference between the total idle and this maximum will be culled.
max_idle_connections = integer | Float::INFINITY
How long (in seconds) a connection can remain idle before being culled
max_idle_time = integer
How many seconds between idle checks (defaults to max_idle_time)
idle_check_interval = integer
Connection-less Code Blocks
Enabling this will add a new method to ActiveRecord::Base that allows you to mark a block of code as not requiring a connection. This can be useful in reducing pressure on the pool, especially when you have sections of code that make potentially long-lived external requests. E.g.,
require 'open-uri'
results = ActiveRecord::Base.without_connection do
open('http://some-slow-site.com/api/')
end
During the call to the remote site, the db connection is checked in and subsequently checked back out once the block finishes. To enable this feature, uncomment the following:
enable_without_connection = true | false
Additionally, you can hook into the checkin / checkout lifecycle by way of callbacks. This can be extremely useful when employing something like Apartment
to manage switching between tenants.
without_connection_callbacks = {
# runs right before the connection is checked back into the pool
before: ->() { },
around: ->(&block) {
tenant = Apartment::Tenant.current
block.call
Apartment::Tenant.switch(tenant)
},
# runs right after the connection is checked back out of the pool
after: ->() { }
}
Statement Pooling
Todos
- Finish development of Statement Pooling
- Write Tests
- Add Code Comments
License
MIT