Skip to content

Commit 548fce7

Browse files
committed
Fix connection inconsistencies on all drivers.
1 parent f9fbe5c commit 548fce7

File tree

5 files changed

+10
-36
lines changed

5 files changed

+10
-36
lines changed

lib/redis/client.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def initialize(options = {})
7777
@connection = nil
7878
@command_map = {}
7979

80+
@pending_reads = 0
81+
8082
if options.include?(:sentinels)
8183
@connector = Connector::Sentinel.new(@options)
8284
else
@@ -243,12 +245,15 @@ def io
243245

244246
def read
245247
io do
246-
connection.read
248+
value = connection.read
249+
@pending_reads -= 1
250+
value
247251
end
248252
end
249253

250254
def write(command)
251255
io do
256+
@pending_reads += 1
252257
connection.write(command)
253258
end
254259
end
@@ -315,6 +320,7 @@ def establish_connection
315320
@options[:port] = server[:port]
316321

317322
@connection = @options[:driver].connect(server)
323+
@pending_reads = 0
318324
rescue TimeoutError,
319325
Errno::ECONNREFUSED,
320326
Errno::EHOSTDOWN,
@@ -326,6 +332,8 @@ def establish_connection
326332
end
327333

328334
def ensure_connected
335+
disconnect if @pending_reads > 0
336+
329337
attempts = 0
330338

331339
begin
@@ -342,10 +350,7 @@ def ensure_connected
342350
connect
343351
end
344352

345-
connection.use { yield }
346-
rescue ConnectionCorruptedError
347-
disconnect
348-
retry
353+
yield
349354
rescue BaseConnectionError
350355
disconnect
351356

lib/redis/connection/hiredis.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def read
5656
rescue RuntimeError => err
5757
raise ProtocolError.new(err.message)
5858
end
59-
60-
def use
61-
yield
62-
end
6359
end
6460
end
6561
end

lib/redis/connection/ruby.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ def get_tcp_keepalive
246246

247247
def initialize(sock)
248248
@sock = sock
249-
@pending_reads = 0
250249
end
251250

252251
def connected?
@@ -267,7 +266,6 @@ def timeout=(timeout)
267266
end
268267

269268
def write(command)
270-
@pending_reads += 1
271269
@sock.write(build_command(command))
272270
end
273271

@@ -292,22 +290,18 @@ def format_reply(reply_type, line)
292290
end
293291

294292
def format_error_reply(line)
295-
@pending_reads -= 1
296293
CommandError.new(line.strip)
297294
end
298295

299296
def format_status_reply(line)
300-
@pending_reads -= 1
301297
line.strip
302298
end
303299

304300
def format_integer_reply(line)
305-
@pending_reads -= 1
306301
line.to_i
307302
end
308303

309304
def format_bulk_reply(line)
310-
@pending_reads -= 1
311305
bulklen = line.to_i
312306
return if bulklen == -1
313307
reply = encode(@sock.read(bulklen))
@@ -317,20 +311,10 @@ def format_bulk_reply(line)
317311

318312
def format_multi_bulk_reply(line)
319313
n = line.to_i
320-
321-
@pending_reads += n
322-
323314
return if n == -1
324315

325-
@pending_reads -= 1
326-
327316
Array.new(n) { read }
328317
end
329-
330-
def use
331-
raise ConnectionCorruptedError if @pending_reads > 0
332-
yield
333-
end
334318
end
335319
end
336320
end

lib/redis/connection/synchrony.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ def read
117117
raise "Unknown type #{type.inspect}"
118118
end
119119
end
120-
121-
def use
122-
yield
123-
end
124120
end
125121
end
126122
end

lib/redis/errors.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,4 @@ class TimeoutError < BaseConnectionError
3737
# Raised when the connection was inherited by a child process.
3838
class InheritedError < BaseConnectionError
3939
end
40-
41-
# Raised when the connection is left in an inconsistent state
42-
# due to timeout issues. See issue #501.
43-
#
44-
# Only used internally.
45-
class ConnectionCorruptedError < BaseConnectionError
46-
end
4740
end

0 commit comments

Comments
 (0)