Netty没有正式关闭渠道

时间:2012-01-14 23:23:13

标签: java netty high-load

我是Netty的新手。现在正在迁移我的服务器,导致高负载。我读了很多男人,像示例一样做了所有事情,但有一个错误:CPU负载增长的时间非常快。

分析堆和日志向我展示了那些引发异常(可能不仅仅是它们)未正常关闭的通道,并保留在工作者的选择器中。我有150多个已打开的频道,有50个在线用户。

我的代码如下。

创建服务器:

networkServer = new ServerBootstrap(new NioServerSocketChannelFactory(bossExec, ioExec, 4));
networkServer.setOption("backlog", 500);
networkServer.setOption("connectTimeoutMillis", 10000);
networkServer.setPipelineFactory(new ServerPipelineFactory());
Channel channel = networkServer.bind(new InetSocketAddress(address, port));

管道工厂:

@Override
public ChannelPipeline getPipeline() throws Exception {
    PacketFrameDecoder decoder = new PacketFrameDecoder();
    PacketFrameEncoder encoder = new PacketFrameEncoder();
    return Channels.pipeline(decoder, encoder, new PlayerHandler(decoder, encoder));
}

解码器:

public class PacketFrameDecoder extends FrameDecoder {
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
}

@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
}

@Override
protected Object decode(ChannelHandlerContext arg0, Channel arg1, ChannelBuffer buffer) throws Exception {
    try {
            buffer.markReaderIndex();
        Packet p = // ... doing decoding things
        if(p != null)
            return p;
                    // Reset buffer if not success
        buffer.resetReaderIndex();
            // Reset buffer if not success and got exception of not enough bytes in buffer
    } catch(BufferUnderflowException e) {
        buffer.resetReaderIndex();
    } catch(ArrayIndexOutOfBoundsException aioobe) {
        buffer.resetReaderIndex();
    }
    return null;
}
}

和处理程序:

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    worker = new PlayerWorkerThread(this, e.getChannel());
}

@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    worker.disconnectedFromChannel();
}

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    if(e.getChannel().isOpen())
        worker.acceptPacket((Packet) e.getMessage());
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
    Server.logger.log(Level.WARNING, "Exception from downstream", e.getCause());
    ctx.getChannel().close();
}

一切都运行良好,除了增加cpu负载......

0 个答案:

没有答案