Class: Waterpig::DeadbeatConnectionRelease

Inherits:
Object
  • Object
show all
Defined in:
lib/waterpig/deadbeat-connections.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ DeadbeatConnectionRelease

Returns a new instance of DeadbeatConnectionRelease.



3
4
5
# File 'lib/waterpig/deadbeat-connections.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

This is a brittle hack for what appears to be a bug in ActiveRecord 4: connections aren’t being properly released after their owning threads die



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/waterpig/deadbeat-connections.rb', line 9

def call(env)
  response = @app.call(env)
  response[2] = ::Rack::BodyProxy.new(response[2]) do
    ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
      reservers = pool.instance_variable_get("@reserved_connections").keys
      deadbeats = reservers - Thread.list.map(&:object_id)
      Rails.logger.info{ "Releasing connections held by these no-longer-living Threads: #{deadbeats}" } unless deadbeats.empty?
      deadbeats.each do |deadbeat_id|
        pool.release_connection(deadbeat_id)
      end
    end
  end

  response
rescue Object => ex
  require 'pp'
  Rails.logger.fatal "Problem responding to request in test"
  Rails.logger.fatal [ex.class, ex.message, ex.backtrace].join("\n")
  Rails.logger.fatal env.pretty_inspect
  Rails.logger.fatal response.pretty_inspect
  raise
end