Skip to content

Commit 5dff4ef

Browse files
w-millerdvora-h
andcommitted
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 <[email protected]>
1 parent 7fa3bd4 commit 5dff4ef

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

redis/asyncio/client.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,15 @@ async def connect(self):
870870
async def _disconnect_raise_connect(self, conn, error):
871871
"""
872872
Close the connection and raise an exception
873-
if retry_on_timeout is not set or the error
874-
is not a TimeoutError. Otherwise, try to reconnect
873+
if retry_on_error is not set or the error is not one
874+
of the specified error types. Otherwise, try to
875+
reconnect
875876
"""
876877
await conn.disconnect()
877-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
878+
if (
879+
conn.retry_on_error is None
880+
or isinstance(error, tuple(conn.retry_on_error)) is False
881+
):
878882
raise error
879883
await conn.connect()
880884

@@ -1286,8 +1290,8 @@ async def _disconnect_reset_raise(self, conn, error):
12861290
"""
12871291
Close the connection, reset watching state and
12881292
raise an exception if we were watching,
1289-
retry_on_timeout is not set,
1290-
or the error is not a TimeoutError
1293+
if retry_on_error is not set or the error is not one
1294+
of the specified error types.
12911295
"""
12921296
await conn.disconnect()
12931297
# if we were already watching a variable, the watch is no longer
@@ -1298,9 +1302,12 @@ async def _disconnect_reset_raise(self, conn, error):
12981302
raise WatchError(
12991303
"A ConnectionError occurred on while watching one or more keys"
13001304
)
1301-
# if retry_on_timeout is not set, or the error is not
1302-
# a TimeoutError, raise it
1303-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1305+
# if retry_on_error is not set or the error is not one
1306+
# of the specified error types, raise it
1307+
if (
1308+
conn.retry_on_error is None
1309+
or isinstance(error, tuple(conn.retry_on_error)) is False
1310+
):
13041311
await self.aclose()
13051312
raise
13061313

@@ -1475,8 +1482,8 @@ async def load_scripts(self):
14751482
async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
14761483
"""
14771484
Close the connection, raise an exception if we were watching,
1478-
and raise an exception if retry_on_timeout is not set,
1479-
or the error is not a TimeoutError
1485+
and raise an exception if retry_on_error is not set or the
1486+
error is not one of the specified error types.
14801487
"""
14811488
await conn.disconnect()
14821489
# if we were watching a variable, the watch is no longer valid
@@ -1486,9 +1493,12 @@ async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
14861493
raise WatchError(
14871494
"A ConnectionError occurred on while watching one or more keys"
14881495
)
1489-
# if retry_on_timeout is not set, or the error is not
1490-
# a TimeoutError, raise it
1491-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1496+
# if retry_on_error is not set or the error is not one
1497+
# of the specified error types, raise it
1498+
if (
1499+
conn.retry_on_error is None
1500+
or isinstance(error, tuple(conn.retry_on_error)) is False
1501+
):
14921502
await self.reset()
14931503
raise
14941504

redis/client.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
SentinelCommands,
2020
list_or_args,
2121
)
22-
from redis.connection import ConnectionPool, SSLConnection, UnixDomainSocketConnection
22+
from redis.connection import (
23+
AbstractConnection,
24+
ConnectionPool,
25+
SSLConnection,
26+
UnixDomainSocketConnection,
27+
)
2328
from redis.credentials import CredentialProvider
2429
from redis.exceptions import (
2530
ConnectionError,
@@ -783,11 +788,15 @@ def clean_health_check_responses(self) -> None:
783788
def _disconnect_raise_connect(self, conn, error) -> None:
784789
"""
785790
Close the connection and raise an exception
786-
if retry_on_timeout is not set or the error
787-
is not a TimeoutError. Otherwise, try to reconnect
791+
if retry_on_error is not set or the error is not one
792+
of the specified error types. Otherwise, try to
793+
reconnect
788794
"""
789795
conn.disconnect()
790-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
796+
if (
797+
conn.retry_on_error is None
798+
or isinstance(error, tuple(conn.retry_on_error)) is False
799+
):
791800
raise error
792801
conn.connect()
793802

@@ -1263,8 +1272,8 @@ def _disconnect_reset_raise(self, conn, error) -> None:
12631272
"""
12641273
Close the connection, reset watching state and
12651274
raise an exception if we were watching,
1266-
retry_on_timeout is not set,
1267-
or the error is not a TimeoutError
1275+
if retry_on_error is not set or the error is not one
1276+
of the specified error types.
12681277
"""
12691278
conn.disconnect()
12701279
# if we were already watching a variable, the watch is no longer
@@ -1275,9 +1284,12 @@ def _disconnect_reset_raise(self, conn, error) -> None:
12751284
raise WatchError(
12761285
"A ConnectionError occurred on while watching one or more keys"
12771286
)
1278-
# if retry_on_timeout is not set, or the error is not
1279-
# a TimeoutError, raise it
1280-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1287+
# if retry_on_error is not set or the error is not one
1288+
# of the specified error types, raise it
1289+
if (
1290+
conn.retry_on_error is None
1291+
or isinstance(error, tuple(conn.retry_on_error)) is False
1292+
):
12811293
self.reset()
12821294
raise
12831295

@@ -1435,11 +1447,15 @@ def load_scripts(self):
14351447
if not exist:
14361448
s.sha = immediate("SCRIPT LOAD", s.script)
14371449

1438-
def _disconnect_raise_reset(self, conn: Redis, error: Exception) -> None:
1450+
def _disconnect_raise_reset(
1451+
self,
1452+
conn: AbstractConnection,
1453+
error: Exception,
1454+
) -> None:
14391455
"""
14401456
Close the connection, raise an exception if we were watching,
1441-
and raise an exception if TimeoutError is not part of retry_on_error,
1442-
or the error is not a TimeoutError
1457+
and raise an exception if retry_on_error is not set or the
1458+
error is not one of the specified error types.
14431459
"""
14441460
conn.disconnect()
14451461
# if we were watching a variable, the watch is no longer valid
@@ -1449,11 +1465,13 @@ def _disconnect_raise_reset(self, conn: Redis, error: Exception) -> None:
14491465
raise WatchError(
14501466
"A ConnectionError occurred on while watching one or more keys"
14511467
)
1452-
# if TimeoutError is not part of retry_on_error, or the error
1453-
# is not a TimeoutError, raise it
1454-
if not (
1455-
TimeoutError in conn.retry_on_error and isinstance(error, TimeoutError)
1468+
# if retry_on_error is not set or the error is not one
1469+
# of the specified error types, raise it
1470+
if (
1471+
conn.retry_on_error is None
1472+
or isinstance(error, tuple(conn.retry_on_error)) is False
14561473
):
1474+
14571475
self.reset()
14581476
raise error
14591477

0 commit comments

Comments
 (0)