Sha256: ac234c30c9744945382fe46950023d52c3ce9897aec6662e4ed539d6e0458dc1

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'concurrent'

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

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

      @replica_group_to_pool[name] = 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(name)
      return @class_to_pool[name] if @class_to_pool.key?(name)
      g = @replica_group_to_pool[name]
      return nil unless g
      @class_to_pool[name] = FreshConnection.connection_manager.new(g)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fresh_connection-2.3.2 lib/fresh_connection/replica_connection_handler.rb
fresh_connection-2.3.1 lib/fresh_connection/replica_connection_handler.rb