Skip to content

Commit

Permalink
refactor: unify ping method code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik committed Jul 15, 2024
1 parent e7ee4f5 commit d55cad2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
12 changes: 7 additions & 5 deletions faststream/kafka/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions faststream/nats/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions faststream/rabbit/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions faststream/redis/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit d55cad2

Please sign in to comment.