-
Notifications
You must be signed in to change notification settings - Fork 1k
/
pipeline.rb
306 lines (246 loc) · 6.7 KB
/
pipeline.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# frozen_string_literal: true
require "delegate"
class Redis
class PipelinedConnection
def initialize(pipeline)
@pipeline = pipeline
end
include Commands
def db
@pipeline.db
end
def db=(db)
@pipeline.db = db
end
def pipelined
yield self
end
def call_pipeline(pipeline)
@pipeline.call_pipeline(pipeline)
nil
end
private
def synchronize
yield self
end
def send_command(command, &block)
@pipeline.call(command, &block)
end
def send_blocking_command(command, timeout, &block)
@pipeline.call_with_timeout(command, timeout, &block)
end
end
class Pipeline
REDIS_INTERNAL_PATH = File.expand_path("..", __dir__).freeze
# Redis use MonitorMixin#synchronize and this class use DelegateClass which we want to filter out.
# Both are in the stdlib so we can simply filter the entire stdlib out.
STDLIB_PATH = File.expand_path("..", MonitorMixin.instance_method(:synchronize).source_location.first).freeze
class << self
def deprecation_warning(method, caller_locations) # :nodoc:
callsite = caller_locations.find { |l| !l.path.start_with?(REDIS_INTERNAL_PATH, STDLIB_PATH) }
callsite ||= caller_locations.last # The caller_locations should be large enough, but just in case.
::Redis.deprecate! <<~MESSAGE
Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0.
redis.#{method} do
redis.get("key")
end
should be replaced by
redis.#{method} do |pipeline|
pipeline.get("key")
end
(called from #{callsite}}
MESSAGE
end
end
attr_accessor :db
attr_reader :client
attr :futures
alias materialized_futures futures
def initialize(client)
@client = client.is_a?(Pipeline) ? client.client : client
@with_reconnect = true
@shutdown = false
@futures = []
end
def timeout
client.timeout
end
def with_reconnect?
@with_reconnect
end
def without_reconnect?
!@with_reconnect
end
def shutdown?
@shutdown
end
def empty?
@futures.empty?
end
def call(command, timeout: nil, &block)
# A pipeline that contains a shutdown should not raise ECONNRESET when
# the connection is gone.
@shutdown = true if command.first == :shutdown
future = Future.new(command, block, timeout)
@futures << future
future
end
def call_with_timeout(command, timeout, &block)
call(command, timeout: timeout, &block)
end
def call_pipeline(pipeline)
@shutdown = true if pipeline.shutdown?
@futures.concat(pipeline.materialized_futures)
@db = pipeline.db
nil
end
def commands
@futures.map(&:_command)
end
def timeouts
@futures.map(&:timeout)
end
def with_reconnect(val = true)
@with_reconnect = false unless val
yield
end
def without_reconnect(&blk)
with_reconnect(false, &blk)
end
def finish(replies, &blk)
if blk
futures.each_with_index.map do |future, i|
future._set(blk.call(replies[i]))
end
else
futures.each_with_index.map do |future, i|
future._set(replies[i])
end
end
end
class Multi < self
def finish(replies)
exec = replies.last
return if exec.nil? # The transaction failed because of WATCH.
# EXEC command failed.
raise exec if exec.is_a?(CommandError)
if exec.size < futures.size
# Some command wasn't recognized by Redis.
command_error = replies.detect { |r| r.is_a?(CommandError) }
raise command_error
end
super(exec) do |reply|
# Because an EXEC returns nested replies, hiredis won't be able to
# convert an error reply to a CommandError instance itself. This is
# specific to MULTI/EXEC, so we solve this here.
reply.is_a?(::RuntimeError) ? CommandError.new(reply.message) : reply
end
end
def materialized_futures
if empty?
[]
else
[
Future.new([:multi], nil, 0),
*futures,
MultiFuture.new(futures)
]
end
end
def timeouts
if empty?
[]
else
[nil, *super, nil]
end
end
def commands
if empty?
[]
else
[[:multi]] + super + [[:exec]]
end
end
end
end
class DeprecatedPipeline < DelegateClass(Pipeline)
def initialize(pipeline)
super(pipeline)
@deprecation_displayed = false
end
def __getobj__
unless @deprecation_displayed
Pipeline.deprecation_warning("pipelined", Kernel.caller_locations(1, 10))
@deprecation_displayed = true
end
@delegate_dc_obj
end
end
class DeprecatedMulti < DelegateClass(Pipeline::Multi)
def initialize(pipeline)
super(pipeline)
@deprecation_displayed = false
end
def __getobj__
unless @deprecation_displayed
Pipeline.deprecation_warning("multi", Kernel.caller_locations(1, 10))
@deprecation_displayed = true
end
@delegate_dc_obj
end
end
class FutureNotReady < RuntimeError
def initialize
super("Value will be available once the pipeline executes.")
end
end
class Future < BasicObject
FutureNotReady = ::Redis::FutureNotReady.new
attr_reader :timeout
def initialize(command, transformation, timeout)
@command = command
@transformation = transformation
@timeout = timeout
@object = FutureNotReady
end
def ==(_other)
message = +"The methods == and != are deprecated for Redis::Future and will be removed in 5.0.0"
message << " - You probably meant to call .value == or .value !="
message << " (#{::Kernel.caller(1, 1).first})\n"
::Redis.deprecate!(message)
super
end
def inspect
"<Redis::Future #{@command.inspect}>"
end
def _set(object)
@object = @transformation ? @transformation.call(object) : object
value
end
def _command
@command
end
def value
::Kernel.raise(@object) if @object.is_a?(::RuntimeError)
@object
end
def is_a?(other)
self.class.ancestors.include?(other)
end
def class
Future
end
end
class MultiFuture < Future
def initialize(futures)
@futures = futures
@command = [:exec]
end
def _set(replies)
@futures.each_with_index do |future, index|
future._set(replies[index])
end
replies
end
end
end