class Redisent

Constants

ECONN

Attributes

healthy[R]
hosts[R]
invalid[R]
prime[R]
scout[R]
unknown[R]

Public Class Methods

new(hosts:, name:, client: Redic, auth: nil) click to toggle source
# File lib/redisent.rb, line 19
def initialize(hosts:, name:, client: Redic, auth: nil)
  @name = name
  @auth = auth

  # Client library
  @client = client

  # Hosts according to availability
  @healthy = []
  @invalid = []
  @unknown = []

  # Last known healthy hosts
  @hosts = hosts

  # Primary client
  @prime = @client.new

  # Scout client
  @scout = @client.new

  explore!
end

Public Instance Methods

call(*args) click to toggle source
# File lib/redisent.rb, line 93
def call(*args)
  forward do
    @prime.call(*args)
  end
end
call!(*args) click to toggle source
# File lib/redisent.rb, line 99
def call!(*args)
  forward do
    @prime.call!(*args)
  end
end
commit() click to toggle source
# File lib/redisent.rb, line 109
def commit
  buffer = @prime.buffer

  forward do
    @prime.buffer.replace(buffer)
    @prime.commit
  end
end
forward() { || ... } click to toggle source
# File lib/redisent.rb, line 118
def forward
  yield
rescue
  explore!
  retry
end
queue(*args) click to toggle source
# File lib/redisent.rb, line 105
def queue(*args)
  @prime.queue(*args)
end
url() click to toggle source
# File lib/redisent.rb, line 43
def url
  @prime.url
end

Private Instance Methods

explore!() click to toggle source
# File lib/redisent.rb, line 47
        def explore!
  @unknown = []
  @invalid = []
  @healthy = []

  @hosts.each do |host|
    begin
      @scout.configure(sentinel_url(host))

      sentinels = @scout.call("SENTINEL", "sentinels", @name)

      if RuntimeError === sentinels
        unknown.push(host)
      else
        healthy.push(host)

        sentinels.each do |sentinel|
          info = Hash[*sentinel]

          healthy.push(sprintf("%s:%s", info["ip"], info["port"]))
        end
      end

      @scout.quit

    rescue *ECONN
      invalid.push(host)
    end
  end

  if healthy.any?
    healthy.uniq!
    @hosts.replace(healthy)
    @prime.configure(master)
    return true
  end

  if invalid.any?
    raise UnreachableHosts, invalid
  end

  if unknown.any?
    raise UnknownMaster, @name
  end
end
master() click to toggle source
# File lib/redisent.rb, line 137
        def master
  hosts.each do |host|
    begin
      @scout.configure(sentinel_url(host))
      ip, port = @scout.call("SENTINEL", "get-master-addr-by-name", @name)

      break redis_url(sprintf("%s:%s", ip, port))
    rescue *ECONN
      $stderr.puts($!.inspect)
    end
  end
end
redis_url(host) click to toggle source
# File lib/redisent.rb, line 129
        def redis_url(host)
  if @auth then
    sprintf("redis://:%s@%s", @auth, host)
  else
    sprintf("redis://%s", host)
  end
end
sentinel_url(host) click to toggle source
# File lib/redisent.rb, line 125
        def sentinel_url(host)
  sprintf("redis://%s", host)
end