class RedisHA::ConnectionPool

Constants

DEFAULT_READ_TIMEOUT

timeout after which a redis connection is considered down. the default is 500ms

DEFAULT_RETRY_TIMEOUT

timeout after which a redis that was marked as down is retried the default is 5s

Attributes

connections[RW]
read_timeout[RW]
retry_timeout[RW]

Public Class Methods

new() click to toggle source
# File lib/redis_ha/connection_pool.rb, line 13
def initialize
  @read_timeout  = DEFAULT_READ_TIMEOUT
  @retry_timeout = DEFAULT_RETRY_TIMEOUT

  @connections = []
end

Public Instance Methods

connect(*conns) click to toggle source
# File lib/redis_ha/connection_pool.rb, line 20
def connect(*conns)
  conns.each do |conn|
    @connections << RedisHA::Connection.new(conn, self)
    @connections.last.yield_connect
  end
end
method_missing(*msg) click to toggle source
# File lib/redis_ha/connection_pool.rb, line 27
def method_missing(*msg)
  msg = msg.map(&:to_s)
  req = RedisHA::Protocol.request(*msg)
  execute(req)
end

Private Instance Methods

await() click to toggle source
# File lib/redis_ha/connection_pool.rb, line 76
def await
  loop do
    begin
      await = false
      select

      @connections.each do |conn|
        next unless conn.up_or_retry?
        await = true unless conn.ready?
      end

      break unless await
    rescue Errno::EAGAIN, Errno::EINTR
      next
    end
  end
end
execute(cmd) click to toggle source
# File lib/redis_ha/connection_pool.rb, line 35
def execute(cmd)
  @connections.each do |c|
    c.rewind
    c << cmd
  end

  await

  @connections.map do |conn|
    res = conn.next

    if res.is_a?(Exception)
      @connections.each(&:rewind)
      raise res
    else
      res
    end
  end
end
select() click to toggle source
# File lib/redis_ha/connection_pool.rb, line 55
def select
  req = [[],[],[]]

  @connections.each do |c|
    req[0] << c if c.wait_read?
    req[1] << c if c.wait_write?
  end

  req << @read_timeout
  ready = IO.select(*req)

  unless ready
    req[0].each(&:execution_expired)
    req[1].each(&:execution_expired)
    return
  end

  ready[0].each(&:yield_read)
  ready[1].each(&:yield_write)
end