315157973 commented on a change in pull request #10754:
URL: https://github.com/apache/pulsar/pull/10754#discussion_r664440914



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ConnectionController.java
##########
@@ -0,0 +1,133 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.common.tls.InetAddressUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public interface ConnectionController {
+
+    /**
+     * Increase the number of connections counter.
+     * @param remoteAddress
+     * @return
+     */
+    boolean increaseConnection(SocketAddress remoteAddress);
+
+    /**
+     * Decrease the number of connections counter.
+     * @param remoteAddress
+     */
+    void decreaseConnection(SocketAddress remoteAddress);
+
+
+    class DefaultConnectionController implements ConnectionController {
+        private static final Logger log = 
LoggerFactory.getLogger(DefaultConnectionController.class);
+        private static final Map<String, MutableInt> CONNECTIONS = new 
HashMap<>();
+        private static final ReentrantLock lock = new ReentrantLock();
+        private static int totalConnectionNum = 0;
+
+        private final int maxConnections;
+        private final int maxConnectionPerIp;
+        private final boolean maxConnectionsLimitEnabled;
+        private final boolean maxConnectionsLimitPerIpEnabled;
+
+        public DefaultConnectionController(ServiceConfiguration configuration) 
{
+            this.maxConnections = configuration.getBrokerMaxConnections();
+            this.maxConnectionPerIp = 
configuration.getBrokerMaxConnectionsPerIp();
+            this.maxConnectionsLimitEnabled = 
configuration.getBrokerMaxConnections() > 0;
+            this.maxConnectionsLimitPerIpEnabled = 
configuration.getBrokerMaxConnectionsPerIp() > 0;
+        }
+
+        @Override
+        public boolean increaseConnection(SocketAddress remoteAddress) {
+            if (!maxConnectionsLimitEnabled && 
!maxConnectionsLimitPerIpEnabled) {
+                return true;
+            }
+            if (!(remoteAddress instanceof InetSocketAddress)
+                    || !isLegalIpAddress(((InetSocketAddress) 
remoteAddress).getHostString())) {
+                return true;
+            }
+            lock.lock();
+            try {
+                String ip = ((InetSocketAddress) 
remoteAddress).getHostString();
+                if (maxConnectionsLimitPerIpEnabled) {
+                    CONNECTIONS.computeIfAbsent(ip, (x) -> new 
MutableInt(0)).increment();

Review comment:
       ```
           Map<String, AtomicInteger> map = new HashMap<>();
           map.computeIfAbsent("1", (x) -> new 
AtomicInteger(0)).getAndIncrement();
           map.computeIfAbsent("1", (x) -> new 
AtomicInteger(0)).getAndIncrement();
           map.computeIfAbsent("1", (x) -> new 
AtomicInteger(0)).getAndIncrement();
           map.computeIfAbsent("1", (x) -> new 
AtomicInteger(0)).getAndIncrement();
           map.computeIfAbsent("1", (x) -> new 
AtomicInteger(0)).getAndIncrement();
           System.out.println(map.get("1").get());
   ```
   the result is:
   
![image](https://user-images.githubusercontent.com/9758905/124586807-7d5c0300-de89-11eb-807d-f47fef50b8b1.png)
   

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
##########
@@ -243,11 +244,18 @@ public ServerCnx(PulsarService pulsar) {
         this.maxPendingBytesPerThread = 
conf.getMaxMessagePublishBufferSizeInMB() * 1024L * 1024L
                 / conf.getNumIOThreads();
         this.resumeThresholdPendingBytesPerThread = 
this.maxPendingBytesPerThread / 2;
+        this.connectionController = new 
ConnectionController.DefaultConnectionController(conf);
     }
 
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
         super.channelActive(ctx);
+        if (!connectionController.increaseConnection(remoteAddress)) {
+            ctx.channel().writeAndFlush(Commands.newError(-1, 
ServerError.NotAllowedError
+                    , "Reached the maximum number of connections"));

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to