为请求类型配置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);
}
})));
}
}

相关内容

  • 没有找到相关文章

最新更新