zookeeper如何控制请求的数量?

zookeeper通过参数maxClientCnxns数来控制客户端的连接数,,超过这个数量就会抛出IO异常:

throw new IOException("Too many connections from " + ia" - max is " + maxClientCnxns );

 

连接数maxClientCnxns默认是60:

 

关键代码在于nio服务工厂对于NIOServerCnxnFactory的接受连接的处理:

/**
     * Accept new socket connections. Enforces maximum number of connections
     * per client IP address. Round-robin assigns to selector thread for
     * handling. Returns whether pulled a connection off the accept queue
     * or not. If encounters an error attempts to fast close the socket.
     *
     * @return whether was able to accept a connection or not
     */
    private boolean doAccept() {
        boolean accepted = false;
        SocketChannel sc = null;
        try {
            sc = acceptSocket.accept();
            accepted = true;
            InetAddress ia = sc.socket().getInetAddress();
            int cnxncount = getClientCnxnCount(ia);

            //请求数量过多,抛出IO异常
            if (maxClientCnxns > 0 && cnxncount >= maxClientCnxns){
                throw new IOException("Too many connections from " + ia
                                      + " - max is " + maxClientCnxns );
            }

            LOG.debug("Accepted socket connection from "
                     + sc.socket().getRemoteSocketAddress());
            sc.configureBlocking(false);

            // Round-robin assign this connection to a selector thread
            if (!selectorIterator.hasNext()) {
                selectorIterator = selectorThreads.iterator();
            }
            SelectorThread selectorThread = selectorIterator.next();
            if (!selectorThread.addAcceptedConnection(sc)) {
                throw new IOException(
                    "Unable to add connection to selector queue"
                    + (stopped ? " (shutdown in progress)" : ""));
            }
            acceptErrorLogger.flush();
        } catch (IOException e) {
            // accept, maxClientCnxns, configureBlocking
            acceptErrorLogger.rateLimitLog(
                "Error accepting new connection: " + e.getMessage());
            fastCloseSock(sc);
        }
        return accepted;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值