为请求类型配置 ReadTimeoutHandler 或 WriteTimeoutHandler

问题描述

我有一个接收 HTTP 和 WS 请求的服务。

如何仅为 HTTP 请求配置 ReadTimeoutHandler?

解决方法

我已经配置了 HttpServer 的超时时间,如果请求是 WebSocket 将被删除:

  private static class TimeoutNettyCustomizer implements NettyServerCustomizer {
        private static final String READ_TIMEOUT_HANDLER = "READ_TIMEOUT_HANDLER";
        private static  final String WRITE_TIMEOUT_HANDLER = "WRITE_TIMEOUT_HANDLER";

        private final NettyServerProperties nettyServerProperties;

        public TimeoutNettyCustomizer(NettyServerProperties nettyServerProperties) {
            this.nettyServerProperties = nettyServerProperties;
        }

        @Override
        public HttpServer apply(HttpServer httpServer) {
            int writeTimeout = nettyServerProperties.getWriteTimeout();
            int readTimeout = nettyServerProperties.getReadTimeout();

            return httpServer.tcpConfiguration(tcp -> tcp.doOnConnection(connection -> connection
                    .addHandlerLast(READ_TIMEOUT_HANDLER,new WriteTimeoutHandler(writeTimeout,TimeUnit.SECONDS))
                    .addHandlerLast(WRITE_TIMEOUT_HANDLER,new ReadTimeoutHandler(readTimeout,TimeUnit.SECONDS))
                    .addHandlerLast(new ChannelDuplexHandler() {
                        @Override
                        public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception {
                            super.channelRead(ctx,msg);
                            if (msg instanceof HttpRequest) {
                                HttpRequest httpRequest = (HttpRequest) msg;
                                HttpHeaders headers = httpRequest.headers();

                                // delete timeout for Websocket and SSI
                                boolean notApplyTimeout = (HttpMethod.GET.equals(httpRequest.method())
                                        && headers.containsValue(HttpHeaderNames.CONNECTION,HttpHeaderValues.UPGRADE,true)
                                        && headers.containsValue(HttpHeaderNames.UPGRADE,HttpHeaderValues.WEBSOCKET,true));

                                if (notApplyTimeout) {
                                    connection.removeHandler(READ_TIMEOUT_HANDLER);
                                    connection.removeHandler(WRITE_TIMEOUT_HANDLER);
                                }

                            }
                        }

                        private void closeChannel(ChannelHandlerContext ctx) {
                            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.GATEWAY_TIMEOUT,Unpooled.EMPTY_BUFFER);

                            ctx.writeAndFlush(response)
                                    .addListener(ChannelFutureListener.CLOSE);
                        }
                    })));
        }
    }