Use writer-idle websocket ping (#3225)

This commit is contained in:
hengyunabc
2026-06-26 18:46:09 +08:00
committed by GitHub
parent 2100c9b115
commit 1bc67389ac
4 changed files with 25 additions and 3 deletions
@@ -42,7 +42,7 @@ public class LocalTtyServerInitializer extends ChannelInitializer<LocalChannel>
pipeline.addLast(new HttpObjectAggregator(ArthasConstants.MAX_HTTP_CONTENT_LENGTH));
pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler(ArthasConstants.DEFAULT_WEBSOCKET_PATH));
pipeline.addLast(new WebSocketServerProtocolHandler(ArthasConstants.DEFAULT_WEBSOCKET_PATH, null, false, ArthasConstants.MAX_HTTP_CONTENT_LENGTH, false, true));
pipeline.addLast(new IdleStateHandler(0, 0, ArthasConstants.WEBSOCKET_IDLE_SECONDS));
pipeline.addLast(new IdleStateHandler(0, ArthasConstants.WEBSOCKET_IDLE_SECONDS, 0));
pipeline.addLast(new TtyWebSocketFrameHandler(group, handler));
}
}
@@ -44,7 +44,7 @@ public class TtyServerInitializer extends ChannelInitializer<SocketChannel> {
pipeline.addLast(new BasicHttpAuthenticatorHandler(httpSessionManager));
pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler(ArthasConstants.DEFAULT_WEBSOCKET_PATH));
pipeline.addLast(new WebSocketServerProtocolHandler(ArthasConstants.DEFAULT_WEBSOCKET_PATH, null, false, ArthasConstants.MAX_HTTP_CONTENT_LENGTH, false, true));
pipeline.addLast(new IdleStateHandler(0, 0, ArthasConstants.WEBSOCKET_IDLE_SECONDS));
pipeline.addLast(new IdleStateHandler(0, ArthasConstants.WEBSOCKET_IDLE_SECONDS, 0));
pipeline.addLast(new TtyWebSocketFrameHandler(group, handler));
}
}
@@ -95,7 +95,7 @@ public class ProtocolDetectHandler extends ChannelInboundHandlerAdapter {
pipeline.addLast(new BasicHttpAuthenticatorHandler(httpSessionManager));
pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler(ArthasConstants.DEFAULT_WEBSOCKET_PATH));
pipeline.addLast(new WebSocketServerProtocolHandler(ArthasConstants.DEFAULT_WEBSOCKET_PATH, null, false, ArthasConstants.MAX_HTTP_CONTENT_LENGTH, false, true));
pipeline.addLast(new IdleStateHandler(0, 0, ArthasConstants.WEBSOCKET_IDLE_SECONDS));
pipeline.addLast(new IdleStateHandler(0, ArthasConstants.WEBSOCKET_IDLE_SECONDS, 0));
pipeline.addLast(new TtyWebSocketFrameHandler(channelGroup, ttyConnectionFactory));
ctx.fireChannelActive();
}
@@ -5,7 +5,9 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.junit.jupiter.api.Test;
@@ -46,6 +48,26 @@ class TtyWebSocketFrameHandlerQuietTest {
assertThat(channel.isOpen()).isFalse();
}
@Test
void writerIdleShouldSendWebSocketPingFrame() {
EmbeddedChannel channel = new EmbeddedChannel(new TtyWebSocketFrameHandler(
new DefaultChannelGroup(GlobalEventExecutor.INSTANCE),
connection -> {
}));
channel.pipeline().fireUserEventTriggered(IdleStateEvent.WRITER_IDLE_STATE_EVENT);
PingWebSocketFrame ping = channel.readOutbound();
try {
assertThat(ping).isNotNull();
} finally {
if (ping != null) {
ping.release();
}
channel.finishAndReleaseAll();
}
}
@Test
void extSessionsShouldCarryQuietFlag() {
Map<String, Object> extSessions = new ExtHttpTtyConnection(null, true).extSessions();