oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Fix a streaming bug + document streaming through example

Open perbu opened this issue 1 year ago • 7 comments

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

perbu avatar Sep 19 '24 06:09 perbu

I might need a bit of help devising a test-strategy for this.

perbu avatar Sep 19 '24 07:09 perbu

Sorry about all the noisy commits. It took me long time to figure out how to align the go.mod files properly.

perbu avatar Sep 23 '24 10:09 perbu

thanks for this PR. Hopefully it gets merged. The update to the SSE endpoint logic fixed SSE for us!

rgarcia avatar May 21 '25 16:05 rgarcia

Hi @jamietanna,

Can we get this reviewed/merged? It is blocking us from using it on our next AI project.

Thanks!!

injeniero avatar Sep 06 '25 01:09 injeniero

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.

perbu avatar Sep 06 '25 07:09 perbu

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.

injeniero avatar Sep 12 '25 12:09 injeniero

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.

perbu avatar Oct 25 '25 09:10 perbu