File tree Expand file tree Collapse file tree 5 files changed +35
-1
lines changed Expand file tree Collapse file tree 5 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -342,7 +342,10 @@ def ensure_connected
342
342
connect
343
343
end
344
344
345
- yield
345
+ connection . use { yield }
346
+ rescue ConnectionCorruptedError
347
+ disconnect
348
+ retry
346
349
rescue BaseConnectionError
347
350
disconnect
348
351
Original file line number Diff line number Diff line change @@ -56,6 +56,10 @@ def read
56
56
rescue RuntimeError => err
57
57
raise ProtocolError . new ( err . message )
58
58
end
59
+
60
+ def use
61
+ yield
62
+ end
59
63
end
60
64
end
61
65
end
Original file line number Diff line number Diff line change @@ -246,6 +246,7 @@ def get_tcp_keepalive
246
246
247
247
def initialize ( sock )
248
248
@sock = sock
249
+ @pending_reads = 0
249
250
end
250
251
251
252
def connected?
@@ -266,6 +267,7 @@ def timeout=(timeout)
266
267
end
267
268
268
269
def write ( command )
270
+ @pending_reads += 1
269
271
@sock . write ( build_command ( command ) )
270
272
end
271
273
@@ -290,18 +292,22 @@ def format_reply(reply_type, line)
290
292
end
291
293
292
294
def format_error_reply ( line )
295
+ @pending_reads -= 1
293
296
CommandError . new ( line . strip )
294
297
end
295
298
296
299
def format_status_reply ( line )
300
+ @pending_reads -= 1
297
301
line . strip
298
302
end
299
303
300
304
def format_integer_reply ( line )
305
+ @pending_reads -= 1
301
306
line . to_i
302
307
end
303
308
304
309
def format_bulk_reply ( line )
310
+ @pending_reads -= 1
305
311
bulklen = line . to_i
306
312
return if bulklen == -1
307
313
reply = encode ( @sock . read ( bulklen ) )
@@ -311,10 +317,20 @@ def format_bulk_reply(line)
311
317
312
318
def format_multi_bulk_reply ( line )
313
319
n = line . to_i
320
+
321
+ @pending_reads += n
322
+
314
323
return if n == -1
315
324
325
+ @pending_reads -= 1
326
+
316
327
Array . new ( n ) { read }
317
328
end
329
+
330
+ def use
331
+ raise ConnectionCorruptedError if @pending_reads > 0
332
+ yield
333
+ end
318
334
end
319
335
end
320
336
end
Original file line number Diff line number Diff line change @@ -117,6 +117,10 @@ def read
117
117
raise "Unknown type #{ type . inspect } "
118
118
end
119
119
end
120
+
121
+ def use
122
+ yield
123
+ end
120
124
end
121
125
end
122
126
end
Original file line number Diff line number Diff line change @@ -37,4 +37,11 @@ class TimeoutError < BaseConnectionError
37
37
# Raised when the connection was inherited by a child process.
38
38
class InheritedError < BaseConnectionError
39
39
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
40
47
end
You can’t perform that action at this time.
0 commit comments