Module: SimpleFeed::Providers::Redis::Driver

Included in:
Provider
Defined in:
lib/simplefeed/providers/redis/driver.rb

Defined Under Namespace

Classes: Error, LoggingRedis, MockRedis

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#poolObject

Returns the value of attribute pool


72
73
74
# File 'lib/simplefeed/providers/redis/driver.rb', line 72

def pool
  @pool
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/simplefeed/providers/redis/driver.rb', line 68

def debug?
  SimpleFeed::Providers::Redis.debug?
end

#exec(redis_method, *args, **opts, &block) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/simplefeed/providers/redis/driver.rb', line 120

def exec(redis_method, *args, **opts, &block)
  send_proc = redis_method if redis_method.respond_to?(:call)
  send_proc ||= ->(redis) { redis.send(redis_method, *args, &block) }

  if opts[:pipelined]
    opts.delete :pipelined
    with_pipelined { |redis| send_proc.call(redis) }
  else
    with_redis { |redis| send_proc.call(redis) }
  end
end

#initialize(**opts) ⇒ Object

Various ways of defining a new Redis driver:

SimpleFeed::Redis::Driver.new(pool: ConnectionPool.new(size: 2) { Redis.new }) SimpleFeed::Redis::Driver.new(redis: -> { Redis.new }, pool_size: 2) SimpleFeed::Redis::Driver.new(redis: Redis.new) SimpleFeed::Redis::Driver.new(redis: { host: 'localhost', port: 6379, db: 1, timeout: 0.2 }, pool_size: 1)

Raises:

  • (ArgumentError)

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/simplefeed/providers/redis/driver.rb', line 84

def initialize(**opts)
  if opts[:pool] && opts[:pool].respond_to?(:with)
    self.pool = opts[:pool]

  elsif opts[:redis]
    redis      = opts[:redis]
    redis_proc = nil

    if redis.is_a?(::Hash)
      redis_proc = -> { ::Redis.new(**opts[:redis]) }
    elsif redis.is_a?(::Proc)
      redis_proc = redis
    elsif redis.is_a?(::Redis)
      redis_proc = -> { redis }
    end

    if redis_proc
      self.pool = ::ConnectionPool.new(size: (opts[:pool_size] || 2)) do
        redis_proc.call
      end
    end
  end

  raise ArgumentError, "Unable to construct Redis connection from arguments: #{opts.inspect}" unless self.pool && self.pool.respond_to?(:with)
end

#on_error(e) ⇒ Object

Raises:


181
182
183
# File 'lib/simplefeed/providers/redis/driver.rb', line 181

def on_error(e)
  raise Error.new(e)
end

#with_multiObject


156
157
158
159
160
161
162
163
164
# File 'lib/simplefeed/providers/redis/driver.rb', line 156

def with_multi
  with_retries do
    with_redis do |redis|
      redis.multi do
        yield(redis)
      end
    end
  end
end

#with_pipelinedObject


146
147
148
149
150
151
152
153
154
# File 'lib/simplefeed/providers/redis/driver.rb', line 146

def with_pipelined
  with_retries do
    with_redis do |redis|
      redis.pipelined do
        yield(redis)
      end
    end
  end
end

#with_redisObject


138
139
140
141
142
143
144
# File 'lib/simplefeed/providers/redis/driver.rb', line 138

def with_redis
  with_retries do
    pool.with do |redis|
      yield(self.debug? ? LoggingRedis.new(redis) : redis)
    end
  end
end

#with_retries(tries = 3) ⇒ Object


166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/simplefeed/providers/redis/driver.rb', line 166

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