Skip to content

Commit

Permalink
refactor(src): replace set by list; close stream, not transport
Browse files Browse the repository at this point in the history
  • Loading branch information
christgau committed Dec 26, 2024
1 parent df9b523 commit 7546890
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/wsdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ class ApiServer:
def __init__(self, aio_loop: asyncio.AbstractEventLoop, listen_address: bytes,
address_monitor: 'NetworkAddressMonitor') -> None:
self.server = None
self.clients = set()
self.clients = []
self.address_monitor = address_monitor

# defer server creation
Expand All @@ -1147,7 +1147,7 @@ async def create_server(self, aio_loop: asyncio.AbstractEventLoop, listen_addres
self.on_connect, path=listen_address))

async def on_connect(self, read_stream: asyncio.StreamReader, write_stream: asyncio.StreamWriter) -> None:
self.clients.add(write_stream.transport)
self.clients.append(write_stream)
while True:
try:
line = await read_stream.readline()
Expand All @@ -1156,14 +1156,14 @@ async def on_connect(self, read_stream: asyncio.StreamReader, write_stream: asyn
if not write_stream.is_closing():
await write_stream.drain()
else:
self.clients.discard(write_stream.transport)
self.clients.remove(write_stream)
write_stream.close()
return
except UnicodeDecodeError as e:
logger.debug('invalid input utf8', e)
except Exception as e:
logger.warning('exception in API client', e)
self.clients.discard(write_stream.transport)
self.clients.remove(write_stream)
write_stream.close()
return

Expand Down Expand Up @@ -1223,8 +1223,8 @@ async def cleanup(self) -> None:
await self.create_task
if self.server:
self.server.close()
for transport in self.clients:
transport.close()
for client in self.clients:
client.close()
await self.server.wait_closed()


Expand Down

0 comments on commit 7546890

Please sign in to comment.