【问题标题】:Conditionally attach HttpObjectAggregator in the Channel Pipeline有条件地在通道管道中附加 HttpObjectAggregator
【发布时间】:2021-06-05 09:48:27
【问题描述】:

对于某些用例,例如:当它是 GET 请求或 WebSocket 升级请求时,HttpObjectAggregator 原来是性能开销,很容易被忽略。

我可以通过检测它是不是GET请求还是websocket请求来有条件地添加HttpObjectAggregator吗?

更新 1

// on channelRead()

if (req.isInstanceOf[JFullHttpRequest]) {
    val aggregator = new JHttpObjectAggregator(Int.MaxValue)
    val pipeline   = ctx.channel().pipeline

    pipeline.addLast(OBJECT_AGGREGATOR, aggregator)

    // re-attaching the handler
    pipeline.remove(HTTP_REQUEST_HANDLER)
    pipeline.addLast(HTTP_REQUEST_HANDLER, this)

    // firing channel read again
    aggregator.channelRead(ctx, jHttpRequest)
}

我能够通过有条件地添加聚合器来使其工作。为什么我需要重新附加处理程序?

更新 2

// on channelRead()

if (req.isInstanceOf[JFullHttpRequest]) {
    val aggregator = new JHttpObjectAggregator(Int.MaxValue)
    val pipeline   = ctx.channel().pipeline

    pipeline.addBefore(HTTP_REQUEST_HANDLER, aggregator)    
}

这个版本确实有效。

【问题讨论】:

    标签: performance netty


    【解决方案1】:

    是的,您可以即时修改ChannelPipeline

    【讨论】:

    • 如果之后添加聚合器,我们不会错过最初的几条消息吗?
    • 我能够通过做两件事来使其工作 - 1. 在将聚合器添加到管道后对其执行一次 channelRead() ,然后 2. 再次删除并添加可共享请求处理程序。
    • 我不知道为什么我必须删除并重新添加处理程序。
    猜你喜欢
    • 2021-01-13
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多