Hello, I’m maintaining a library for building websocket servers (mainly). Before shutting down, the server should perform a closing handshake on open connections. I assume many other protocols with long-lived connections have similar requirements.
The most natural solution would to inherit asyncio.Server and to extend close() and wait_closed() appropriately. Unfortunately there’s no way to tell create_server() to use a custom Server class. Adding a kwarg: create_server(…, server_class=Server) looks like a straightforward solution but it wouldn’t solve the problem completely. Indeed it’s the protocol instance tied to each connection that knows what should be done before closing its connection. But the protocol instances and the server instance don't know about each other. As you can see on https://github.com/aaugustin/websockets/commit/8e5a3954d62d01e2201aedaf4fe366d67e6af2c3#diff-a548b853d665035e6e62944845d0a575L220 <https://github.com/aaugustin/websockets/commit/8e5a3954d62d01e2201aedaf4fe366d67e6af2c3#diff-a548b853d665035e6e62944845d0a575L220> I worked around the issue by: 1. instantiating my custom Server class — but it isn’t functional until step 4 2. using it in the protocol factory so that the protocol instances can register themselves with the server 3. calling create_server() 4. inserting the resulting asyncio.Server into my custom Server instance — thus completing its initialization I’m not happy with the “delayed initialization” pattern. I wish there was a better way. Has anyone else encountered a similar issue? Have you found a better solution? Could asyncio’s APIs be enhanced in this area? Thanks! -- Aymeric.
