315157973 commented on a change in pull request #10754: URL: https://github.com/apache/pulsar/pull/10754#discussion_r647078727
########## File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ConnectionController.java ########## @@ -0,0 +1,127 @@ +/** + * 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 java.util.regex.Pattern; +import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.pulsar.broker.ServiceConfiguration; +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 Pattern IPV4_PATTERN = Pattern + .compile("^" + "(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)" + "(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}" + "$"); + private static final ReentrantLock lock = new ReentrantLock(); + private static int totalConnectionNum = 0; + + private final int maxConnections; + private final int maxConnectionPerIp; + + public DefaultConnectionController(ServiceConfiguration configuration) { + this.maxConnections = configuration.getBrokerMaxConnections(); + this.maxConnectionPerIp = configuration.getBrokerMaxConnectionsPerIp(); + } + + @Override + public boolean increaseConnection(SocketAddress remoteAddress) { + if (maxConnections <= 0 && maxConnectionPerIp <= 0) { + return true; + } + if (!(remoteAddress instanceof InetSocketAddress) || !IPV4_PATTERN.matcher( Review comment: There are many scenarios to consider for IPV6, and I will have another PR to support -- 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. For queries about this service, please contact Infrastructure at: [email protected]
