class EbisuConnection::Replica

Attributes

hostname[R]
weight[R]

Public Class Methods

new(conf, spec_name) click to toggle source
# File lib/ebisu_connection/replica.rb, line 7
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

Public Instance Methods

active_connection?() click to toggle source
# File lib/ebisu_connection/replica.rb, line 39
def active_connection?
  @pool.active_connection?
end
connection() click to toggle source
# File lib/ebisu_connection/replica.rb, line 28
def connection
  @pool.connection
end
disconnect!() click to toggle source
# File lib/ebisu_connection/replica.rb, line 47
def disconnect!
  @pool.disconnect!
end
put_aside!() click to toggle source
# File lib/ebisu_connection/replica.rb, line 32
def put_aside!
  return unless active_connection?
  return if connection.transaction_open?

  release_connection
end
release_connection() click to toggle source
# File lib/ebisu_connection/replica.rb, line 43
def release_connection
  @pool.release_connection
end

Private Instance Methods

modify_spec() click to toggle source
# File lib/ebisu_connection/replica.rb, line 53
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