Sha256: adeec4efd3e5b593952d02e1b24342203817f48eac27f518b8e97524963e4303

Contents?: true

Size: 923 Bytes

Versions: 5

Compression:

Stored size: 923 Bytes

Contents

require 'concurrent'

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

    def slave_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 slave_connection.active?
      put_aside!
      true
    end

    private

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

    def current_thread_id
      Thread.current.object_id
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fresh_connection-2.1.2 lib/fresh_connection/connection_manager.rb
fresh_connection-2.1.1 lib/fresh_connection/connection_manager.rb
fresh_connection-2.1.0 lib/fresh_connection/connection_manager.rb
fresh_connection-2.0.4 lib/fresh_connection/connection_manager.rb
fresh_connection-2.0.3 lib/fresh_connection/connection_manager.rb