aio-libs / aiohttp-sse

Server-sent events support for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expose internal _ping_task's state or provide a way to notify handler that client connection is closed

koromodako opened this issue · comments

Hello,

It could be interesting to find a nice way of exposing internal _ping_task's state to the handler as it is a great indicator that client closed the connection. I'll illustrate that point with some code:

async with sse_response(request, sep='\r\n') as resp:
    resp.ping_interval = 5 # set ping interval to 5 seconds instead of default 15 seconds
    while True:
        # internal ping task ended prematurely meaning that the client closed the connection, exit
        # using _ping_task is bad because _ping_task is private and subject to internal changes like renaming
        if resp._ping_task.done(): 
            break
        # server is shutting down, notify the client and exit
        if stop_event.is_set():
            await resp.send('reset', event='reset')
            break
        # ... perform some task that might not call resp.send for a long time meaning that
        # we cannot reliably detect that the client closed the connection catching ConnectionResetError 
        # at that point ...

Do you think that _ping_task's state could be exposed through an official API to allow checking it ? Or maybe have _ping_task's ConnectionResetError exception forwarded to the handler ?

Also, it would be very cool for this package to provide the client-side EventSource to be used with aiohttp's ClientSession.

Thank you !