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

Add X-Accel-Buffering header to SSE responses #1012

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions mesop/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import secrets
import time
import urllib.parse as urlparse
from typing import Any, Generator, Sequence
from typing import Any, Generator, Iterable, Sequence
from urllib import request as urllib_request
from urllib.error import URLError

Expand Down Expand Up @@ -268,10 +268,7 @@ def ui_stream() -> Response:
ui_request = pb.UiRequest()
ui_request.ParseFromString(base64.urlsafe_b64decode(data))

response = Response(
stream_with_context(generate_data(ui_request)),
content_type="text/event-stream",
)
response = make_sse_response(stream_with_context(generate_data(ui_request)))
return response

@flask_app.before_request
Expand Down Expand Up @@ -408,7 +405,7 @@ def generate():
}
yield f"data: {json.dumps(sse_data)}\n\n"

return Response(generate(), content_type="text/event-stream")
return make_sse_response(generate())

@flask_app.route("/__hot-reload__")
def hot_reload() -> Response:
Expand Down Expand Up @@ -513,3 +510,16 @@ def sse_request(
if decoded_line.startswith(SSE_DATA_PREFIX):
event_data = json.loads(decoded_line[len(SSE_DATA_PREFIX) :])
yield event_data


def make_sse_response(
response: Iterable[bytes] | bytes | Iterable[str] | str | None = None,
):
return Response(
response,
content_type="text/event-stream",
# "X-Accel-Buffering" impacts SSE responses due to response buffering (i.e.
# individual events may get batched together instead of being sent right away).
# See https://nginx.org/en/docs/http/ngx_http_proxy_module.html
headers={"X-Accel-Buffering": "no"},
)
Loading