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
#pool ⇒ Object
Returns the value of attribute pool.
75
76
77
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 75
def pool
@pool
end
|
Instance Method Details
#exec(redis_method, *args, **_opts, &block) ⇒ Object
121
122
123
124
125
126
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 121
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) }
with_redis { |redis| send_proc.call(redis) }
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)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 85
def initialize(opts)
if 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 pool&.respond_to?(:with)
end
|
#on_error(e) ⇒ Object
175
176
177
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 175
def on_error(e)
raise Error, e
end
|
#with_multi ⇒ Object
150
151
152
153
154
155
156
157
158
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 150
def with_multi
with_retries do
with_redis do |redis|
redis.multi do
yield(redis)
end
end
end
end
|
#with_pipelined(&block) ⇒ Object
142
143
144
145
146
147
148
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 142
def with_pipelined(&block)
with_retries do
with_redis do |redis|
redis.pipelined(&block)
end
end
end
|
#with_redis ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 134
def with_redis
with_retries do
pool.with do |redis|
yield(debug? ? LoggingRedis.new(redis) : redis)
end
end
end
|
#with_retries(tries = 3) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/simplefeed/providers/redis/driver.rb', line 160
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
|