Skip to content

Bug: Streaming endpoints aren't flushing hte http writers. #1764

Closed

Description

@perbu

Hi.

I'm working on a streaming (text/event-stream) example and I noticed the generated code doesn't flush the output properly. I've tracked this down to the code in pkg/codegen/templates/strict/strict-interface.tmpl, which looks something like this:

                    if closer, ok := response.Body.(io.ReadCloser); ok {
                        defer closer.Close()
                    }
                    _, err := io.Copy(w, response.Body)
                    return err

io.Copy will buffer the response, which would be fine for most cases, but would add an arbitrary delay on text/event-stream, which is pretty bad as the delay can easy be 10+ seconds. text/event-streams are often time-sensitive.

What we wanna do in here is to check the content-type and if it is text/event-stream, we wanna flush after each write. similar code is in the httputil/reverseproxy part of the stdlib in case anyone wants to see other places where text/event-stream is a special case.

Since fixing this would touch the generated code I thought I'd at least bring the topic up before trying to fix this myself. Let me know how I should proceed.

Activity

  1. rucciva commented on Jan 16, 2026

    @rucciva

    Hey @perbu , out of curiosity since im planning to do something similar, why not wrap the response body with something that implement https://pkg.go.dev/io#WriterTo as pointed out in https://pkg.go.dev/io#Copy? that way you can control when to flush instead of relying on hardcoded buffer.

  2. perbu commented on Jan 16, 2026

    @perbu
    ContributorAuthor

    I like that you think that I remember the reasoning behind code I wrote 18 months ago. :-)

    What you suggest sounds reasonable.

  3. rucciva commented on Jan 16, 2026

    @rucciva

    Haha, fair enough.

    I was asking because your PR if merged wouldn't let me do that (im trying to implement SSE) since it will prevent the code to call io.Copy

  4. perbu commented on Jan 16, 2026

    @perbu
    ContributorAuthor

    I don't expect the PR to get merged. There is a quite a bit of complexity. This one also contains a lot of docs, which makes it seem quite big. Maybe a minimal one could get through, but the project is kinda overloaded atm.

  5. rucciva commented on Jan 16, 2026

    @rucciva

    Got it. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions