Skip to content

Commit

Permalink
Check if the connection is open inside the lock
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Oct 12, 2020
1 parent 6afb71d commit efd51ba
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions aiokef/aiokef.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,21 @@ def is_connected(self) -> bool:
return (self._reader, self._writer) != (None, None)

async def open_connection(self) -> None:
if self.is_connected:
if self._writer.is_closing(): # type: ignore
_LOGGER.debug(
"%s: Connection closing but did not disconnect", self.host
)
await self._disconnect()
else:
_LOGGER.debug("%s: Connection is still alive", self.host)
return
retries = 0
while retries < _MAX_CONNECTION_RETRIES:
_LOGGER.debug("%s: Opening connection", self.host)

try:
async with self._lock, timeout(_TIMEOUT):
if self.is_connected:
if self._writer.is_closing(): # type: ignore
_LOGGER.debug(
"%s: Connection closing but did not disconnect",
self.host,
)
await self._disconnect(use_lock=False)
else:
_LOGGER.debug("%s: Connection is still alive", self.host)
return
_LOGGER.debug("%s: Opening connection", self.host)
self._reader, self._writer = await asyncio.open_connection(
self.host, self.port, family=socket.AF_INET
)
Expand All @@ -274,6 +274,7 @@ async def open_connection(self) -> None:
await asyncio.sleep(0.5)
except BlockingIOError: # Connection incoming
# XXX: I have never seen this.
_LOGGER.debug("%s: BlockingIOError", self.host)
retries = 0
await asyncio.sleep(1)
except (asyncio.TimeoutError, OSError) as e: # Host is down
Expand Down

0 comments on commit efd51ba

Please sign in to comment.