在通道管道中有条件地附加 HttpObjectAggregator

Conditionally attach HttpObjectAggregator in the Channel Pipeline

对于某些用例,例如:当它是 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)    
}

这个版本确实有效。

是的,您可以即时修改 ChannelPipeline