为何Spring Cloud Gateway会降低SSE并发连接数?
关于Spring Cloud Gateway限制SSE并发连接数的疑问
我们使用响应式Spring Boot应用为按key订阅的客户端生成Server Sent Events(SSE),核心接口代码如下:
@GetMapping(path = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<String> sse(@RequestParam String key) { return Flux.interval(Duration.ofSeconds(2)) .map((i) -> key + " Fixed delay task - " + System.currentTimeMillis() / 1000); }
两种访问场景的差异
直接访问服务
不通过Spring Cloud Gateway访问时,该服务最多可建立10K个并发Event Source连接,超过时抛出如下异常:
2022-09-07 12:59:02.123 WARN 90517 --- [ctor-http-nio-1] io.netty.channel.DefaultChannelPipeline : An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception. java.io.IOException: Too many open files at java.base/sun.nio.ch.Net.accept(Native Method) ~[na:na] at java.base/sun.nio.ch.ServerSocketChannelImpl.implAccept(ServerSocketChannelImpl.java:425) ~[na:na]
通过Spring Cloud Gateway访问
当以Spring Cloud Gateway作为前置网关时,并发连接上限降至2K,超过后服务端抛出:
2022-09-07 13:09:37.590 WARN 90707 --- [ctor-http-nio-9] reactor.netty.channel.FluxReceive : [559c58ed-1, L:/127.0.0.1:9999 - R:/127.0.0.1:54567] An exception has been observed post termination, use DEBUG level to see the full stack: java.net.SocketException: Connection reset
同时网关端抛出:
2022-09-07 12:59:55.696 ERROR 90264 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [7772ec3b-4438] 500 Server Error for HTTP GET "/sse?key=2682" java.net.SocketException: Connection reset at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) ~[na:na] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain] *__checkpoint ⇢ HTTP GET "/sse?key=2682" [ExceptionHandlingWebHandler] Original Stack Trace: at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) ~[na:na] at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426) ~[na:na] at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:258) ~[netty-buffer-4.1.79.Final.jar:4.1.79.Final]
疑问
为何Spring Cloud Gateway会对SSE服务的并发连接数产生如此大的影响?
内容的提问来源于stack exchange,提问作者Toni
相关产品推荐
相关产品推荐

