Skip to content

Commit ebb6171

Browse files
w-millerdvora-h
andauthored
Fix retry logic for pubsub and pipeline (#3134)
* Fix retry logic for pubsub and pipeline Extend the fix from bea7299 to apply to pipeline and pubsub as well. Fixes #2973 * fix isort --------- Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
1 parent 2b2a2e0 commit ebb6171

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

redis/asyncio/client.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -927,11 +927,15 @@ async def connect(self):
927927
async def _disconnect_raise_connect(self, conn, error):
928928
"""
929929
Close the connection and raise an exception
930-
if retry_on_timeout is not set or the error
931-
is not a TimeoutError. Otherwise, try to reconnect
930+
if retry_on_error is not set or the error is not one
931+
of the specified error types. Otherwise, try to
932+
reconnect
932933
"""
933934
await conn.disconnect()
934-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
935+
if (
936+
conn.retry_on_error is None
937+
or isinstance(error, tuple(conn.retry_on_error)) is False
938+
):
935939
raise error
936940
await conn.connect()
937941

@@ -1344,8 +1348,8 @@ async def _disconnect_reset_raise(self, conn, error):
13441348
"""
13451349
Close the connection, reset watching state and
13461350
raise an exception if we were watching,
1347-
retry_on_timeout is not set,
1348-
or the error is not a TimeoutError
1351+
if retry_on_error is not set or the error is not one
1352+
of the specified error types.
13491353
"""
13501354
await conn.disconnect()
13511355
# if we were already watching a variable, the watch is no longer
@@ -1356,9 +1360,12 @@ async def _disconnect_reset_raise(self, conn, error):
13561360
raise WatchError(
13571361
"A ConnectionError occurred on while watching one or more keys"
13581362
)
1359-
# if retry_on_timeout is not set, or the error is not
1360-
# a TimeoutError, raise it
1361-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1363+
# if retry_on_error is not set or the error is not one
1364+
# of the specified error types, raise it
1365+
if (
1366+
conn.retry_on_error is None
1367+
or isinstance(error, tuple(conn.retry_on_error)) is False
1368+
):
13621369
await self.aclose()
13631370
raise
13641371

@@ -1533,8 +1540,8 @@ async def load_scripts(self):
15331540
async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
15341541
"""
15351542
Close the connection, raise an exception if we were watching,
1536-
and raise an exception if retry_on_timeout is not set,
1537-
or the error is not a TimeoutError
1543+
and raise an exception if retry_on_error is not set or the
1544+
error is not one of the specified error types.
15381545
"""
15391546
await conn.disconnect()
15401547
# if we were watching a variable, the watch is no longer valid
@@ -1544,9 +1551,12 @@ async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
15441551
raise WatchError(
15451552
"A ConnectionError occurred on while watching one or more keys"
15461553
)
1547-
# if retry_on_timeout is not set, or the error is not
1548-
# a TimeoutError, raise it
1549-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1554+
# if retry_on_error is not set or the error is not one
1555+
# of the specified error types, raise it
1556+
if (
1557+
conn.retry_on_error is None
1558+
or isinstance(error, tuple(conn.retry_on_error)) is False
1559+
):
15501560
await self.reset()
15511561
raise
15521562

redis/client.py

+34-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
SentinelCommands,
2626
list_or_args,
2727
)
28-
from redis.connection import ConnectionPool, SSLConnection, UnixDomainSocketConnection
28+
from redis.connection import (
29+
AbstractConnection,
30+
ConnectionPool,
31+
SSLConnection,
32+
UnixDomainSocketConnection,
33+
)
2934
from redis.credentials import CredentialProvider
3035
from redis.exceptions import (
3136
ConnectionError,
@@ -839,11 +844,15 @@ def clean_health_check_responses(self) -> None:
839844
def _disconnect_raise_connect(self, conn, error) -> None:
840845
"""
841846
Close the connection and raise an exception
842-
if retry_on_timeout is not set or the error
843-
is not a TimeoutError. Otherwise, try to reconnect
847+
if retry_on_error is not set or the error is not one
848+
of the specified error types. Otherwise, try to
849+
reconnect
844850
"""
845851
conn.disconnect()
846-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
852+
if (
853+
conn.retry_on_error is None
854+
or isinstance(error, tuple(conn.retry_on_error)) is False
855+
):
847856
raise error
848857
conn.connect()
849858

@@ -1320,8 +1329,8 @@ def _disconnect_reset_raise(self, conn, error) -> None:
13201329
"""
13211330
Close the connection, reset watching state and
13221331
raise an exception if we were watching,
1323-
retry_on_timeout is not set,
1324-
or the error is not a TimeoutError
1332+
if retry_on_error is not set or the error is not one
1333+
of the specified error types.
13251334
"""
13261335
conn.disconnect()
13271336
# if we were already watching a variable, the watch is no longer
@@ -1332,9 +1341,12 @@ def _disconnect_reset_raise(self, conn, error) -> None:
13321341
raise WatchError(
13331342
"A ConnectionError occurred on while watching one or more keys"
13341343
)
1335-
# if retry_on_timeout is not set, or the error is not
1336-
# a TimeoutError, raise it
1337-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1344+
# if retry_on_error is not set or the error is not one
1345+
# of the specified error types, raise it
1346+
if (
1347+
conn.retry_on_error is None
1348+
or isinstance(error, tuple(conn.retry_on_error)) is False
1349+
):
13381350
self.reset()
13391351
raise
13401352

@@ -1492,11 +1504,15 @@ def load_scripts(self):
14921504
if not exist:
14931505
s.sha = immediate("SCRIPT LOAD", s.script)
14941506

1495-
def _disconnect_raise_reset(self, conn: Redis, error: Exception) -> None:
1507+
def _disconnect_raise_reset(
1508+
self,
1509+
conn: AbstractConnection,
1510+
error: Exception,
1511+
) -> None:
14961512
"""
14971513
Close the connection, raise an exception if we were watching,
1498-
and raise an exception if TimeoutError is not part of retry_on_error,
1499-
or the error is not a TimeoutError
1514+
and raise an exception if retry_on_error is not set or the
1515+
error is not one of the specified error types.
15001516
"""
15011517
conn.disconnect()
15021518
# if we were watching a variable, the watch is no longer valid
@@ -1506,11 +1522,13 @@ def _disconnect_raise_reset(self, conn: Redis, error: Exception) -> None:
15061522
raise WatchError(
15071523
"A ConnectionError occurred on while watching one or more keys"
15081524
)
1509-
# if TimeoutError is not part of retry_on_error, or the error
1510-
# is not a TimeoutError, raise it
1511-
if not (
1512-
TimeoutError in conn.retry_on_error and isinstance(error, TimeoutError)
1525+
# if retry_on_error is not set or the error is not one
1526+
# of the specified error types, raise it
1527+
if (
1528+
conn.retry_on_error is None
1529+
or isinstance(error, tuple(conn.retry_on_error)) is False
15131530
):
1531+
15141532
self.reset()
15151533
raise error
15161534

0 commit comments

Comments
 (0)