Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server sent events seem to be capped at 6 connections #304

Closed
oyv opened this issue Dec 23, 2023 · 1 comment
Closed

Server sent events seem to be capped at 6 connections #304

oyv opened this issue Dec 23, 2023 · 1 comment

Comments

@oyv
Copy link

oyv commented Dec 23, 2023

I'm not sure if this belongs here or in Hypercorn.

If I create 6 SSE connections to the server, it seems to stall, and no more can be created, and it seems no requests or any kind get through. They only get through when the SSEs time out.

(Maybe relatedly, I noticed that Hypercorn doesn't want to be killed via CTRL-C when there are active SSEs.)

To reproduce, put the following code into a file called sse_test.py and run it.

from quart import Quart, make_response
from asyncio import sleep

app = Quart(__name__)
@app.get("/")
async def index():
    return """
<script>
for (let i = 1000; i < 1010; i++){
  let sse = new EventSource("/stream/" + i);
  sse.addEventListener('sse_test', (e) => {console.log(e.data)});
}
</script>
"""

async def send_events(channel):
    i = 0
    while True:
        ret = f"{channel}:{i}"
        yield f'data: {ret}\nevent: sse_test\r\n\r\n'.encode()
        print(ret)
        i += 1
        await sleep(1)

@app.get("/stream/<string:channel>")
async def stream(channel):
    response = await make_response(
        send_events(channel),
        {
            'Content-Type': 'text/event-stream',
            'Cache-Control': 'no-cache',
            'Transfer-Encoding': 'chunked',
        },
    )
    response.timeout = 5
    return response

Run it via hypercorn sse_test.py:app.

The js tries to start 10 SSE connections (1000-1009), but only 6 are ever printed out by hypercorn and to the console.

1000:0
1001:0
1002:0
1003:0
1004:0
1005:0
1000:1
1001:1
1002:1
1003:1
1004:1
1005:1
1000:2
1001:2
1002:2
1003:2
1004:2
1005:2
1000:3
1001:3
1002:3
1003:3
1004:3
1005:3
1000:4
1001:4
1002:4
1003:4
1004:4
1005:4
1006:0
1007:0
1008:0
1009:0
1006:1
1007:1
1008:1
1009:1
1006:2
1007:2
1008:2
1009:2
1006:3
1007:3
1008:3
1009:3
1006:4
1007:4
1008:4
1009:4
1000:0
1001:0
1002:0
1003:0
1004:0
1005:0
1000:1
1001:1
1002:1
1003:1
1004:1
1005:1
1000:2
1001:2
1002:2
1003:2
1004:2
1005:2
1000:3
1001:3
1002:3
1003:3
1004:3
1005:3

I'm not sure if the cap is intended, but as far as I can tell the limitation is not documented, and not configurable. Interestingly, it also doesn't change by adding more workers, it's still 6.

Environment:

  • Python version: Python 3.12.1
  • Quart version: 0.19.4
  • Hypercorn version: 0.15.0
@oyv
Copy link
Author

oyv commented Dec 24, 2023

Never mind, I just found out that it's a browser limitation: https://stackoverflow.com/questions/5195452/websockets-vs-server-sent-events-eventsource?rq=2

@oyv oyv closed this as completed Dec 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant