Skip to content

Commit 72d7616

Browse files
authored
Enable automatic retry on redis errors (#4595)
* Enable automatic retry on redis errors ExponentialBackoff 3x retry for BusyLoadingError, ConnectionError, and TimeoutError * retry on any redis error * Use default single-retry for any RedisError Using the default Retry means that async and sync clients get the appropriate type of Retry
1 parent eae15e3 commit 72d7616

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

reflex/utils/prerequisites.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
from alembic.util.exc import CommandError
2929
from packaging import version
3030
from redis import Redis as RedisSync
31-
from redis import exceptions
3231
from redis.asyncio import Redis
32+
from redis.exceptions import RedisError
3333

3434
from reflex import constants, model
3535
from reflex.compiler import templates
@@ -333,10 +333,11 @@ def get_redis() -> Redis | None:
333333
Returns:
334334
The asynchronous redis client.
335335
"""
336-
if isinstance((redis_url_or_options := parse_redis_url()), str):
337-
return Redis.from_url(redis_url_or_options)
338-
elif isinstance(redis_url_or_options, dict):
339-
return Redis(**redis_url_or_options)
336+
if (redis_url := parse_redis_url()) is not None:
337+
return Redis.from_url(
338+
redis_url,
339+
retry_on_error=[RedisError],
340+
)
340341
return None
341342

342343

@@ -346,14 +347,15 @@ def get_redis_sync() -> RedisSync | None:
346347
Returns:
347348
The synchronous redis client.
348349
"""
349-
if isinstance((redis_url_or_options := parse_redis_url()), str):
350-
return RedisSync.from_url(redis_url_or_options)
351-
elif isinstance(redis_url_or_options, dict):
352-
return RedisSync(**redis_url_or_options)
350+
if (redis_url := parse_redis_url()) is not None:
351+
return RedisSync.from_url(
352+
redis_url,
353+
retry_on_error=[RedisError],
354+
)
353355
return None
354356

355357

356-
def parse_redis_url() -> str | dict | None:
358+
def parse_redis_url() -> str | None:
357359
"""Parse the REDIS_URL in config if applicable.
358360
359361
Returns:
@@ -387,7 +389,7 @@ async def get_redis_status() -> dict[str, bool | None]:
387389
redis_client.ping()
388390
else:
389391
status = None
390-
except exceptions.RedisError:
392+
except RedisError:
391393
status = False
392394

393395
return {"redis": status}

0 commit comments

Comments
 (0)