Sha256: ab0625f4966e033aa360aecda2b9d6330eac3066645e08424e325bfce4eb57c3

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module FreshConnection
  class SlaveConnection
    COUNT = :fresh_connection_access_count
    TARGET = :fresh_connection_access_target

    class << self
      attr_writer :ignore_models, :ignore_configure_connection

      def raw_connection
        slave_connection.raw_connection
      end

      def slave_connection
        connection_manager.slave_connection
      end

      def put_aside!
        connection_manager.put_aside!
      end

      def manage_access(model_name, go_slave, &block)
        if ignore_model?(model_name)
          force_master_access(&block)
        else
          target = go_slave ? :slave : :master
          begin
            access_in(target)
            block.call
          ensure
            access_out
          end
        end
      end

      def slave_access?
        Thread.current[TARGET] == :slave
      end

      def ignore_model?(model_name)
        (@ignore_models || []).include?(model_name)
      end

      def ignore_configure_connection?
        !!@ignore_configure_connection
      end

      def connection_manager=(manager)
        @connection_manager_class = manager
      end

      private

      def force_master_access
        now_target = Thread.current[TARGET]
        Thread.current[TARGET] = :master
        yield
      ensure
        Thread.current[TARGET] = now_target
      end

      def access_in(target)
        Thread.current[COUNT] = (Thread.current[COUNT] || 0) + 1
        Thread.current[TARGET] ||= target
      end

      def access_out
        Thread.current[COUNT] -= 1
        if Thread.current[COUNT] == 0
          Thread.current[TARGET] = nil
          Thread.current[COUNT] = nil
        end
      end

      def connection_manager
        @connection_manager ||=
          (@connection_manager_class || FreshConnection::ConnectionManager).new
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fresh_connection-0.1.2 lib/fresh_connection/slave_connection.rb