NETTY 4 DefaultEventExecutorGroup似乎阻止了IO?

时间:2015-08-04 20:04:25

标签: java asynchronous netty nio

我注意到当我在我的一个处理程序(应该由DefaultEventExecutorGroup执行)上添加一点延迟时,它会导致用户在结束时遇到超时?没有延迟,一切似乎都运行正常,但1ms的延迟不应该导致1/3的用户超时。我当前的负载大约是每秒20k个请求,不一定是所有并发的。

这是我的管道设置

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    p.addLast("frameDecoder", new LineBasedFrameDecoder(80));
    p.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
    p.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
    p.addLast(CoolServer.getExecutors(), "cool_request", new CoolServerHandler()); 

    //private static CoolServer.executors = new DefaultEventExecutorGroup(48);
    //CoolServer.getExecutors() returns CoolServer.executors 
}

以下是我的CoolServerHandler的设置方法:(“extends SimpleChannelInboundHandler /”)

public class CoolServerHandler extends SimpleChannelInboundHandler<Object>{
//default constructor used
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

    //LOOP TO ADD SOME LATENCY TO MY HANDLER 
    long currentTime = System.currentTimeMillis();
    long timeIncrement = 1; // waits for this many milliseconds
    for (long checkTime = System.currentTimeMillis(); checkTime  < (currentTime + timeIncrement); checkTime = System.currentTimeMillis()){
        //busy waiting
    } 
    //encodeResponse taken out for simplicity, simple string manipulation function
    String response = encodeResponse(key, true);

    ReferenceCountUtil.retain(msg);
    ctx.writeAndFlush(response);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
}



@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {           
    cause.printStackTrace();
    ctx.close();
}
}

我试图保持代码简单,以防止此帖太长,如果需要更多信息,请告诉我。谢谢

0 个答案:

没有答案