Sha256: a9eed112c9550fd23a97aac9e0d804718a4d1dc60f8a4010aa37e8c50d0464d7

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require 'active_support/core_ext/module/delegation'
require 'concurrent/map'

require 'active_replicas/rails4/process_local_connection_handler'

module ActiveReplicas
  module Rails4
    # Wraps around Rails' `ActiveRecord::ConnectionAdapters::ConnectionHandler`
    # to provide proxy wrappers around requested connections.
    #
    # This is the process-safe handler; it creates instances of
    # `ThreadLocalConnectionHandler` to provide proxy wrappers for each thread.
    class ConnectionHandler
      attr_accessor :proxy_configuration

      def initialize(proxy_configuration:, delegate: nil, overrides: nil)
        @proxy_configuration = proxy_configuration
        # @delegate          = delegate
        # @overrides         = Set.new(overrides || [])

        # Each process will get its own thread-safe handler.
        @process_to_handler = Concurrent::Map.new
      end

      delegate :active_connections?, :clear_active_connections!,
          :clear_all_connections!, :clear_reloadable_connections!,
          :connected?, :connection_pool_list, :establish_connection,
          :remove_connection, :retrieve_connection, :retrieve_connection_pool,
        to: :retrieve_handler

      def clear_all_connections!
        # We also want to clear the process's connection handler in case our
        # configuration has been changed.
        if handler = @process_to_handler.delete(Process.pid)
          handler.clear_all_connections!
        end
      end

      # Returns a `ProcessLocalConnectionHandler` which is local to the
      # current process.
      def retrieve_handler
        @process_to_handler[Process.pid] ||= ProcessLocalConnectionHandler.new(@proxy_configuration)
      end
    end
  end

  ConnectionHandler = Rails4::ConnectionHandler
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_replicas-0.5.1 lib/active_replicas/rails4/connection_handler.rb
active_replicas-0.5.0 lib/active_replicas/rails4/connection_handler.rb
active_replicas-0.4.0 lib/active_replicas/rails4/connection_handler.rb