zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eribeiro <...@git.apache.org>
Subject [GitHub] zookeeper pull request #205: ZOOKEEPER-2736 Add a connection rate limiter
Date Fri, 24 Mar 2017 02:43:19 GMT
Github user eribeiro commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/205#discussion_r107824798
  
    --- Diff: src/java/main/org/apache/zookeeper/server/NIOServerCnxnFactory.java ---
    @@ -817,24 +849,23 @@ public void touchCnxn(NIOServerCnxn cnxn) {
     
         private void addCnxn(NIOServerCnxn cnxn) {
             InetAddress addr = cnxn.getSocketAddress();
    -        Set<NIOServerCnxn> set = ipMap.get(addr);
    -        if (set == null) {
    -            // in general we will see 1 connection from each
    -            // host, setting the initial cap to 2 allows us
    -            // to minimize mem usage in the common case
    -            // of 1 entry --  we need to set the initial cap
    -            // to 2 to avoid rehash when the first entry is added
    -            // Construct a ConcurrentHashSet using a ConcurrentHashMap
    -            set = Collections.newSetFromMap(
    -                new ConcurrentHashMap<NIOServerCnxn, Boolean>(2));
    -            // Put the new set in the map, but only if another thread
    +        IpCnxns ipCnxns = ipMap.get(addr);
    +
    +        if (ipCnxns == null) {
    +            // create an IpCnxns which is a wrapper that holds a RateLimiter and
    +            // a set of connections for each ip
    +            RateLimiter rateLimiter = RateLimiter.Factory.create(
    +                    QuorumPeerConfig.getRateLimiterImpl(), QuorumPeerConfig.getClientCnxnBurst(),
    +                    QuorumPeerConfig.getClientCnxnRate());
    +            ipCnxns = new IpCnxns(rateLimiter);
    +            // Put the ip limiter/set in the map, but only if another thread
                 // hasn't beaten us to it
    -            Set<NIOServerCnxn> existingSet = ipMap.putIfAbsent(addr, set);
    -            if (existingSet != null) {
    -                set = existingSet;
    +            IpCnxns existingIpCnxns = ipMap.putIfAbsent(addr, ipCnxns);
    +            if (existingIpCnxns != null) {
    +                ipCnxns = existingIpCnxns;
                 }
             }
    -        set.add(cnxn);
    +        ipCnxns.cnxnSet.add(cnxn);
     
    --- End diff --
    
    Nit: direct field access... again.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message