You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
ifcloser, ok:=response.Body.(io.ReadCloser); ok {
defercloser.Close()
}
_, err:=io.Copy(w, response.Body)
returnerr
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.
Reacted by Saath, Stefan McShane, Rafael and Simon Paredes
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.
Activity
rucciva commentedon Jan 16, 2026
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.
perbu commentedon Jan 16, 2026
I like that you think that I remember the reasoning behind code I wrote 18 months ago. :-)
What you suggest sounds reasonable.
rucciva commentedon Jan 16, 2026
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
perbu commentedon Jan 16, 2026
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.
rucciva commentedon Jan 16, 2026
Got it. Thanks