module Turnstile::Redis::Connection

Attributes

config[RW]

Public Instance Methods

exec(redis_method, *args, &block) click to toggle source
# File lib/turnstile/redis/connection.rb, line 12
def exec(redis_method, *args, &block)
  send_proc = redis_method if redis_method.respond_to?(:call)
  send_proc ||= ->(redis) { redis.send(redis_method, *args, &block) }
  with_redis { |redis| send_proc.call(redis) }
end
on_error(e) click to toggle source
# File lib/turnstile/redis/connection.rb, line 77
def on_error(e)
  raise Error.new(e)
end
pool() click to toggle source

Connection pool to the Redis server

# File lib/turnstile/redis/connection.rb, line 73
def pool
  @pool ||= ::ConnectionPool.new(size: config.redis.pool_size, &redis_proc)
end
redis_opts(opts) click to toggle source
# File lib/turnstile/redis/connection.rb, line 89
def redis_opts(opts)
  opts.tap do |hash|
    hash[:driver] = 'hiredis' if config.redis_use_hiredis
  end
end
redis_proc() click to toggle source

This is how we'll be creating redis; depending on input arguments:

# File lib/turnstile/redis/connection.rb, line 68
def redis_proc
  @redis_proc ||= (config.redis.url ? redis_proc_from_url : redis_proc_from_opts)
end
redis_proc_from_opts() click to toggle source
# File lib/turnstile/redis/connection.rb, line 95
def redis_proc_from_opts
  @redis_proc_from_opts ||= proc {
    ::Redis.new(redis_opts(host: config.redis.host,
                           port: config.redis.port,
                           db:   config.redis.db)).tap do |conn|
      log_connection(conn)
    end
  }
end
redis_proc_from_url() click to toggle source
# File lib/turnstile/redis/connection.rb, line 81
def redis_proc_from_url
  @redis_proc_from_url ||= proc do
    ::Redis.new(redis_opts(url: config.redis.url)).tap do |conn|
      log_connection(conn)
    end
  end
end
with_multi() { |redis| ... } click to toggle source
# File lib/turnstile/redis/connection.rb, line 42
def with_multi
  with_retries do
    with_redis do |redis|
      redis.multi do
        yield(redis)
      end
    end
  end
end
with_pipelined() { |redis| ... } click to toggle source
# File lib/turnstile/redis/connection.rb, line 32
def with_pipelined
  with_retries do
    with_redis do |redis|
      redis.pipelined do
        yield(redis)
      end
    end
  end
end
with_redis() { |debug? ? logging_redis: redis| ... } click to toggle source
# File lib/turnstile/redis/connection.rb, line 24
def with_redis
  with_retries do
    pool.with do |redis|
      yield(Turnstile.debug? ? LoggingRedis.new(redis) : redis)
    end
  end
end
with_retries(tries = 3) { |tries| ... } click to toggle source
# File lib/turnstile/redis/connection.rb, line 52
def with_retries(tries = 3)
  yield(tries)
rescue Errno::EINVAL => e
  on_error e
rescue ::Redis::BaseConnectionError => e
  if (tries -= 1) > 0
    sleep rand(0..0.01)
    retry
  else
    on_error e
  end
rescue ::Redis::CommandError => e
  (e.message =~ /loading/i || e.message =~ /connection/i) ? on_error(e) : raise(e)
end

Private Instance Methods

log_connection(conn) click to toggle source
# File lib/turnstile/redis/connection.rb, line 107
def log_connection(conn)
  if config.trace
    tdb "created a new redis connection: #{conn}"
    tdb "driver: #{conn.instance_variable_get(:@client).driver}"
  end
end