No commit activity in last 3 years
No release in over 3 years
Adds idle connection management, statement pooling, and other advanced connection features
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.8.13
~> 4.6.4
~> 0.18.4
~> 0.10.3
~> 0.2.4
~> 1.6.4
~> 10.5.0
~> 3.4.0
~> 1.2.0

Runtime

 Project Readme

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

Gem 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
WARNING: this feature cannot be enabled with Statement Pooling.

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