dealloc async_stream instances in connection termination

- improve close handler
- clearify logging on disconnection
This commit is contained in:
Stefan Allius
2023-10-03 19:44:24 +02:00
parent 0886b30032
commit 6d9be75ce3
2 changed files with 22 additions and 10 deletions

View File

@@ -101,10 +101,14 @@ class AsyncStream(Message):
def close(self):
logger.info(f'in async_stream.close() {self.addr}')
logger.debug(f'in AsyncStream.close() {self.addr}')
self.writer.close()
self.proxy = None
self.remoteStream = None
super().close() # call close handler in the parent class
self.proxy = None # clear our refernce to the proxy, to avoid memory leaks
if self.remoteStream: # if we have knowledge about a remote stream, we del the references between the two streams
self.remoteStream.remoteStream = None
self.remoteStream = None
'''
@@ -154,6 +158,7 @@ class AsyncStream(Message):
self.new_data[key] = False
def __del__ (self):
logger.debug ("AsyncStream __del__")
logger.debug ("AsyncStream __del__")
super().__del__()

View File

@@ -7,21 +7,28 @@ class Proxy:
proxy.ClientStream = None
async def server_loop(proxy, addr):
logging.info(f'Accept connection from {addr}')
'''Loop for receiving messages from the inverter (server-side)'''
logging.info(f'Accept connection from {addr}')
await proxy.ServerStream.loop()
logging.info(f'Close server connection {addr}')
logging.info(f'Server loop stopped for {addr}')
# if the server connection closes, we also disconnect the connection to te TSUN cloud
if proxy.ClientStream:
logging.debug ("close client connection")
proxy.ClientStream.close()
logging.debug ("disconnect client connection")
proxy.ClientStream.disc()
async def client_loop(proxy, addr):
'''Loop for receiving messages from the TSUN cloud (client-side)'''
await proxy.ClientStream.loop()
logging.info(f'Close client connection {addr}')
proxy.ServerStream.remoteStream = None
logging.info(f'Client loop stopped for {addr}')
# if the client connection closes, we don't touch the server connection. Instead we erase the client
# connection stream, thus on the next received packet from the inverter, we can establish a new connection
# to the TSUN cloud
proxy.ClientStream = None
async def CreateClientStream (proxy, stream, host, port):
'''Establish a client connection to the TSUN cloud'''
addr = (host, port)
try: