Fix a streaming bug + document streaming through example
This PR fixes two issues related to streaming of server-sent events (SSE). One is documenting streaming responses through an example. This showcases how an SSE endpoint could be specified and implemented in strict mode.
The second issue is a bug. Currently the generated code uses io.Copy() to copy to the response socket. This will allow the underlying OS to buffer the response for an arbitrary amount of time. A text/event-stream is typically time-sensitive so a flush of the socket should be forced after each write.
The PR will change the templated code and if the content-type is text/event-stream the io.Copy() will be replaced with a for-loop copying and flushing the data after each write.
Fixes: #1764
I might need a bit of help devising a test-strategy for this.
Sorry about all the noisy commits. It took me long time to figure out how to align the go.mod files properly.
thanks for this PR. Hopefully it gets merged. The update to the SSE endpoint logic fixed SSE for us!
Hi @jamietanna,
Can we get this reviewed/merged? It is blocking us from using it on our next AI project.
Thanks!!
Note that this PR is a lot bigger than it needed to be because @jamietanna also asked me to document this, which dramatically expanded the scope of the fix.
It's been a long time since I did this, but I think the actual bug fix is just the code in pkg/codegen/templates/strict/strict-interface.tmpl, not all the rest.
Something to consider, the behavior changes per target server. the Standard http server works properly with the current implementation, while gin requires the flush for it to stream.
Something to consider, the behavior changes per target server. the Standard http server works properly with the current implementation, while gin requires the flush for it to stream.
This doesn't seem to be the case. I've just ran into this again, with the stdlib server.