Sha256: 188fe72e553544f6b101cbe57d6425898b747867f7edc06a6c6dfa3b733f0695

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

require 'concurrent'

module FreshConnection
  class SlaveConnectionHandler
    def initialize
      @class_to_pool = Concurrent::Map.new
    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 clear_all_connections!
      all_connection_managers do |connection_manager|
        connection_manager.clear_all_connections!
      end
    end

    def recovery?(klass)
      detect_connection_manager(klass).recovery?
    end

    def put_aside!
      all_connection_managers do |connection_manager|
        connection_manager.put_aside!
      end
    end

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

    private

    def all_connection_managers
      class_to_pool.each_value do |connection_manager|
        yield(connection_manager)
      end
    end

    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

    def class_to_pool
      @class_to_pool
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fresh_connection-2.2.0 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-2.1.2 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-2.1.1 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-2.1.0 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-2.0.4 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-2.0.3 lib/fresh_connection/slave_connection_handler.rb
fresh_connection-2.0.2 lib/fresh_connection/slave_connection_handler.rb