spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.44k stars 3.27k forks source link

Server Side event Responses from an server behind Spring-clod-gateway-mvc #3410

Open AkashB23 opened 1 month ago

AkashB23 commented 1 month ago

I have a Spring boot mvc server that has SseEmitter as Response, and the server is behind Spring-clod-gateway-mvc gateway which has the Route for the path with handler function being  ProxyExchangeHandlerFunction

The server sent events are consumed properly when server is being called directly without gateway, but once we have gateway to be as reverse proxy, SSE does not work with MVC version of spring-cloud-gateway

we see EOF reached while reading exception

rworsnop commented 1 month ago

This is because of the way Tomcat's OutputBuffer works. That's what is being written to when applications write to a response's OutputStream. As its name suggests, it will buffer data before actually sending anything to the client. A good idea for downloading a file, but does not work with SSE. The solution is to call OutputStream.flush after every single write. That's not what SCG does, as it's using InputStream.transferTo via StreamUtils.copy. Ideally SCG would offer a way of specifying whether or not a route should flush after every write. Maybe a variant of HandlerFunctions.http. Failing that, you'll probably have to provide your own handler to use instead of HandlerFunctions.http.