NettyRPC icon indicating copy to clipboard operation
NettyRPC copied to clipboard

异步消息存放的map pendingRPC异常情况无法回收

Open dempeZheng opened this issue 9 years ago • 1 comments

pendingRPC 这个ConcurrentHashMap可能会有内存溢出的风险 比如client 请求异常,或者Server返回Response异常,这个pendingRPC内的对象都无法回收

public class DefaultClientHandler extends SimpleChannelInboundHandler<RPCContext>{

    private ConcurrentHashMap<Long, RPCFuture> pendingRPC = new ConcurrentHashMap<Long, RPCFuture>();


    @Override
    protected void channelRead0(ChannelHandlerContext arg0, RPCContext rpcCtx)
            throws Exception {

        RPCFuture rpcFuture = pendingRPC.get(rpcCtx.getResponse().getSeqNum());

        if(rpcFuture != null){
            pendingRPC.remove(rpcCtx.getResponse().getSeqNum());
            rpcFuture.done(rpcCtx.getResponse());
        }
    }

    public RPCFuture doRPC(RPCContext rpcCtx){
        RPCFuture rpcFuture = new RPCFuture(rpcCtx, this);
        pendingRPC.put(rpcCtx.getRequest().getSeqNum(), rpcFuture);
        channel.writeAndFlush(rpcCtx);
        return rpcFuture;
    }

}

dempeZheng avatar Sep 19 '16 02:09 dempeZheng

嗯,极端情况下会出现这个问题。超时和限流的机制还空缺。

dawnbreaks avatar Sep 19 '16 03:09 dawnbreaks