Sha256: 19e57ce48f19271fbb2e3a4bad7bc968b68c2269a4302f2d3ecfd2dc0b3f13cf

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

require 'concurrent'
require 'fresh_connection/abstract_connection_manager'
require 'fresh_connection/connection_factory'

module FreshConnection
  class ConnectionManager < AbstractConnectionManager
    def initialize(*args)
      super
      @connections = Concurrent::Map.new
    end

    def replica_connection
      @connections.fetch_or_store(current_thread_id) do |_|
        connection_factory.new_connection
      end
    end

    def put_aside!
      conn = @connections.delete(current_thread_id)
      return unless conn
      conn && conn.disconnect! rescue nil
    end

    def clear_all_connections!
      @connections.each_value do |conn|
        conn.disconnect! rescue nil
      end
      @connections.clear
    end

    def recovery?
      return false if replica_connection.active?
      put_aside!
      true
    end

    private

    def connection_factory
      @connection_factory ||= ConnectionFactory.new(@replica_group)
    end

    def current_thread_id
      Thread.current.object_id
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fresh_connection-2.3.2 lib/fresh_connection/connection_manager.rb
fresh_connection-2.3.1 lib/fresh_connection/connection_manager.rb
fresh_connection-2.3.0 lib/fresh_connection/connection_manager.rb