class RedisHA::Connection

Attributes

addr[RW]
read_buffer[RW]
status[RW]
write_buffer[RW]

Public Class Methods

new(redis, pool) click to toggle source
Calls superclass method
# File lib/redis_ha/connection.rb, line 4
def initialize(redis, pool)
  @write_buffer = ""
  @read_buffer = ""
  @response_offset = 0

  super(AF_INET, SOCK_STREAM, 0)

  @redis = redis
  @pool = pool
  setup(redis)
end

Public Instance Methods

<<(buf) click to toggle source
# File lib/redis_ha/connection.rb, line 51
def <<(buf)
  @write_buffer << buf
end
check() click to toggle source
# File lib/redis_ha/connection.rb, line 105
def check
  if RedisHA::Protocol.peek?(@read_buffer)
    @ready = true
  end

  finish(:success) if @ready
  @ready
end
execution_expired() click to toggle source
# File lib/redis_ha/connection.rb, line 75
def execution_expired
  finish(:fail)
end
finish(stat) click to toggle source
# File lib/redis_ha/connection.rb, line 93
def finish(stat)
  @ready = true

  if stat == :success
    @down_since = nil if @status != :up
    @status = :up
  else
    @status = :down
    @down_since = Time.now.to_f
  end
end
next() click to toggle source
# File lib/redis_ha/connection.rb, line 60
def next
  @response_offset -= 1
  RedisHA::Protocol.parse(@read_buffer)
end
ready?() click to toggle source
# File lib/redis_ha/connection.rb, line 79
def ready?
  if @ready && @response_offset > 0
    self.next; @ready = false; check
  end

  !!@ready
end
rewind() click to toggle source
# File lib/redis_ha/connection.rb, line 55
def rewind
  @ready = false
  @response_offset -= 0
end
setup(redis) click to toggle source
# File lib/redis_ha/connection.rb, line 87
def setup(redis)
  addr = [redis.fetch(:port), redis.fetch(:host)]
  addr[1] = (TCPSocket.gethostbyname(addr[1]).last)
  @__addr = Socket.pack_sockaddr_in(*addr)
end
up_or_retry?() click to toggle source
# File lib/redis_ha/connection.rb, line 114
def up_or_retry?
  return true if @status == :up
  return true unless @down_since

  down_diff = Time.now.to_f - @down_since
  return true if down_diff > @pool.retry_timeout
  false
end
wait_read?() click to toggle source
# File lib/redis_ha/connection.rb, line 65
def wait_read?
  return false if @ready
  @write_buffer.size == 0
end
wait_write?() click to toggle source
# File lib/redis_ha/connection.rb, line 70
def wait_write?
  return false if @ready
  @write_buffer.size != 0
end
yield_connect() click to toggle source
# File lib/redis_ha/connection.rb, line 16
def yield_connect
  if @redis[:db] && !@db_selected
    @db_selected = true
    @response_offset += 1
    self << RedisHA::Protocol.request("select", @redis[:db])
  end

  connect_nonblock(@__addr)
rescue Errno::EINPROGRESS, Errno::ECONNABORTED, Errno::EINVAL
  nil
rescue Errno::ECONNREFUSED
  finish(:fail)
end
yield_read() click to toggle source
# File lib/redis_ha/connection.rb, line 30
def yield_read
  loop do
    @read_buffer << read_nonblock(1)[0]
  end
rescue Errno::EAGAIN
  check || raise(Errno::EAGAIN)
rescue Errno::ENOTCONN
  yield_connect
rescue Errno::ECONNREFUSED
  finish(:fail)
end
yield_write() click to toggle source
# File lib/redis_ha/connection.rb, line 42
def yield_write
  len = write_nonblock(@write_buffer)
  @write_buffer = @write_buffer[len..-1] || ""
rescue Errno::EPIPE
  yield_connect
rescue Errno::ECONNREFUSED
  finish(:fail)
end