Sha256: 3b11e7d890f4fa90fa0625e43132bb6554aadca35969cb94ea25821cf54b3b44

Contents?: true

Size: 996 Bytes

Versions: 9

Compression:

Stored size: 996 Bytes

Contents

module FreshConnection
  class SlaveConnectionHandler
    def initialize
      @class_to_pool = {}
    end

    def establish_connection(name, slave_group)
      if cm = @class_to_pool[name]
        cm.put_aside!
      end

      @class_to_pool[name] = FreshConnection.connection_manager.new(slave_group)
    end

    def connection(klass)
      detect_connection_manager(klass).slave_connection
    end

    def put_aside!
      @class_to_pool.values.each do |connection_manager|
        connection_manager.put_aside!
      end
    end

    def recovery(klass, failure_connection, exception)
      detect_connection_manager(klass).recovery(failure_connection, exception)
    end

    def slave_group(klass)
      detect_connection_manager(klass).slave_group
    end

    private

    def detect_connection_manager(klass)
      c = @class_to_pool[klass.name]
      return c if c
      return nil if ActiveRecord::Base == klass
      detect_connection_manager(klass.superclass)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fresh_connection-0.4.0 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.3.0 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.6 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.5 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.4 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.3 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.2 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.1 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-0.2.0 lib/fresh_connection/slave_connection_handler.rb