Skip to content

Commit

Permalink
Handle cancelled redis connection checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Jul 25, 2024
1 parent f21169a commit 396535f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions starlette_plus/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ async def ping(self) -> bool:
return self._could_connect

async def _health_task(self) -> None:
while True:
previous = self.could_connect
await self.ping()

if not previous and self.could_connect:
logger.info("Redis connection has been (re)established: %s", self.url)

await asyncio.sleep(5)
try:
while True:
previous = self.could_connect
await self.ping()

if not previous and self.could_connect:
logger.info("Redis connection has been (re)established: %s", self.url)

await asyncio.sleep(5)
except asyncio.CancelledError:
logger.warning(
'Redis connection: "%s" health check was cancelled. Safe to ignore on shutdown. '
"If this was not intentional, all middleware relying on this connection will now be in-memory.",
self.url,
)
self._could_connect = False
finally:
await self.pool.aclose()

0 comments on commit 396535f

Please sign in to comment.