Sha256: d64d374e1d25a390a2ed1098c7ae6e347e4130c28e62a041f9cca73951359e4e

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'concurrent'

module FreshConnection
  class ReplicaConnectionHandler
    def initialize
      @class_to_pool = Concurrent::Map.new
    end

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

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

    def connection(klass)
      detect_connection_manager(klass).replica_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 replica_group(klass)
      detect_connection_manager(klass).replica_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

1 entries across 1 versions & 1 rubygems

Version Path
fresh_connection-2.3.0 lib/fresh_connection/replica_connection_handler.rb