New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid flushing for each SseEventBuilder entry #30912
Comments
Thanks for the insights @ratacolita Can you elaborate on how and from where this "lazy flush" flag is being controlled? Is it on the emitter itself? How are you deciding when to turn it off and on? |
Yes. We've created a method "isLazyFlush" on the ResponseBodyEmitter switch always returns false (because I didn't know the other use cases for this class) and overloaded the same method on the SseEmitter class. The SseEmitter itself got a new constructor with the option of setting its flag. So, once it set for the emitter its done. |
I see, so this setting is completely disabling flushing for the entire lifetime of the emitter, until the very end. If that's the case, I don't think we can add such a feature here since this goes against the core use case for SSE. Maybe in your case SSE is not the right call and I'm closing this issue for now, but we can reopen if it turns out there is more to this. Thanks! |
Regarding your suggestion to use @async, while it's a great feature for handling data production, it doesn't necessarily address the issue at hand. Even with asynchronous data production, we could still face network and CPU overhead if each small update is sent as a separate packet. I believe this feature request warrants further consideration. It could potentially lead to significant performance improvements in certain scenarios without affecting the core use case of SSE. You see. If we are using SseEmitter#send(SseEmitter.SseEventBuilder), we already have produced the data we are going to delivery inside the event builder. What the benefit of flushing the data in each item of the event build instead of flushing at once in the end of writing? |
Your comment is making a case against SSE itself. SSE is about sending data to clients as soon as it's available. If you'd like to buffer data upstream and only send when a few messages are available, it's really up to you. Delaying the send operation until the end completely breaks the use case. Again, using |
SseEventBuilder does exactly this, it buffers the the 'lines' of events. |
Starting from an actual use case with |
Affects: 5.x/6.x
I'm working with Spring MVC, especially with Server-Sent Events (SSE). Now, it is always flushing data every time SSE event is sent. This is good for fast updates, but it can have bad effects on performance if many updates are being sent. It can create much network traffic and use more server CPU.
So, I suggest an improvement. I want to add 'lazy flushing' option. This gives choice to users when they want the flush operation to happen. With lazy flushing, system can choose best time to flush data, which could use resources better and also group updates together.
In our use case we could handle twice the number of clients receiving updates using about 1/3 of the CPU.
This is a scatch of that we did.
The text was updated successfully, but these errors were encountered: