【问题标题】:Netty doesn't detect timeout after channel drop通道丢弃后 Netty 未检测到超时
【发布时间】:2014-08-12 15:14:11
【问题描述】:

奇怪的 Netty 行为:获得了一个服务器通道,该通道需要检测信号并使用 IdleStateHandler 捕获超时。像这样构造:

@Override
public void initChannel(SocketChannel ch) throws Exception { 
    final ByteBuf delimiter = Unpooled.buffer(1); 
    long serverTimeout = 1000; 

    // Break input by newlines: 
    delimiter.writeByte('\n'); 
    ch.pipeline().addLast(new DelimiterBasedFrameDecoder(32768, delimiter)); 

    // Use UTF8 string: 
    ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8)); 

    // Timeout
    ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(serverTimeout, 0, 0, TimeUnit.MILLISECONDS)); 

    // Handle commands: 
    ch.pipeline().addLast("myHandler", new DBConnectionHandler()); 
}

如果客户端发送心跳失败,一切正常——超时被触发和处理。但是,如果我粗暴地杀死客户端,相当于断开电缆,Netty 不会触发超时。

使用 Netty 的超时异常(而不是空闲事件)具有相同的结果。

【问题讨论】:

    标签: java timeout netty heartbeat


    【解决方案1】:

    事实证明,Netty 确实识别出通道丢弃,它只是不会触发死通道的超时。为相关通道上下文检测此调用channelHandlerContext.isRemoved()

    【讨论】:

    • 我认为在这里检测channelInactive(...)会更正确。
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多