Sha256: 844adf31077cce9bac85c4c2797436b68cd1740f35b9b8d83ab8679f4c82c872
Contents?: true
Size: 1.22 KB
Versions: 1
Compression:
Stored size: 1.22 KB
Contents
module ActiveRecord module ConnectionAdapters class RefreshConnectionManagement DEFAULT_OPTIONS = {max_requests: 1} def initialize(app, options = {}) @app = app @options = DEFAULT_OPTIONS.merge(options) @mutex = Mutex.new reset_remain_count end def call(env) testing = env.key?('rack.test') response = @app.call(env) clear_connections = should_clear_connections? && !testing response[2] = ::Rack::BodyProxy.new(response[2]) do # disconnect all connections on the connection pool ActiveRecord::Base.clear_all_connections! if clear_connections end response rescue Exception ActiveRecord::Base.clear_all_connections! if clear_connections raise end private def should_clear_connections? return true if max_requests <= 1 @mutex.synchronize do @remain_count -= 1 (@remain_count <= 0).tap do |clear| reset_remain_count if clear end end end def reset_remain_count @remain_count = max_requests end def max_requests @options[:max_requests] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activerecord-refresh_connection-0.0.3 | lib/activerecord-refresh_connection/active_record/connection_adapters/refresh_connection_management.rb |