gpt4 book ai didi

java - 如何在 HttpDecompressor 之前添加 GlobalTrafficShapingHandler

转载 作者:行者123 更新时间:2023-12-02 00:47:40 34 4
gpt4 key购买 nike

我想计算HttpDecompressor之前的压缩大小。

我尝试调用connection.addHandlerFirst,但不起作用。

HttpClient.create()
.mapConnect((connection, bootstrap) -> connection.map(
conn -> {
conn.addHandlerFirst(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpContent) {
System.out.println("received:" + msg);
}
super.channelRead(ctx, msg);
}
});
return conn;
}
))
.compress(true);

最佳答案

使用Connection#addHandlerFirst不会有帮助,因为处理程序将在 react 器编解码器之后添加。 More information here

您可以将此处理程序直接添加到 Netty 管道,如下所示:

HttpClient.create()
.mapConnect((connection, bootstrap) -> connection.map(
conn -> {
conn.channel().pipeline().addBefore(NettyPipeline.HttpDecompressor, "myhandler",new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpContent) {
System.out.println("received:" + msg);
}
super.channelRead(ctx, msg);
}
});
return conn;
}
))
.compress(true);

但是您应该记住,一旦将其直接添加到管道中,如果您使用连接池,则该处理程序也将保留用于下一个请求(Connection#addHandlerFirst 的情况并非如此)。因此,如果您仅需要针对特定​​请求,那么您应该在收到响应后将其删除。像这样的事情:

HttpClient.create()
.doOnResponse((res, conn) ->
conn.channel().pipeline().addBefore(NettyPipeline.HttpDecompressor, "myhandler",new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpContent) {
System.out.println("received:" + msg);
}
super.channelRead(ctx, msg);
}
}))
.doAfterResponse((res, conn) ->
conn.channel().pipeline().remove("myhandler"))
.compress(true)

关于java - 如何在 HttpDecompressor 之前添加 GlobalTrafficShapingHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57885043/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com