From f9f9d06c9951f8536bf9321dcebc96759eae03e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Wed, 27 Jul 2022 16:21:25 +0000 Subject: automatically reconnect pubsub when reading messages in blocking mode (#2281) * optimistic default info on test sessionstart. Makes test discovery work, even without a redis connection. * Add unittests verifying that (non-async) PubSub will automatically reconnect * Add tests for asyncio pubsub subsciription auto-reconnect * automatically connect for blocking reads (asyncio) * fix automatic connect on blocking pubsub read (non-async) * lint & format * Perform `connect()` call in PubSub code rather than `read_response`. --- redis/asyncio/client.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'redis/asyncio/client.py') diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index 3d59016..9c8caae 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -754,9 +754,15 @@ class PubSub: await self.check_health() - if not block and not await self._execute(conn, conn.can_read, timeout=timeout): - return None - response = await self._execute(conn, conn.read_response) + async def try_read(): + if not block: + if not await conn.can_read(timeout=timeout): + return None + else: + await conn.connect() + return await conn.read_response() + + response = await self._execute(conn, try_read) if conn.health_check_interval and response == self.health_check_response: # ignore the health check message as user might not expect it -- cgit v1.2.1