From d55cad257c3f57a6c07503c7bc7d44da3d711ef3 Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Mon, 15 Jul 2024 22:14:31 +0300 Subject: [PATCH] refactor: unify ping method code --- faststream/kafka/broker/broker.py | 12 +++++++----- faststream/nats/broker/broker.py | 7 +++++-- faststream/rabbit/broker/broker.py | 6 ++++-- faststream/redis/broker/broker.py | 7 +++++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/faststream/kafka/broker/broker.py b/faststream/kafka/broker/broker.py index d0f5b56945..9eb9d8bd1b 100644 --- a/faststream/kafka/broker/broker.py +++ b/faststream/kafka/broker/broker.py @@ -811,8 +811,10 @@ async def publish_batch( @override async def ping(self, timeout: Optional[float]) -> bool: with move_on_after(timeout) as cancel_scope: - return not ( - cancel_scope.cancel_called - or self._producer is None - or self._producer._producer._closed - ) + if cancel_scope.cancel_called: + return False + + if self._producer is None: + return False + + return not self._producer._producer._closed diff --git a/faststream/nats/broker/broker.py b/faststream/nats/broker/broker.py index e0c1546a15..74fa1f0bdf 100644 --- a/faststream/nats/broker/broker.py +++ b/faststream/nats/broker/broker.py @@ -921,5 +921,8 @@ async def ping(self, timeout: Optional[float]) -> bool: with move_on_after(timeout) as cancel_scope: if cancel_scope.cancel_called: return False - else: - return self._connection.is_connected + + if self._connection is None: + return False + + return self._connection.is_connected diff --git a/faststream/rabbit/broker/broker.py b/faststream/rabbit/broker/broker.py index 0374df0852..c12ec28bae 100644 --- a/faststream/rabbit/broker/broker.py +++ b/faststream/rabbit/broker/broker.py @@ -662,6 +662,8 @@ async def ping(self, timeout: Optional[float]) -> bool: with move_on_after(timeout) as cancel_scope: if cancel_scope.cancel_called: return False - if not self._connection or self._connection.is_closed: + + if self._connection is None: return False - return True + + return not self._connection.is_closed diff --git a/faststream/redis/broker/broker.py b/faststream/redis/broker/broker.py index 62e851b396..aebfc1add9 100644 --- a/faststream/redis/broker/broker.py +++ b/faststream/redis/broker/broker.py @@ -484,5 +484,8 @@ async def ping(self, timeout: Optional[float]) -> bool: with move_on_after(timeout) as cancel_scope: if cancel_scope.cancel_called: return False - else: - return await self._connection.ping() + + if self._connection is None: + return False + + return await self._connection.ping()