perf[ws]: add IdleStateHandler to websocket server

This commit is contained in:
godotg committed 2024-01-19 14:39:55 +08:00
1 parent cb454ed404
commit d6ca2397e5
4 files changed
+16

No files matched your search

@@ -17,6 +17,7 @@ import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort; import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler; import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler; import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.IOUtils;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpClientCodec;
@@ -24,6 +25,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig; import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler; import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler; import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
/** /**
* @author godotg * @author godotg
@@ -39,6 +41,8 @@ public class JsonWebsocketClient extends AbstractClient<SocketChannel> {
@Override @Override
public void initChannel(SocketChannel channel) { public void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
channel.pipeline().addLast(new ClientIdleHandler());
channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB)); channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB)); channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig)); channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
@@ -17,12 +17,14 @@ import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort; import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler; import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler; import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.IOUtils;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler; import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
/** /**
* @author godotg * @author godotg
@@ -35,6 +37,8 @@ public class JsonWebsocketServer extends AbstractServer<SocketChannel> {
@Override @Override
public void initChannel(SocketChannel channel) { public void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
// 编解码 http 请求 // 编解码 http 请求
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB)); channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
// 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest // 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest
@@ -17,6 +17,7 @@ import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort; import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler; import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler; import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.IOUtils;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpClientCodec;
@@ -24,6 +25,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig; import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler; import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler; import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
/** /**
@@ -40,6 +42,8 @@ public class WebsocketClient extends AbstractClient<SocketChannel> {
@Override @Override
public void initChannel(SocketChannel channel) { public void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
channel.pipeline().addLast(new ClientIdleHandler());
channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB)); channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB)); channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig)); channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
@@ -17,12 +17,14 @@ import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort; import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler; import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler; import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.IOUtils;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler; import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
/** /**
* @author godotg * @author godotg
@@ -35,6 +37,8 @@ public class WebsocketServer extends AbstractServer<SocketChannel> {
@Override @Override
public void initChannel(SocketChannel channel) { public void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
// 编解码 http 请求 // 编解码 http 请求
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB)); channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
// 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest // 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest