lib/ebisu_connection/replica.rb in ebisu_connection-2.3.1 vs lib/ebisu_connection/replica.rb in ebisu_connection-2.4.0.rc1

- old
+ new

@@ -1,46 +1,55 @@ +require 'fresh_connection/connection_specification' + module EbisuConnection class Replica attr_reader :hostname, :weight - def initialize(conf, replica_group) + def initialize(conf, spec_name) case conf when String host, weight = conf.split(/\s*,\s*/) - @hostname, port = host.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] + @port = conf["port"] || conf[:port] else raise ArgumentError, "replica config is invalid" end - spec = modify_spec(port) - @connection_factory = FreshConnection::ConnectionFactory.new(replica_group, spec) + 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 - @connection ||= @connection_factory.new_connection + @pool.connection end - def active? - connection.active? + def active_connection? + @pool.active_connection? end + def release_connection + @pool.release_connection + end + def disconnect! - if @connection - @connection.disconnect! - @connection = nil - end - rescue + @pool.disconnect! end - def modify_spec(port) + private + + def modify_spec modify_spec = {"host" => @hostname} - return modify_spec unless port - modify_spec["port"] = port.to_i if port.is_a?(Integer) || !port.empty? + 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