From 4ea70dee64a6f3e4b5476ab9f9b1419d79133d1a Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Sat, 11 May 2024 20:55:31 +0200 Subject: [PATCH] improve connection handling - insure close() call after graceful disconnect, to release proxy internal resources - timeout handler disconnect inverter connection if no message was received for longer than 2.5 minutes --- app/src/async_stream.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/src/async_stream.py b/app/src/async_stream.py index 7c99373..56f475d 100644 --- a/app/src/async_stream.py +++ b/app/src/async_stream.py @@ -59,28 +59,35 @@ class AsyncStream(): while True: try: - # await asyncio.wait_for(self.__async_read(), 0.3) - await self.__async_read() + if self.state == self.STATE_UP and self.server_side: + await asyncio.wait_for(self.__async_read(), 150) + else: + await self.__async_read() if self.unique_id: await self.async_write() await self.__async_forward() await self.async_publ_mqtt() - except asyncio.TimeoutError: - pass - except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError) as error: logger.error(f'{error} for l{self.l_addr} | ' f'r{self.r_addr}') await self.disc() + self.close() return self except RuntimeError as error: logger.warning(f"{error} for {self.l_addr}") await self.disc() + self.close() + return self + + except asyncio.TimeoutError: + logger.warning(f"Timeout for {self.l_addr}") + await self.disc() + self.close() return self except Exception: