class RooOnRails::Rack::SafeTimeouts

Cleans up Rails database connections on timeouts, before they're returned to the pool.

In particular, this clears the prepared statement cache, which can become corrupted as ActiveRecord isn't interrupt-safe.

Public Class Methods

new(app) click to toggle source
# File lib/roo_on_rails/rack/safe_timeouts.rb, line 15
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/roo_on_rails/rack/safe_timeouts.rb, line 19
def call(env)
  @app.call(env)
rescue ::Rack::Timeout::Error, ::Rack::Timeout::RequestTimeoutException
  Rails.logger.warn('Clearing ActiveRecord connection cache due to timeout')
  ActiveRecord::Base.connection.clear_cache!
  raise
end