Sha256: e4ff9b2d2e76d50cd214e434d0ecb1ea3acde2d242da8b52b44920a999f76a7d

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

require 'fresh_connection/connection_specification'

module EbisuConnection
  class Replica
    attr_reader :hostname, :weight

    def initialize(conf, spec_name)
      case conf
      when String
        host, weight = conf.split(/\s*,\s*/)
        @hostname, @port = host.split(/\s*:\s*/)
      when Hash
        @hostname = conf["host"] || conf[:host]
        weight = conf["weight"] || conf[:weight]
        @port = conf["port"] || conf[:port]
      else
        raise ArgumentError, "replica config is invalid"
      end

      spec = FreshConnection::ConnectionSpecification.new(
        spec_name, modify_spec: modify_spec
      ).spec

      @pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
      @weight = (weight || 1).to_i
    end

    def connection
      @pool.connection
    end

    def active_connection?
      @pool.active_connection?
    end

    def release_connection
      @pool.release_connection
    end

    def disconnect!
      @pool.disconnect!
    end

    private

    def modify_spec
      modify_spec = {"host" => @hostname}
      return modify_spec if !defined?(@port) || @port.nil?
      return modify_spec if @port.respond_to?(:empty?) && @port.empty?

      modify_spec["port"] = @port.to_i
      modify_spec
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ebisu_connection-2.4.2 lib/ebisu_connection/replica.rb
ebisu_connection-2.4.1 lib/ebisu_connection/replica.rb
ebisu_connection-2.4.0 lib/ebisu_connection/replica.rb
ebisu_connection-2.4.0.rc4 lib/ebisu_connection/replica.rb
ebisu_connection-2.4.0.rc3 lib/ebisu_connection/replica.rb
ebisu_connection-2.4.0.rc2 lib/ebisu_connection/replica.rb
ebisu_connection-2.4.0.rc1 lib/ebisu_connection/replica.rb