#36293: Extend `django.utils.text.compress_sequence()` to optionally flush data
written to compressed file
-------------------------------+--------------------------------------
     Reporter:  huoyinghui     |                    Owner:  (none)
         Type:  New feature    |                   Status:  closed
    Component:  HTTP handling  |                  Version:  dev
     Severity:  Normal         |               Resolution:  needsinfo
     Keywords:  gzip flush     |             Triage Stage:  Unreviewed
    Has patch:  1              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Comment (by huoyinghui):

 I’ve already resolved this in my own project by subclassing GZipMiddleware
 to skip compression for responses with Content-Type: text/event-stream.
 However, I believe this is a common enough use case that it deserves
 better support at the framework level.

 SSE relies on real-time delivery, and buffering caused by gzip (especially
 under the 17KB threshold) can introduce noticeable latency on the client
 side. Developers may not immediately realize gzip is the cause, leading to
 unnecessary debugging time.

 It would be helpful if Django could:
         1.      Document this behavior and its impact on SSE;
         2.      Automatically skip compression for text/event-stream
 responses;
         3.      Or offer a more explicit way to opt-out of compression per
 response.

 Supporting this natively would improve the developer experience and better
 accommodate streaming use cases.

 Here’s my test case:

 def test_sse_middleware_skipped(self):
     from django.middleware.gzip import GZipMiddleware
     from django.http import StreamingHttpResponse

     class DummyRequest:
         META = {"HTTP_ACCEPT_ENCODING": "gzip"}

     def event_stream():
         for i in range(3):
             yield f"data: {i}\n\n".encode("utf-8")
             time.sleep(1)

     response = StreamingHttpResponse(event_stream(), content_type="text
 /event-stream")
     middleware = GZipMiddleware(lambda req: response)
     result = middleware(DummyRequest())

     first_chunk = next(result.streaming_content)
     self.assertFalse(first_chunk.startswith(b"\x1f\x8b"), "SSE response
 should not be gzipped")

 Test results:
         •       Case 1: flush_each=True
 ✅ The client receives each event promptly — no visible delay.
         •       Case 2: flush_each=False (default gzip behavior)
 ⚠️ All events are buffered and maybe delivered only after ~17KB, which
 defeats SSE’s purpose. The client appears stuck until buffer threshold is
 reached.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36293#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701960fb91175-ece21411-a9b1-4c74-9200-6ceced5653ba-000000%40eu-central-1.amazonses.com.

Reply via email to