Skip to content

Commit 71b7f37

Browse files
fix(websocket): keep HTTP timeout handlers off proxied WebSocket connections (#8377)
The websocket starter reuses the shared HttpClient bean, and HttpClientFactory installs 3-second idle/read/write timeout handlers on every connection for HTTP request/response semantics. A proxied WebSocket is long-lived, so silence for 3 seconds in either direction closed the session. Derive the WebSocket client's HttpClient from the shared one and remove those three handlers when a connection is established, keeping the shared SSL, proxy and connection-pool configuration. Fixes #8374
1 parent aacca7e commit 71b7f37

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
package org.apache.shenyu.springboot.plugin.websocket;
1919

20+
import io.netty.channel.ChannelHandler;
21+
import io.netty.channel.ChannelPipeline;
22+
import io.netty.handler.timeout.IdleStateHandler;
23+
import io.netty.handler.timeout.ReadTimeoutHandler;
24+
import io.netty.handler.timeout.WriteTimeoutHandler;
2025
import org.apache.shenyu.common.config.ShenyuConfig;
2126
import org.apache.shenyu.common.constant.Constants;
2227
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
@@ -37,6 +42,7 @@
3742
import reactor.netty.http.client.HttpClient;
3843
import reactor.netty.http.client.WebsocketClientSpec;
3944

45+
import java.util.Objects;
4046
import java.util.function.Supplier;
4147

4248
/**
@@ -81,7 +87,38 @@ public ReactorNettyWebSocketClient reactorNettyWebSocketClient(final ShenyuConfi
8187
Supplier<WebsocketClientSpec.Builder> builder = () -> WebsocketClientSpec.builder()
8288
.maxFramePayloadLength(shenyuConfig.getWebsocket().getMaxFramePayloadSize() * Constants.BYTES_PER_MB)
8389
.handlePing(shenyuConfig.getWebsocket().getEnableProxyPing());
84-
return new ReactorNettyWebSocketClient(httpClient.getIfAvailable(HttpClient::create), builder);
90+
return new ReactorNettyWebSocketClient(
91+
webSocketHttpClient(httpClient.getIfAvailable(HttpClient::create)), builder);
92+
}
93+
94+
/**
95+
* Remove the HTTP request/response timeout handlers from a shared client used for WebSocket connections.
96+
*
97+
* <p>The shared {@code HttpClient} installs idle/read/write timeouts that are meant for short
98+
* request/response exchanges. A proxied WebSocket is long-lived, so those handlers must not tear it
99+
* down while keeping the shared client's SSL, proxy and connection-pool configuration.</p>
100+
*
101+
* @param httpClient the shared http client
102+
* @return the http client to use for WebSocket connections
103+
*/
104+
static HttpClient webSocketHttpClient(final HttpClient httpClient) {
105+
return httpClient.doOnConnected(connection -> {
106+
removeHandler(connection.channel().pipeline(), IdleStateHandler.class);
107+
removeHandler(connection.channel().pipeline(), ReadTimeoutHandler.class);
108+
removeHandler(connection.channel().pipeline(), WriteTimeoutHandler.class);
109+
});
110+
}
111+
112+
/**
113+
* Remove a handler from the pipeline when it is present.
114+
*
115+
* @param pipeline the channel pipeline
116+
* @param handlerType the handler type
117+
*/
118+
static void removeHandler(final ChannelPipeline pipeline, final Class<? extends ChannelHandler> handlerType) {
119+
if (Objects.nonNull(pipeline.get(handlerType))) {
120+
pipeline.remove(handlerType);
121+
}
85122
}
86123

87124
/**

‎shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/test/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfigurationTest.java‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
package org.apache.shenyu.springboot.plugin.websocket;
1919

20+
import io.netty.channel.ChannelInboundHandlerAdapter;
21+
import io.netty.channel.ChannelPipeline;
22+
import io.netty.channel.embedded.EmbeddedChannel;
23+
import io.netty.handler.timeout.IdleStateHandler;
24+
import io.netty.handler.timeout.ReadTimeoutHandler;
25+
import io.netty.handler.timeout.WriteTimeoutHandler;
2026
import org.apache.shenyu.common.config.ShenyuConfig;
2127
import org.apache.shenyu.common.enums.PluginEnum;
2228
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
@@ -31,12 +37,20 @@
3137
import org.springframework.test.util.ReflectionTestUtils;
3238
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
3339
import org.springframework.web.reactive.socket.server.WebSocketService;
40+
import reactor.core.publisher.Mono;
41+
import reactor.netty.DisposableServer;
42+
import reactor.netty.http.client.HttpClient;
3443
import reactor.netty.http.client.WebsocketClientSpec;
44+
import reactor.netty.http.server.HttpServer;
3545

46+
import java.net.URI;
47+
import java.time.Duration;
48+
import java.util.concurrent.TimeUnit;
3649
import java.util.function.Supplier;
3750

3851
import static org.assertj.core.api.Assertions.assertThat;
3952
import static org.junit.jupiter.api.Assertions.assertNotNull;
53+
import static org.junit.jupiter.api.Assertions.assertTrue;
4054

4155
/**
4256
* Test case for {@link WebSocketPluginConfiguration}.
@@ -89,6 +103,56 @@ public void testReactorNettyWebSocketClient() {
89103
);
90104
}
91105

106+
@Test
107+
public void testRemoveHttpTimeoutHandlers() {
108+
EmbeddedChannel channel = new EmbeddedChannel();
109+
ChannelPipeline pipeline = channel.pipeline();
110+
pipeline.addLast(new IdleStateHandler(3, 3, 3, TimeUnit.SECONDS));
111+
pipeline.addLast(new ReadTimeoutHandler(3, TimeUnit.SECONDS));
112+
pipeline.addLast(new WriteTimeoutHandler(3, TimeUnit.SECONDS));
113+
pipeline.addLast("keep", new ChannelInboundHandlerAdapter());
114+
115+
WebSocketPluginConfiguration.removeHandler(pipeline, IdleStateHandler.class);
116+
WebSocketPluginConfiguration.removeHandler(pipeline, ReadTimeoutHandler.class);
117+
WebSocketPluginConfiguration.removeHandler(pipeline, WriteTimeoutHandler.class);
118+
119+
assertThat(pipeline.get(IdleStateHandler.class)).isNull();
120+
assertThat(pipeline.get(ReadTimeoutHandler.class)).isNull();
121+
assertThat(pipeline.get(WriteTimeoutHandler.class)).isNull();
122+
assertThat(pipeline.get("keep")).isNotNull();
123+
channel.finishAndReleaseAll();
124+
}
125+
126+
@Test
127+
public void testWebSocketSurvivesHttpTimeouts() {
128+
DisposableServer server = HttpServer.create()
129+
.port(0)
130+
.handle((request, response) -> response.sendWebsocket((in, out) -> out.send(in.receive().retain())))
131+
.bindNow();
132+
try {
133+
// Mirrors the shared client built by HttpClientFactory: the three connection-level
134+
// timeout handlers are added through doOnConnected and responseTimeout is 3s by default.
135+
HttpClient httpClient = HttpClient.create()
136+
.responseTimeout(Duration.ofSeconds(3))
137+
.doOnConnected(connection -> {
138+
connection.addHandlerLast(new IdleStateHandler(3, 3, 3, TimeUnit.SECONDS));
139+
connection.addHandlerLast(new WriteTimeoutHandler(3, TimeUnit.SECONDS));
140+
connection.addHandlerLast(new ReadTimeoutHandler(3, TimeUnit.SECONDS));
141+
});
142+
ReactorNettyWebSocketClient client =
143+
new ReactorNettyWebSocketClient(WebSocketPluginConfiguration.webSocketHttpClient(httpClient));
144+
client.execute(URI.create("ws://localhost:" + server.port() + "/"), session ->
145+
session.send(Mono.just(session.textMessage("first")))
146+
.then(Mono.delay(Duration.ofSeconds(4)))
147+
.then(Mono.fromRunnable(() -> assertTrue(session.isOpen())))
148+
.then(session.send(Mono.just(session.textMessage("second"))))
149+
.then())
150+
.block(Duration.ofSeconds(15));
151+
} finally {
152+
server.disposeNow();
153+
}
154+
}
155+
92156
@Test
93157
public void testHandshakeWebSocketService() {
94158
applicationContextRunner.run(context -> {

0 commit comments

Comments
 (0)