<commit_before>require 'concurrent/map'

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

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

    # Provisioned for each process by `ConnectionHandler`. Each process owns
    # its own pools of connections to primary and replica databases.
    # Proxying connection pools are then provisioned for each thread.
    class ProcessLocalConnectionHandler
      # Instance of `ConnectionHandler`.
      attr_reader :owner

      attr_reader :primary_pool, :replica_pools

      def initialize(owner)
        @owner = owner

        @primary_pool = Helpers.connection_pool_for_spec proxy_configuration[:primary]

        @replica_pools = (proxy_configuration[:replicas] || {}).map do |name, config_spec|
          [name, Helpers.connection_pool_for_spec(config_spec)]
        end.to_h

        # Each thread gets its own `ProxyingConnectionPool`.
        @reserved_proxies = Concurrent::Map.new

        extend MonitorMixin
      end

      delegate :proxy_configuration, to: :owner

      # Returns a list of *all* the connection pools owned by this handler.
      def connection_pool_list
        [@primary_pool] + @replica_pools.values
      end
<commit_msg>Tweak relationship between and initialization of `ConnectionHandler` and `ProcessLocalConnectionHandler`<commit_after>require 'concurrent/map'

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

    # Provisioned for each process by `ConnectionHandler`. Each process owns
    # its own pools of connections to primary and replica databases.
    # Proxying connection pools are then provisioned for each thread.
    class ProcessLocalConnectionHandler
      attr_reader :proxy_configuration
      attr_reader :primary_pool, :replica_pools

      def initialize(proxy_configuration)
        @proxy_configuration = proxy_configuration

        @primary_pool = Helpers.connection_pool_for_spec @proxy_configuration[:primary]

        @replica_pools = (@proxy_configuration[:replicas] || {}).map do |name, config_spec|
          [name, Helpers.connection_pool_for_spec(config_spec)]
        end.to_h

        # Each thread gets its own `ProxyingConnectionPool`.
        @reserved_proxies = Concurrent::Map.new

        extend MonitorMixin
      end

      # Returns a list of *all* the connection pools owned by this handler.
      def connection_pool_list
        [@primary_pool] + @replica_pools.values
      end

{'fraction_non_alphanumeric': 0.06798202773979292, 'fraction_numerical': 0.0003907013088493846, 'mean_word_length': 3.2454394693200666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4145878', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1301, 'n_words': 377}