You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2006/11/09 12:24:05 UTC
svn commit: r472864 - in /james/server/trunk/src/java/org/apache/james:
core/ services/ util/connection/
Author: norman
Date: Thu Nov 9 03:23:59 2006
New Revision: 472864
URL: http://svn.apache.org/viewvc?view=rev&rev=472864
Log:
Add ConnectionLimit per IP. See JAMES-670
Some refactoring
Added:
james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java (with props)
Modified:
james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
Modified: james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java?view=diff&rev=472864&r1=472863&r2=472864
==============================================================================
--- james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java (original)
+++ james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java Thu Nov 9 03:23:59 2006
@@ -181,6 +181,9 @@
* The DNSServer
*/
private DNSServer dnsServer = null;
+
+ private boolean connPerIPConfigured = false;
+ private int connPerIP = 0;
public void setConnectionManager(JamesConnectionManager connectionManager) {
this.connectionManager = connectionManager;
@@ -310,6 +313,29 @@
.append(connectionLimit.intValue())
.append(" connections.");
getLogger().info(infoBuffer.toString());
+
+ String connectionLimitPerIP = conf.getChild("connectionLimitPerIP").getValue(null);
+ if (connectionLimitPerIP != null) {
+ try {
+ connPerIP = new Integer(connectionLimitPerIP).intValue();
+ connPerIPConfigured = true;
+ } catch (NumberFormatException nfe) {
+ getLogger().error("Connection limit per IP value is not properly formatted.", nfe);
+ }
+ if (connPerIP < 0) {
+ getLogger().error("Connection limit per IP value cannot be less than zero.");
+ throw new ConfigurationException("Connection limit value cannot be less than zero.");
+ }
+ } else {
+ connPerIP = connectionManager.getMaximumNumberOfOpenConnectionsPerIP();
+ }
+ infoBuffer = new StringBuffer(128)
+ .append(getServiceType())
+ .append(" will allow a maximum of ")
+ .append(connPerIP)
+ .append(" per IP connections.");
+ getLogger().info(infoBuffer.toString());
+
}
private void configureHelloName(Configuration handlerConfiguration) {
@@ -401,15 +427,31 @@
if ((connectionLimit != null)) {
if (null != threadPool) {
+ if (connPerIPConfigured) {
+ connectionManager.connect(connectionName, serverSocket, this, threadPool, connectionLimit.intValue(),connPerIP);
+ } else {
connectionManager.connect(connectionName, serverSocket, this, threadPool, connectionLimit.intValue());
+ }
} else {
- connectionManager.connect(connectionName, serverSocket, this, connectionLimit.intValue()); // default pool
+ if (connPerIPConfigured) {
+ connectionManager.connect(connectionName, serverSocket, this, connectionLimit.intValue(),connPerIP); // default pool
+ } else {
+ connectionManager.connect(connectionName, serverSocket, this, connectionLimit.intValue());
+ }
}
} else {
if (null != threadPool) {
- connectionManager.connect(connectionName, serverSocket, this, threadPool);
+ if (connPerIPConfigured) {
+ connectionManager.connect(connectionName, serverSocket, this, threadPool);
} else {
- connectionManager.connect(connectionName, serverSocket, this); // default pool
+ connectionManager.connect(connectionName, serverSocket, this, threadPool, 0, connPerIP);
+ }
+ } else {
+ if (connPerIPConfigured) {
+ connectionManager.connect(connectionName, serverSocket, this); // default pool
+ } else {
+ connectionManager.connect(connectionName, serverSocket, this, 0, connPerIP);
+ }
}
}
}
Modified: james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java?view=diff&rev=472864&r1=472863&r2=472864
==============================================================================
--- james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java (original)
+++ james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java Thu Nov 9 03:23:59 2006
@@ -114,5 +114,51 @@
ConnectionHandlerFactory handlerFactory )
throws Exception;
+ /**
+ * Start managing a connection.
+ * Management involves accepting connections and farming them out to threads
+ * from pool to be handled.
+ *
+ * @param name the name of connection
+ * @param socket the ServerSocket from which to
+ * @param handlerFactory the factory from which to acquire handlers
+ * @param threadPool the thread pool to use
+ * @param maxOpenConnections the maximum number of open connections allowed for this server socket.
+ * @param maxOpenConnectionsPerIP the maximum number of open connections per IP allowed for this server socket.
+ * @throws Exception
+ */
+ void connect( String name,
+ ServerSocket socket,
+ ConnectionHandlerFactory handlerFactory,
+ ThreadPool threadPool,
+ int maxOpenConnections,
+ int maxOpenConnectionsPerIP)
+ throws Exception;
+ /**
+ * Start managing a connection.
+ * Management involves accepting connections and farming them out to threads
+ * from pool to be handled.
+ *
+ * @param name the name of connection
+ * @param socket the ServerSocket from which to
+ * @param handlerFactory the factory from which to acquire handlers
+ * @param maxOpenConnections the maximum number of open connections allowed for this server socket.
+ * @param maxOpenConnectionsPerIP the maximum number of open connections per IP allowed for this server socket.
+ * @throws Exception
+ */
+ void connect( String name,
+ ServerSocket socket,
+ ConnectionHandlerFactory handlerFactory,
+ int maxOpenConnections,
+ int maxOpenConnectionsPerIP)
+ throws Exception;
+
+ /**
+ * Returns the default maximum number of open connections per IP supported by this
+ * SimpleConnectionManager
+ *
+ * @return the maximum number of connections
+ */
+ int getMaximumNumberOfOpenConnectionsPerIP();
}
Added: james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java?view=auto&rev=472864
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java (added)
+++ james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java Thu Nov 9 03:23:59 2006
@@ -0,0 +1,91 @@
+/****************************************************************
+ * 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.james.util.connection;
+
+import java.util.HashMap;
+
+/**
+ * Map which holds information about the connection count per ip
+ */
+public class ConnectionPerIpMap {
+
+ /**
+ * A HashMap of clientip and connections
+ */
+ private final HashMap connectionPerIP = new HashMap();
+
+ /**
+ * Raise the connectionCount for the given ipAdrress
+ *
+ * @param ip raise the connectionCount for the given ipAddress
+ */
+ public synchronized void addConnectionPerIP (String ip) {
+ connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
+ }
+
+ /**
+ * Get the count of connections for the given ip
+ *
+ * @param ip the ipAddress to get the connections for.
+ * @return count
+ */
+ public synchronized int getConnectionPerIP(String ip) {
+ int count = 0;
+ String curCount = null;
+ Object c = connectionPerIP.get(ip);
+
+ if (c != null) {
+ curCount = c.toString();
+
+ if (curCount != null) {
+ return Integer.parseInt(curCount);
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Set the connection count for the given ipAddress
+ *
+ * @param ip ipAddres for which we want to set the count
+ */
+ public synchronized void removeConnectionPerIP (String ip) {
+
+ int count = getConnectionPerIP(ip);
+ if (count > 1) {
+ connectionPerIP.put(ip,Integer.toString(count -1));
+ } else {
+ // not need this entry any more
+ connectionPerIP.remove(ip);
+ }
+
+ }
+
+ /**
+ * Clear the connection count map
+ */
+ public synchronized void clearConnectionPerIP () {
+ connectionPerIP.clear();
+ }
+
+}
Propchange: james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java?view=diff&rev=472864&r1=472863&r2=472864
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java (original)
+++ james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java Thu Nov 9 03:23:59 2006
@@ -27,7 +27,6 @@
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
@@ -107,17 +106,15 @@
* connection will allow.
*/
private int maxOpenConnPerIP;
+
+ private ConnectionPerIpMap connPerIpMap;
/**
* A collection of client connection runners.
*/
private final ArrayList clientConnectionRunners = new ArrayList();
- /**
- * A HashMap of clientip and connections
- */
- private final HashMap connectionPerIP = new HashMap();
-
+
/**
* The thread used to manage this server connection.
*/
@@ -154,6 +151,7 @@
*/
public void initialize() throws Exception {
runnerPool = new HardResourceLimitingPool(theRunnerFactory, 5, maxOpenConn);
+ connPerIpMap = new ConnectionPerIpMap();
ContainerUtil.enableLogging(runnerPool,getLogger());
ContainerUtil.initialize(runnerPool);
}
@@ -212,7 +210,7 @@
runner = null;
}
clientConnectionRunners.clear();
- clearConnectionPerIP();
+ connPerIpMap.clearConnectionPerIP();
}
@@ -267,57 +265,7 @@
synchronized (this) { notify(); } // match the wait(...) in the run() inner loop before accept().
}
- /**
- * Raise the connectionCount for the given ipAdrress
- * @param ip raise the connectionCount for the given ipAddress
- */
- private synchronized void addConnectionPerIP (String ip) {
- connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
- }
-
- /**
- * Get the count of connections for the given ip
- * @param ip the ipAddress to get the connections for.
- * @return count
- */
- private synchronized int getConnectionPerIP(String ip) {
- int count = 0;
- String curCount = null;
- Object c = connectionPerIP.get(ip);
-
- if (c != null) {
- curCount = c.toString();
-
- if (curCount != null) {
- return Integer.parseInt(curCount);
- }
- }
- return count;
- }
-
- /**
- * Set the connection count for the given ipAddress
- * @param ip ipAddres for which we want to set the count
- */
- private synchronized void removeConnectionPerIP (String ip) {
-
- int count = getConnectionPerIP(ip);
- if (count > 1) {
- connectionPerIP.put(ip,Integer.toString(count -1));
- } else {
- // not need this entry any more
- connectionPerIP.remove(ip);
- }
- }
-
- /**
- * Clear the connection count map
- */
- private synchronized void clearConnectionPerIP () {
- connectionPerIP.clear();
- }
-
/**
* Provides the body for the thread of execution for a ServerConnection.
* Connections made to the server socket are passed to an appropriate,
@@ -388,9 +336,9 @@
// We ignore this exception, as we already have an error condition.
}
continue;
- } else if ((maxOpenConnPerIP > 0) && (getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()) >= maxOpenConnPerIP)) {
+ } else if ((maxOpenConnPerIP > 0) && (connPerIpMap.getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()) >= maxOpenConnPerIP)) {
if (getLogger().isWarnEnabled()) {
- getLogger().warn("Maximum number of open connections per IP exceeded - refusing connection. Current number of connections for " + clientSocket.getInetAddress().getHostAddress() + " is " + getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()));
+ getLogger().warn("Maximum number of open connections per IP exceeded - refusing connection. Current number of connections for " + clientSocket.getInetAddress().getHostAddress() + " is " + connPerIpMap.getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()));
}
try {
clientSocket.close();
@@ -400,7 +348,7 @@
continue;
} else {
- addConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
+ connPerIpMap.addConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
clientSocket.setSoTimeout(socketTimeout);
runner = addClientConnectionRunner();
runner.setSocket(clientSocket);
@@ -417,7 +365,7 @@
getLogger().error("Internal error - insufficient threads available to service request. " +
Thread.activeCount() + " threads in service request pool.", e);
try {
- removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
+ connPerIpMap.removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
clientSocket.close();
} catch (IOException ignored) {
// We ignore this exception, as we already have an error condition.
@@ -529,7 +477,7 @@
} finally {
// remove this connection from map!
- removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
+ connPerIpMap.removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
// Close the underlying socket
try {
Modified: james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java?view=diff&rev=472864&r1=472863&r2=472864
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java (original)
+++ james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java Thu Nov 9 03:23:59 2006
@@ -136,6 +136,8 @@
public void service(ServiceManager componentManager) throws ServiceException {
setThreadManager((ThreadManager)componentManager.lookup(ThreadManager.ROLE));
}
+
+
/**
* Disconnects all the underlying ServerConnections
*/
@@ -159,6 +161,8 @@
getLogger().debug("Finishing SimpleConnectionManager dispose...");
}
}
+
+
/**
* Start managing a connection.
* Management involves accepting connections and farming them out to threads
@@ -169,6 +173,7 @@
* @param handlerFactory the factory from which to acquire handlers
* @param threadPool the thread pool to use
* @param maxOpenConnections the maximum number of open connections allowed for this server socket.
+ * @param maxOpenConnectionsPerIP the maximum number of open connections per IP allowed for this server socket.
* @exception Exception if an error occurs
*/
public void connect(
@@ -176,7 +181,8 @@
ServerSocket socket,
ConnectionHandlerFactory handlerFactory,
ThreadPool threadPool,
- int maxOpenConnections)
+ int maxOpenConnections,
+ int maxOpenConnectionsPerIP)
throws Exception {
if (disposed) {
throw new IllegalStateException("Connection manager has already been shutdown.");
@@ -187,16 +193,18 @@
if (maxOpenConnections < 0) {
throw new IllegalArgumentException("The maximum number of client connections per server socket cannot be less that zero.");
}
- if (maxOpenConnPerIP < 0) {
+ if (maxOpenConnectionsPerIP < 0) {
throw new IllegalArgumentException("The maximum number of client connections (per IP) per server socket cannot be less that zero.");
}
ServerConnection runner =
- new ServerConnection(socket, handlerFactory, threadPool, timeout, maxOpenConnections, maxOpenConnPerIP);
+ new ServerConnection(socket, handlerFactory, threadPool, timeout, maxOpenConnections, maxOpenConnectionsPerIP);
setupLogger(runner);
ContainerUtil.initialize(runner);
connectionMap.put(name, runner);
threadPool.execute(runner);
}
+
+
/**
* Start managing a connection.
* Management involves accepting connections and farming them out to threads
@@ -214,8 +222,10 @@
ConnectionHandlerFactory handlerFactory,
ThreadPool threadPool)
throws Exception {
- connect(name, socket, handlerFactory, threadPool, maxOpenConn);
+ connect(name, socket, handlerFactory, threadPool, maxOpenConn, maxOpenConnPerIP);
}
+
+
/**
* Start managing a connection.
* This is similar to other connect method except that it uses default thread pool.
@@ -229,6 +239,8 @@
throws Exception {
connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool());
}
+
+
/**
* Start managing a connection.
* This is similar to other connect method except that it uses default thread pool.
@@ -247,6 +259,8 @@
throws Exception {
connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool(), maxOpenConnections);
}
+
+
/**
* This shuts down all handlers and socket, waiting for each to gracefully shutdown.
*
@@ -256,6 +270,8 @@
public void disconnect(final String name) throws Exception {
disconnect(name, false);
}
+
+
/**
* This shuts down a connection.
* If tearDown is true then it will forcefully the connection and try
@@ -274,6 +290,24 @@
// TODO: deal with tear down parameter
connection.dispose();
}
+
+
+ /**
+ * @see org.apache.james.services.JamesConnectionManager#connect(java.lang.String, java.net.ServerSocket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory, org.apache.excalibur.thread.ThreadPool, int)
+ */
+ public void connect(String name, ServerSocket socket, ConnectionHandlerFactory handlerFactory, ThreadPool threadPool, int maxOpenConnections) throws Exception {
+ connect(name,socket,handlerFactory,threadPool,maxOpenConnections,maxOpenConnPerIP);
+ }
+
+
+ /**
+ * @see org.apache.james.services.JamesConnectionManager#connect(java.lang.String, java.net.ServerSocket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory, int, int)
+ */
+ public void connect(String name, ServerSocket socket, ConnectionHandlerFactory handlerFactory, int maxOpenConnections,int maxOpenConnectionsPerIP) throws Exception {
+ connect(name,socket,handlerFactory,threadManager.getDefaultThreadPool(),maxOpenConnections,maxOpenConnectionsPerIP);
+ }
+
+
/**
* Returns the default maximum number of open connections supported by this
* SimpleConnectionManager
@@ -284,15 +318,12 @@
return maxOpenConn;
}
+
/**
- * Returns the default maximum number of open connections per IP supported by this
- * SimpleConnectionManager
- *
- * @return the maximum number of connections
+ * @see org.apache.james.services.JamesConnectionManager#getMaximumNumberOfOpenConnectionsPerIP()
*/
public int getMaximumNumberOfOpenConnectionsPerIP() {
return maxOpenConnPerIP;
}
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
Re: svn commit: r472864 - in /james/server/trunk/src/java/org/apache/james: core/ services/ util/connection/
Posted by Bernd Fondermann <be...@googlemail.com>.
On 11/9/06, norman@apache.org <no...@apache.org> wrote:
> Author: norman
> + public synchronized void addConnectionPerIP (String ip) {
> + connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
> + }
> +
> + public synchronized int getConnectionPerIP(String ip) {
> + int count = 0;
> + String curCount = null;
> + Object c = connectionPerIP.get(ip);
> +
> + if (c != null) {
> + curCount = c.toString();
> +
> + if (curCount != null) {
> + return Integer.parseInt(curCount);
> + }
> + }
> + return count;
> + }
> +
BTW, while I like that ConnectionPerIpMap is extracted as a separate
class, why is String used as a value type and not Integer? This is
making life harder than it should be with all its casts and
conversions.
Alternatively, to reduce object creation on increment and conversions
even more, something like int[] counter = int[] {0} could be used
internally. arrays are objects if I am not mistaken and can be put
into maps.
Bernd
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
Re: svn commit: r472864 - in /james/server/trunk/src/java/org/apache/james: core/ services/ util/connection/
Posted by Bernd Fondermann <be...@googlemail.com>.
Hi, I don't like the way how the connect logic gets complicated and
hard to read by introducing the new parameters to connect() in the
three classes
james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
What was a couple of lines before in configureHelloName() is now
bloated unnecessarily.
We should reduce the number of connect() methods by using NULL to
signal we want a default to be used. For example putting the thread
pool parameter in all/most of them and allowing it to be null. If it
is, the default pool is used by SimpleConnectionManager.
(Another way to solve this would be a parameter object
http://www.refactoring.com/catalog/introduceParameterObject.html )
Thanks,
Bernd
On 11/9/06, norman@apache.org <no...@apache.org> wrote:
> Author: norman
> Date: Thu Nov 9 03:23:59 2006
> New Revision: 472864
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=472864
> Log:
> Add ConnectionLimit per IP. See JAMES-670
> Some refactoring
>
> Added:
> james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java (with props)
> Modified:
> james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
> james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
> james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
> james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
>
> Modified: james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
> URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java?view=diff&rev=472864&r1=472863&r2=472864
> ==============================================================================
> --- james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java (original)
> +++ james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java Thu Nov 9 03:23:59 2006
> @@ -181,6 +181,9 @@
> * The DNSServer
> */
> private DNSServer dnsServer = null;
> +
> + private boolean connPerIPConfigured = false;
> + private int connPerIP = 0;
>
> public void setConnectionManager(JamesConnectionManager connectionManager) {
> this.connectionManager = connectionManager;
> @@ -310,6 +313,29 @@
> .append(connectionLimit.intValue())
> .append(" connections.");
> getLogger().info(infoBuffer.toString());
> +
> + String connectionLimitPerIP = conf.getChild("connectionLimitPerIP").getValue(null);
> + if (connectionLimitPerIP != null) {
> + try {
> + connPerIP = new Integer(connectionLimitPerIP).intValue();
> + connPerIPConfigured = true;
> + } catch (NumberFormatException nfe) {
> + getLogger().error("Connection limit per IP value is not properly formatted.", nfe);
> + }
> + if (connPerIP < 0) {
> + getLogger().error("Connection limit per IP value cannot be less than zero.");
> + throw new ConfigurationException("Connection limit value cannot be less than zero.");
> + }
> + } else {
> + connPerIP = connectionManager.getMaximumNumberOfOpenConnectionsPerIP();
> + }
> + infoBuffer = new StringBuffer(128)
> + .append(getServiceType())
> + .append(" will allow a maximum of ")
> + .append(connPerIP)
> + .append(" per IP connections.");
> + getLogger().info(infoBuffer.toString());
> +
> }
>
> private void configureHelloName(Configuration handlerConfiguration) {
> @@ -401,15 +427,31 @@
>
> if ((connectionLimit != null)) {
> if (null != threadPool) {
> + if (connPerIPConfigured) {
> + connectionManager.connect(connectionName, serverSocket, this, threadPool, connectionLimit.intValue(),connPerIP);
> + } else {
> connectionManager.connect(connectionName, serverSocket, this, threadPool, connectionLimit.intValue());
> + }
> } else {
> - connectionManager.connect(connectionName, serverSocket, this, connectionLimit.intValue()); // default pool
> + if (connPerIPConfigured) {
> + connectionManager.connect(connectionName, serverSocket, this, connectionLimit.intValue(),connPerIP); // default pool
> + } else {
> + connectionManager.connect(connectionName, serverSocket, this, connectionLimit.intValue());
> + }
> }
> } else {
> if (null != threadPool) {
> - connectionManager.connect(connectionName, serverSocket, this, threadPool);
> + if (connPerIPConfigured) {
> + connectionManager.connect(connectionName, serverSocket, this, threadPool);
> } else {
> - connectionManager.connect(connectionName, serverSocket, this); // default pool
> + connectionManager.connect(connectionName, serverSocket, this, threadPool, 0, connPerIP);
> + }
> + } else {
> + if (connPerIPConfigured) {
> + connectionManager.connect(connectionName, serverSocket, this); // default pool
> + } else {
> + connectionManager.connect(connectionName, serverSocket, this, 0, connPerIP);
> + }
> }
> }
> }
>
> Modified: james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
> URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java?view=diff&rev=472864&r1=472863&r2=472864
> ==============================================================================
> --- james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java (original)
> +++ james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java Thu Nov 9 03:23:59 2006
> @@ -114,5 +114,51 @@
> ConnectionHandlerFactory handlerFactory )
> throws Exception;
>
> + /**
> + * Start managing a connection.
> + * Management involves accepting connections and farming them out to threads
> + * from pool to be handled.
> + *
> + * @param name the name of connection
> + * @param socket the ServerSocket from which to
> + * @param handlerFactory the factory from which to acquire handlers
> + * @param threadPool the thread pool to use
> + * @param maxOpenConnections the maximum number of open connections allowed for this server socket.
> + * @param maxOpenConnectionsPerIP the maximum number of open connections per IP allowed for this server socket.
> + * @throws Exception
> + */
> + void connect( String name,
> + ServerSocket socket,
> + ConnectionHandlerFactory handlerFactory,
> + ThreadPool threadPool,
> + int maxOpenConnections,
> + int maxOpenConnectionsPerIP)
> + throws Exception;
>
> + /**
> + * Start managing a connection.
> + * Management involves accepting connections and farming them out to threads
> + * from pool to be handled.
> + *
> + * @param name the name of connection
> + * @param socket the ServerSocket from which to
> + * @param handlerFactory the factory from which to acquire handlers
> + * @param maxOpenConnections the maximum number of open connections allowed for this server socket.
> + * @param maxOpenConnectionsPerIP the maximum number of open connections per IP allowed for this server socket.
> + * @throws Exception
> + */
> + void connect( String name,
> + ServerSocket socket,
> + ConnectionHandlerFactory handlerFactory,
> + int maxOpenConnections,
> + int maxOpenConnectionsPerIP)
> + throws Exception;
> +
> + /**
> + * Returns the default maximum number of open connections per IP supported by this
> + * SimpleConnectionManager
> + *
> + * @return the maximum number of connections
> + */
> + int getMaximumNumberOfOpenConnectionsPerIP();
> }
>
> Added: james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
> URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java?view=auto&rev=472864
> ==============================================================================
> --- james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java (added)
> +++ james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java Thu Nov 9 03:23:59 2006
> @@ -0,0 +1,91 @@
> +/****************************************************************
> + * 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.james.util.connection;
> +
> +import java.util.HashMap;
> +
> +/**
> + * Map which holds information about the connection count per ip
> + */
> +public class ConnectionPerIpMap {
> +
> + /**
> + * A HashMap of clientip and connections
> + */
> + private final HashMap connectionPerIP = new HashMap();
> +
> + /**
> + * Raise the connectionCount for the given ipAdrress
> + *
> + * @param ip raise the connectionCount for the given ipAddress
> + */
> + public synchronized void addConnectionPerIP (String ip) {
> + connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
> + }
> +
> + /**
> + * Get the count of connections for the given ip
> + *
> + * @param ip the ipAddress to get the connections for.
> + * @return count
> + */
> + public synchronized int getConnectionPerIP(String ip) {
> + int count = 0;
> + String curCount = null;
> + Object c = connectionPerIP.get(ip);
> +
> + if (c != null) {
> + curCount = c.toString();
> +
> + if (curCount != null) {
> + return Integer.parseInt(curCount);
> + }
> + }
> + return count;
> + }
> +
> + /**
> + * Set the connection count for the given ipAddress
> + *
> + * @param ip ipAddres for which we want to set the count
> + */
> + public synchronized void removeConnectionPerIP (String ip) {
> +
> + int count = getConnectionPerIP(ip);
> + if (count > 1) {
> + connectionPerIP.put(ip,Integer.toString(count -1));
> + } else {
> + // not need this entry any more
> + connectionPerIP.remove(ip);
> + }
> +
> + }
> +
> + /**
> + * Clear the connection count map
> + */
> + public synchronized void clearConnectionPerIP () {
> + connectionPerIP.clear();
> + }
> +
> +}
>
> Propchange: james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
> ------------------------------------------------------------------------------
> svn:eol-style = native
>
> Modified: james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
> URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java?view=diff&rev=472864&r1=472863&r2=472864
> ==============================================================================
> --- james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java (original)
> +++ james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java Thu Nov 9 03:23:59 2006
> @@ -27,7 +27,6 @@
> import java.net.Socket;
> import java.net.SocketException;
> import java.util.ArrayList;
> -import java.util.HashMap;
> import java.util.Iterator;
>
> import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
> @@ -107,17 +106,15 @@
> * connection will allow.
> */
> private int maxOpenConnPerIP;
> +
> + private ConnectionPerIpMap connPerIpMap;
>
> /**
> * A collection of client connection runners.
> */
> private final ArrayList clientConnectionRunners = new ArrayList();
>
> - /**
> - * A HashMap of clientip and connections
> - */
> - private final HashMap connectionPerIP = new HashMap();
> -
> +
> /**
> * The thread used to manage this server connection.
> */
> @@ -154,6 +151,7 @@
> */
> public void initialize() throws Exception {
> runnerPool = new HardResourceLimitingPool(theRunnerFactory, 5, maxOpenConn);
> + connPerIpMap = new ConnectionPerIpMap();
> ContainerUtil.enableLogging(runnerPool,getLogger());
> ContainerUtil.initialize(runnerPool);
> }
> @@ -212,7 +210,7 @@
> runner = null;
> }
> clientConnectionRunners.clear();
> - clearConnectionPerIP();
> + connPerIpMap.clearConnectionPerIP();
>
> }
>
> @@ -267,57 +265,7 @@
> synchronized (this) { notify(); } // match the wait(...) in the run() inner loop before accept().
> }
>
> - /**
> - * Raise the connectionCount for the given ipAdrress
> - * @param ip raise the connectionCount for the given ipAddress
> - */
> - private synchronized void addConnectionPerIP (String ip) {
> - connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
> - }
> -
> - /**
> - * Get the count of connections for the given ip
> - * @param ip the ipAddress to get the connections for.
> - * @return count
> - */
> - private synchronized int getConnectionPerIP(String ip) {
> - int count = 0;
> - String curCount = null;
> - Object c = connectionPerIP.get(ip);
> -
> - if (c != null) {
> - curCount = c.toString();
> -
> - if (curCount != null) {
> - return Integer.parseInt(curCount);
> - }
> - }
> - return count;
> - }
> -
> - /**
> - * Set the connection count for the given ipAddress
> - * @param ip ipAddres for which we want to set the count
> - */
> - private synchronized void removeConnectionPerIP (String ip) {
> -
> - int count = getConnectionPerIP(ip);
> - if (count > 1) {
> - connectionPerIP.put(ip,Integer.toString(count -1));
> - } else {
> - // not need this entry any more
> - connectionPerIP.remove(ip);
> - }
>
> - }
> -
> - /**
> - * Clear the connection count map
> - */
> - private synchronized void clearConnectionPerIP () {
> - connectionPerIP.clear();
> - }
> -
> /**
> * Provides the body for the thread of execution for a ServerConnection.
> * Connections made to the server socket are passed to an appropriate,
> @@ -388,9 +336,9 @@
> // We ignore this exception, as we already have an error condition.
> }
> continue;
> - } else if ((maxOpenConnPerIP > 0) && (getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()) >= maxOpenConnPerIP)) {
> + } else if ((maxOpenConnPerIP > 0) && (connPerIpMap.getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()) >= maxOpenConnPerIP)) {
> if (getLogger().isWarnEnabled()) {
> - getLogger().warn("Maximum number of open connections per IP exceeded - refusing connection. Current number of connections for " + clientSocket.getInetAddress().getHostAddress() + " is " + getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()));
> + getLogger().warn("Maximum number of open connections per IP exceeded - refusing connection. Current number of connections for " + clientSocket.getInetAddress().getHostAddress() + " is " + connPerIpMap.getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()));
> }
> try {
> clientSocket.close();
> @@ -400,7 +348,7 @@
> continue;
>
> } else {
> - addConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
> + connPerIpMap.addConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
> clientSocket.setSoTimeout(socketTimeout);
> runner = addClientConnectionRunner();
> runner.setSocket(clientSocket);
> @@ -417,7 +365,7 @@
> getLogger().error("Internal error - insufficient threads available to service request. " +
> Thread.activeCount() + " threads in service request pool.", e);
> try {
> - removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
> + connPerIpMap.removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
> clientSocket.close();
> } catch (IOException ignored) {
> // We ignore this exception, as we already have an error condition.
> @@ -529,7 +477,7 @@
> } finally {
>
> // remove this connection from map!
> - removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
> + connPerIpMap.removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>
> // Close the underlying socket
> try {
>
> Modified: james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
> URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java?view=diff&rev=472864&r1=472863&r2=472864
> ==============================================================================
> --- james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java (original)
> +++ james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java Thu Nov 9 03:23:59 2006
> @@ -136,6 +136,8 @@
> public void service(ServiceManager componentManager) throws ServiceException {
> setThreadManager((ThreadManager)componentManager.lookup(ThreadManager.ROLE));
> }
> +
> +
> /**
> * Disconnects all the underlying ServerConnections
> */
> @@ -159,6 +161,8 @@
> getLogger().debug("Finishing SimpleConnectionManager dispose...");
> }
> }
> +
> +
> /**
> * Start managing a connection.
> * Management involves accepting connections and farming them out to threads
> @@ -169,6 +173,7 @@
> * @param handlerFactory the factory from which to acquire handlers
> * @param threadPool the thread pool to use
> * @param maxOpenConnections the maximum number of open connections allowed for this server socket.
> + * @param maxOpenConnectionsPerIP the maximum number of open connections per IP allowed for this server socket.
> * @exception Exception if an error occurs
> */
> public void connect(
> @@ -176,7 +181,8 @@
> ServerSocket socket,
> ConnectionHandlerFactory handlerFactory,
> ThreadPool threadPool,
> - int maxOpenConnections)
> + int maxOpenConnections,
> + int maxOpenConnectionsPerIP)
> throws Exception {
> if (disposed) {
> throw new IllegalStateException("Connection manager has already been shutdown.");
> @@ -187,16 +193,18 @@
> if (maxOpenConnections < 0) {
> throw new IllegalArgumentException("The maximum number of client connections per server socket cannot be less that zero.");
> }
> - if (maxOpenConnPerIP < 0) {
> + if (maxOpenConnectionsPerIP < 0) {
> throw new IllegalArgumentException("The maximum number of client connections (per IP) per server socket cannot be less that zero.");
> }
> ServerConnection runner =
> - new ServerConnection(socket, handlerFactory, threadPool, timeout, maxOpenConnections, maxOpenConnPerIP);
> + new ServerConnection(socket, handlerFactory, threadPool, timeout, maxOpenConnections, maxOpenConnectionsPerIP);
> setupLogger(runner);
> ContainerUtil.initialize(runner);
> connectionMap.put(name, runner);
> threadPool.execute(runner);
> }
> +
> +
> /**
> * Start managing a connection.
> * Management involves accepting connections and farming them out to threads
> @@ -214,8 +222,10 @@
> ConnectionHandlerFactory handlerFactory,
> ThreadPool threadPool)
> throws Exception {
> - connect(name, socket, handlerFactory, threadPool, maxOpenConn);
> + connect(name, socket, handlerFactory, threadPool, maxOpenConn, maxOpenConnPerIP);
> }
> +
> +
> /**
> * Start managing a connection.
> * This is similar to other connect method except that it uses default thread pool.
> @@ -229,6 +239,8 @@
> throws Exception {
> connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool());
> }
> +
> +
> /**
> * Start managing a connection.
> * This is similar to other connect method except that it uses default thread pool.
> @@ -247,6 +259,8 @@
> throws Exception {
> connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool(), maxOpenConnections);
> }
> +
> +
> /**
> * This shuts down all handlers and socket, waiting for each to gracefully shutdown.
> *
> @@ -256,6 +270,8 @@
> public void disconnect(final String name) throws Exception {
> disconnect(name, false);
> }
> +
> +
> /**
> * This shuts down a connection.
> * If tearDown is true then it will forcefully the connection and try
> @@ -274,6 +290,24 @@
> // TODO: deal with tear down parameter
> connection.dispose();
> }
> +
> +
> + /**
> + * @see org.apache.james.services.JamesConnectionManager#connect(java.lang.String, java.net.ServerSocket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory, org.apache.excalibur.thread.ThreadPool, int)
> + */
> + public void connect(String name, ServerSocket socket, ConnectionHandlerFactory handlerFactory, ThreadPool threadPool, int maxOpenConnections) throws Exception {
> + connect(name,socket,handlerFactory,threadPool,maxOpenConnections,maxOpenConnPerIP);
> + }
> +
> +
> + /**
> + * @see org.apache.james.services.JamesConnectionManager#connect(java.lang.String, java.net.ServerSocket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory, int, int)
> + */
> + public void connect(String name, ServerSocket socket, ConnectionHandlerFactory handlerFactory, int maxOpenConnections,int maxOpenConnectionsPerIP) throws Exception {
> + connect(name,socket,handlerFactory,threadManager.getDefaultThreadPool(),maxOpenConnections,maxOpenConnectionsPerIP);
> + }
> +
> +
> /**
> * Returns the default maximum number of open connections supported by this
> * SimpleConnectionManager
> @@ -284,15 +318,12 @@
> return maxOpenConn;
> }
>
> +
> /**
> - * Returns the default maximum number of open connections per IP supported by this
> - * SimpleConnectionManager
> - *
> - * @return the maximum number of connections
> + * @see org.apache.james.services.JamesConnectionManager#getMaximumNumberOfOpenConnectionsPerIP()
> */
> public int getMaximumNumberOfOpenConnectionsPerIP() {
> return maxOpenConnPerIP;
> }
> -
>
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
> For additional commands, e-mail: server-dev-help@james.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
Re: svn commit: r472864 - in /james/server/trunk/src/java/org/apache/james:
core/ services/ util/connection/
Posted by Norman Maurer <nm...@byteaction.de>.
I have no problems with this.. I just followed the "old" behavoir to
kkep it accurate.
bye
Norman
Bernd Fondermann schrieb:
> Hi, I don't like the way how the connect logic gets complicated and
> hard to read by introducing the new parameters to connect() in the
> three classes
>
> james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
>
> james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
>
> james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
>
>
> What was a couple of lines before in configureHelloName() is now
> bloated unnecessarily.
>
> We should reduce the number of connect() methods by using NULL to
> signal we want a default to be used. For example putting the thread
> pool parameter in all/most of them and allowing it to be null. If it
> is, the default pool is used by SimpleConnectionManager.
>
> (Another way to solve this would be a parameter object
> http://www.refactoring.com/catalog/introduceParameterObject.html )
>
> Thanks,
>
> Bernd
>
> On 11/9/06, norman@apache.org <no...@apache.org> wrote:
>> Author: norman
>> Date: Thu Nov 9 03:23:59 2006
>> New Revision: 472864
>>
>> URL: http://svn.apache.org/viewvc?view=rev&rev=472864
>> Log:
>> Add ConnectionLimit per IP. See JAMES-670
>> Some refactoring
>>
>> Added:
>>
>> james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
>> (with props)
>> Modified:
>>
>> james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
>>
>>
>> james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
>>
>>
>> james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
>>
>>
>> james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
>>
>>
>> Modified:
>> james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
>>
>> URL:
>> http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java?view=diff&rev=472864&r1=472863&r2=472864
>>
>> ==============================================================================
>>
>> ---
>> james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
>> (original)
>> +++
>> james/server/trunk/src/java/org/apache/james/core/AbstractJamesService.java
>> Thu Nov 9 03:23:59 2006
>> @@ -181,6 +181,9 @@
>> * The DNSServer
>> */
>> private DNSServer dnsServer = null;
>> +
>> + private boolean connPerIPConfigured = false;
>> + private int connPerIP = 0;
>>
>> public void setConnectionManager(JamesConnectionManager
>> connectionManager) {
>> this.connectionManager = connectionManager;
>> @@ -310,6 +313,29 @@
>> .append(connectionLimit.intValue())
>> .append(" connections.");
>> getLogger().info(infoBuffer.toString());
>> +
>> + String connectionLimitPerIP =
>> conf.getChild("connectionLimitPerIP").getValue(null);
>> + if (connectionLimitPerIP != null) {
>> + try {
>> + connPerIP = new Integer(connectionLimitPerIP).intValue();
>> + connPerIPConfigured = true;
>> + } catch (NumberFormatException nfe) {
>> + getLogger().error("Connection limit per IP value is
>> not properly formatted.", nfe);
>> + }
>> + if (connPerIP < 0) {
>> + getLogger().error("Connection limit per IP value
>> cannot be less than zero.");
>> + throw new ConfigurationException("Connection limit
>> value cannot be less than zero.");
>> + }
>> + } else {
>> + connPerIP =
>> connectionManager.getMaximumNumberOfOpenConnectionsPerIP();
>> + }
>> + infoBuffer = new StringBuffer(128)
>> + .append(getServiceType())
>> + .append(" will allow a maximum of ")
>> + .append(connPerIP)
>> + .append(" per IP connections.");
>> + getLogger().info(infoBuffer.toString());
>> +
>> }
>>
>> private void configureHelloName(Configuration
>> handlerConfiguration) {
>> @@ -401,15 +427,31 @@
>>
>> if ((connectionLimit != null)) {
>> if (null != threadPool) {
>> + if (connPerIPConfigured) {
>> + connectionManager.connect(connectionName,
>> serverSocket, this, threadPool, connectionLimit.intValue(),connPerIP);
>> + } else {
>> connectionManager.connect(connectionName,
>> serverSocket, this, threadPool, connectionLimit.intValue());
>> + }
>> } else {
>> - connectionManager.connect(connectionName,
>> serverSocket, this, connectionLimit.intValue()); // default pool
>> + if (connPerIPConfigured) {
>> + connectionManager.connect(connectionName,
>> serverSocket, this, connectionLimit.intValue(),connPerIP); // default
>> pool
>> + } else {
>> + connectionManager.connect(connectionName,
>> serverSocket, this, connectionLimit.intValue());
>> + }
>> }
>> } else {
>> if (null != threadPool) {
>> - connectionManager.connect(connectionName,
>> serverSocket, this, threadPool);
>> + if (connPerIPConfigured) {
>> + connectionManager.connect(connectionName,
>> serverSocket, this, threadPool);
>> } else {
>> - connectionManager.connect(connectionName,
>> serverSocket, this); // default pool
>> + connectionManager.connect(connectionName,
>> serverSocket, this, threadPool, 0, connPerIP);
>> + }
>> + } else {
>> + if (connPerIPConfigured) {
>> + connectionManager.connect(connectionName,
>> serverSocket, this); // default pool
>> + } else {
>> + connectionManager.connect(connectionName,
>> serverSocket, this, 0, connPerIP);
>> + }
>> }
>> }
>> }
>>
>> Modified:
>> james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
>>
>> URL:
>> http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java?view=diff&rev=472864&r1=472863&r2=472864
>>
>> ==============================================================================
>>
>> ---
>> james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
>> (original)
>> +++
>> james/server/trunk/src/java/org/apache/james/services/JamesConnectionManager.java
>> Thu Nov 9 03:23:59 2006
>> @@ -114,5 +114,51 @@
>> ConnectionHandlerFactory handlerFactory )
>> throws Exception;
>>
>> + /**
>> + * Start managing a connection.
>> + * Management involves accepting connections and farming them
>> out to threads
>> + * from pool to be handled.
>> + *
>> + * @param name the name of connection
>> + * @param socket the ServerSocket from which to
>> + * @param handlerFactory the factory from which to acquire handlers
>> + * @param threadPool the thread pool to use
>> + * @param maxOpenConnections the maximum number of open
>> connections allowed for this server socket.
>> + * @param maxOpenConnectionsPerIP the maximum number of open
>> connections per IP allowed for this server socket.
>> + * @throws Exception
>> + */
>> + void connect( String name,
>> + ServerSocket socket,
>> + ConnectionHandlerFactory handlerFactory,
>> + ThreadPool threadPool,
>> + int maxOpenConnections,
>> + int maxOpenConnectionsPerIP)
>> + throws Exception;
>>
>> + /**
>> + * Start managing a connection.
>> + * Management involves accepting connections and farming them
>> out to threads
>> + * from pool to be handled.
>> + *
>> + * @param name the name of connection
>> + * @param socket the ServerSocket from which to
>> + * @param handlerFactory the factory from which to acquire handlers
>> + * @param maxOpenConnections the maximum number of open
>> connections allowed for this server socket.
>> + * @param maxOpenConnectionsPerIP the maximum number of open
>> connections per IP allowed for this server socket.
>> + * @throws Exception
>> + */
>> + void connect( String name,
>> + ServerSocket socket,
>> + ConnectionHandlerFactory handlerFactory,
>> + int maxOpenConnections,
>> + int maxOpenConnectionsPerIP)
>> + throws Exception;
>> +
>> + /**
>> + * Returns the default maximum number of open connections per IP
>> supported by this
>> + * SimpleConnectionManager
>> + *
>> + * @return the maximum number of connections
>> + */
>> + int getMaximumNumberOfOpenConnectionsPerIP();
>> }
>>
>> Added:
>> james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
>>
>> URL:
>> http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java?view=auto&rev=472864
>>
>> ==============================================================================
>>
>> ---
>> james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
>> (added)
>> +++
>> james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
>> Thu Nov 9 03:23:59 2006
>> @@ -0,0 +1,91 @@
>> +/****************************************************************
>> + * 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.james.util.connection;
>> +
>> +import java.util.HashMap;
>> +
>> +/**
>> + * Map which holds information about the connection count per ip
>> + */
>> +public class ConnectionPerIpMap {
>> +
>> + /**
>> + * A HashMap of clientip and connections
>> + */
>> + private final HashMap connectionPerIP = new HashMap();
>> +
>> + /**
>> + * Raise the connectionCount for the given ipAdrress
>> + *
>> + * @param ip raise the connectionCount for the given ipAddress
>> + */
>> + public synchronized void addConnectionPerIP (String ip) {
>> +
>> connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
>> + }
>> +
>> + /**
>> + * Get the count of connections for the given ip
>> + *
>> + * @param ip the ipAddress to get the connections for.
>> + * @return count
>> + */
>> + public synchronized int getConnectionPerIP(String ip) {
>> + int count = 0;
>> + String curCount = null;
>> + Object c = connectionPerIP.get(ip);
>> +
>> + if (c != null) {
>> + curCount = c.toString();
>> +
>> + if (curCount != null) {
>> + return Integer.parseInt(curCount);
>> + }
>> + }
>> + return count;
>> + }
>> +
>> + /**
>> + * Set the connection count for the given ipAddress
>> + *
>> + * @param ip ipAddres for which we want to set the count
>> + */
>> + public synchronized void removeConnectionPerIP (String ip) {
>> +
>> + int count = getConnectionPerIP(ip);
>> + if (count > 1) {
>> + connectionPerIP.put(ip,Integer.toString(count -1));
>> + } else {
>> + // not need this entry any more
>> + connectionPerIP.remove(ip);
>> + }
>> +
>> + }
>> +
>> + /**
>> + * Clear the connection count map
>> + */
>> + public synchronized void clearConnectionPerIP () {
>> + connectionPerIP.clear();
>> + }
>> +
>> +}
>>
>> Propchange:
>> james/server/trunk/src/java/org/apache/james/util/connection/ConnectionPerIpMap.java
>>
>> ------------------------------------------------------------------------------
>>
>> svn:eol-style = native
>>
>> Modified:
>> james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
>>
>> URL:
>> http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java?view=diff&rev=472864&r1=472863&r2=472864
>>
>> ==============================================================================
>>
>> ---
>> james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
>> (original)
>> +++
>> james/server/trunk/src/java/org/apache/james/util/connection/ServerConnection.java
>> Thu Nov 9 03:23:59 2006
>> @@ -27,7 +27,6 @@
>> import java.net.Socket;
>> import java.net.SocketException;
>> import java.util.ArrayList;
>> -import java.util.HashMap;
>> import java.util.Iterator;
>>
>> import
>> org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
>> @@ -107,17 +106,15 @@
>> * connection will allow.
>> */
>> private int maxOpenConnPerIP;
>> +
>> + private ConnectionPerIpMap connPerIpMap;
>>
>> /**
>> * A collection of client connection runners.
>> */
>> private final ArrayList clientConnectionRunners = new ArrayList();
>>
>> - /**
>> - * A HashMap of clientip and connections
>> - */
>> - private final HashMap connectionPerIP = new HashMap();
>> -
>> +
>> /**
>> * The thread used to manage this server connection.
>> */
>> @@ -154,6 +151,7 @@
>> */
>> public void initialize() throws Exception {
>> runnerPool = new HardResourceLimitingPool(theRunnerFactory,
>> 5, maxOpenConn);
>> + connPerIpMap = new ConnectionPerIpMap();
>> ContainerUtil.enableLogging(runnerPool,getLogger());
>> ContainerUtil.initialize(runnerPool);
>> }
>> @@ -212,7 +210,7 @@
>> runner = null;
>> }
>> clientConnectionRunners.clear();
>> - clearConnectionPerIP();
>> + connPerIpMap.clearConnectionPerIP();
>>
>> }
>>
>> @@ -267,57 +265,7 @@
>> synchronized (this) { notify(); } // match the wait(...) in
>> the run() inner loop before accept().
>> }
>>
>> - /**
>> - * Raise the connectionCount for the given ipAdrress
>> - * @param ip raise the connectionCount for the given ipAddress
>> - */
>> - private synchronized void addConnectionPerIP (String ip) {
>> -
>> connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
>> - }
>> -
>> - /**
>> - * Get the count of connections for the given ip
>> - * @param ip the ipAddress to get the connections for.
>> - * @return count
>> - */
>> - private synchronized int getConnectionPerIP(String ip) {
>> - int count = 0;
>> - String curCount = null;
>> - Object c = connectionPerIP.get(ip);
>> -
>> - if (c != null) {
>> - curCount = c.toString();
>> -
>> - if (curCount != null) {
>> - return Integer.parseInt(curCount);
>> - }
>> - }
>> - return count;
>> - }
>> -
>> - /**
>> - * Set the connection count for the given ipAddress
>> - * @param ip ipAddres for which we want to set the count
>> - */
>> - private synchronized void removeConnectionPerIP (String ip) {
>> -
>> - int count = getConnectionPerIP(ip);
>> - if (count > 1) {
>> - connectionPerIP.put(ip,Integer.toString(count -1));
>> - } else {
>> - // not need this entry any more
>> - connectionPerIP.remove(ip);
>> - }
>>
>> - }
>> -
>> - /**
>> - * Clear the connection count map
>> - */
>> - private synchronized void clearConnectionPerIP () {
>> - connectionPerIP.clear();
>> - }
>> -
>> /**
>> * Provides the body for the thread of execution for a
>> ServerConnection.
>> * Connections made to the server socket are passed to an
>> appropriate,
>> @@ -388,9 +336,9 @@
>> // We ignore this exception, as we
>> already have an error condition.
>> }
>> continue;
>> - } else if ((maxOpenConnPerIP > 0) &&
>> (getConnectionPerIP(clientSocket.getInetAddress().getHostAddress())
>> >= maxOpenConnPerIP)) {
>> + } else if ((maxOpenConnPerIP > 0) &&
>> (connPerIpMap.getConnectionPerIP(clientSocket.getInetAddress().getHostAddress())
>> >= maxOpenConnPerIP)) {
>> if (getLogger().isWarnEnabled()) {
>> - getLogger().warn("Maximum number of open
>> connections per IP exceeded - refusing connection. Current number of
>> connections for " + clientSocket.getInetAddress().getHostAddress() +
>> " is " +
>> getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()));
>> + getLogger().warn("Maximum number of open
>> connections per IP exceeded - refusing connection. Current number of
>> connections for " + clientSocket.getInetAddress().getHostAddress() +
>> " is " +
>> connPerIpMap.getConnectionPerIP(clientSocket.getInetAddress().getHostAddress()));
>>
>> }
>> try {
>> clientSocket.close();
>> @@ -400,7 +348,7 @@
>> continue;
>>
>> } else {
>> -
>> addConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>> +
>> connPerIpMap.addConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>>
>> clientSocket.setSoTimeout(socketTimeout);
>> runner = addClientConnectionRunner();
>> runner.setSocket(clientSocket);
>> @@ -417,7 +365,7 @@
>> getLogger().error("Internal error - insufficient
>> threads available to service request. " +
>> Thread.activeCount() + "
>> threads in service request pool.", e);
>> try {
>> -
>> removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>> +
>> connPerIpMap.removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>>
>> clientSocket.close();
>> } catch (IOException ignored) {
>> // We ignore this exception, as we already
>> have an error condition.
>> @@ -529,7 +477,7 @@
>> } finally {
>>
>> // remove this connection from map!
>> -
>> removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>> +
>> connPerIpMap.removeConnectionPerIP(clientSocket.getInetAddress().getHostAddress());
>>
>>
>> // Close the underlying socket
>> try {
>>
>> Modified:
>> james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
>>
>> URL:
>> http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java?view=diff&rev=472864&r1=472863&r2=472864
>>
>> ==============================================================================
>>
>> ---
>> james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
>> (original)
>> +++
>> james/server/trunk/src/java/org/apache/james/util/connection/SimpleConnectionManager.java
>> Thu Nov 9 03:23:59 2006
>> @@ -136,6 +136,8 @@
>> public void service(ServiceManager componentManager) throws
>> ServiceException {
>>
>> setThreadManager((ThreadManager)componentManager.lookup(ThreadManager.ROLE));
>>
>> }
>> +
>> +
>> /**
>> * Disconnects all the underlying ServerConnections
>> */
>> @@ -159,6 +161,8 @@
>> getLogger().debug("Finishing SimpleConnectionManager
>> dispose...");
>> }
>> }
>> +
>> +
>> /**
>> * Start managing a connection.
>> * Management involves accepting connections and farming them
>> out to threads
>> @@ -169,6 +173,7 @@
>> * @param handlerFactory the factory from which to acquire handlers
>> * @param threadPool the thread pool to use
>> * @param maxOpenConnections the maximum number of open
>> connections allowed for this server socket.
>> + * @param maxOpenConnectionsPerIP the maximum number of open
>> connections per IP allowed for this server socket.
>> * @exception Exception if an error occurs
>> */
>> public void connect(
>> @@ -176,7 +181,8 @@
>> ServerSocket socket,
>> ConnectionHandlerFactory handlerFactory,
>> ThreadPool threadPool,
>> - int maxOpenConnections)
>> + int maxOpenConnections,
>> + int maxOpenConnectionsPerIP)
>> throws Exception {
>> if (disposed) {
>> throw new IllegalStateException("Connection manager has
>> already been shutdown.");
>> @@ -187,16 +193,18 @@
>> if (maxOpenConnections < 0) {
>> throw new IllegalArgumentException("The maximum number
>> of client connections per server socket cannot be less that zero.");
>> }
>> - if (maxOpenConnPerIP < 0) {
>> + if (maxOpenConnectionsPerIP < 0) {
>> throw new IllegalArgumentException("The maximum number
>> of client connections (per IP) per server socket cannot be less that
>> zero.");
>> }
>> ServerConnection runner =
>> - new ServerConnection(socket, handlerFactory, threadPool,
>> timeout, maxOpenConnections, maxOpenConnPerIP);
>> + new ServerConnection(socket, handlerFactory, threadPool,
>> timeout, maxOpenConnections, maxOpenConnectionsPerIP);
>> setupLogger(runner);
>> ContainerUtil.initialize(runner);
>> connectionMap.put(name, runner);
>> threadPool.execute(runner);
>> }
>> +
>> +
>> /**
>> * Start managing a connection.
>> * Management involves accepting connections and farming them
>> out to threads
>> @@ -214,8 +222,10 @@
>> ConnectionHandlerFactory handlerFactory,
>> ThreadPool threadPool)
>> throws Exception {
>> - connect(name, socket, handlerFactory, threadPool, maxOpenConn);
>> + connect(name, socket, handlerFactory, threadPool,
>> maxOpenConn, maxOpenConnPerIP);
>> }
>> +
>> +
>> /**
>> * Start managing a connection.
>> * This is similar to other connect method except that it uses
>> default thread pool.
>> @@ -229,6 +239,8 @@
>> throws Exception {
>> connect(name, socket, handlerFactory,
>> threadManager.getDefaultThreadPool());
>> }
>> +
>> +
>> /**
>> * Start managing a connection.
>> * This is similar to other connect method except that it uses
>> default thread pool.
>> @@ -247,6 +259,8 @@
>> throws Exception {
>> connect(name, socket, handlerFactory,
>> threadManager.getDefaultThreadPool(), maxOpenConnections);
>> }
>> +
>> +
>> /**
>> * This shuts down all handlers and socket, waiting for each to
>> gracefully shutdown.
>> *
>> @@ -256,6 +270,8 @@
>> public void disconnect(final String name) throws Exception {
>> disconnect(name, false);
>> }
>> +
>> +
>> /**
>> * This shuts down a connection.
>> * If tearDown is true then it will forcefully the connection
>> and try
>> @@ -274,6 +290,24 @@
>> // TODO: deal with tear down parameter
>> connection.dispose();
>> }
>> +
>> +
>> + /**
>> + * @see
>> org.apache.james.services.JamesConnectionManager#connect(java.lang.String,
>> java.net.ServerSocket,
>> org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory,
>> org.apache.excalibur.thread.ThreadPool, int)
>> + */
>> + public void connect(String name, ServerSocket socket,
>> ConnectionHandlerFactory handlerFactory, ThreadPool threadPool, int
>> maxOpenConnections) throws Exception {
>> +
>> connect(name,socket,handlerFactory,threadPool,maxOpenConnections,maxOpenConnPerIP);
>>
>> + }
>> +
>> +
>> + /**
>> + * @see
>> org.apache.james.services.JamesConnectionManager#connect(java.lang.String,
>> java.net.ServerSocket,
>> org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory,
>> int, int)
>> + */
>> + public void connect(String name, ServerSocket socket,
>> ConnectionHandlerFactory handlerFactory, int maxOpenConnections,int
>> maxOpenConnectionsPerIP) throws Exception {
>> +
>> connect(name,socket,handlerFactory,threadManager.getDefaultThreadPool(),maxOpenConnections,maxOpenConnectionsPerIP);
>>
>> + }
>> +
>> +
>> /**
>> * Returns the default maximum number of open connections
>> supported by this
>> * SimpleConnectionManager
>> @@ -284,15 +318,12 @@
>> return maxOpenConn;
>> }
>>
>> +
>> /**
>> - * Returns the default maximum number of open connections per IP
>> supported by this
>> - * SimpleConnectionManager
>> - *
>> - * @return the maximum number of connections
>> + * @see
>> org.apache.james.services.JamesConnectionManager#getMaximumNumberOfOpenConnectionsPerIP()
>>
>> */
>> public int getMaximumNumberOfOpenConnectionsPerIP() {
>> return maxOpenConnPerIP;
>> }
>> -
>>
>> }
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
>> For additional commands, e-mail: server-dev-help@james.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
> For additional commands, e-mail: server-dev-help@james.apache.org
>
> !EXCUBATOR:1,455c16d253072105891194!
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
Re: svn commit: r472864 - in /james/server/trunk/src/java/org/apache/james: core/ services/ util/connection/
Posted by Bernd Fondermann <be...@googlemail.com>.
On 11/16/06, Norman Maurer <nm...@byteaction.de> wrote:
> Hi Bernd,
>
> you refer to an old version of this class. I changed the code 2 days ago to:
oops. sorry, didn't notice. fine then.
Bernd
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
Re: svn commit: r472864 - in /james/server/trunk/src/java/org/apache/james:
core/ services/ util/connection/
Posted by Norman Maurer <nm...@byteaction.de>.
Hi Bernd,
you refer to an old version of this class. I changed the code 2 days ago to:
/**
* A HashMap of clientip and connections
*/
private final HashMap connectionPerIP = new HashMap();
/**
* Raise the connectionCount for the given ipAdrress
*
* @param ip raise the connectionCount for the given ipAddress
*/
public synchronized void addConnectionPerIP (String ip) {
connectionPerIP.put(ip,new Integer(getConnectionPerIP(ip) +1));
}
/**
* Get the count of connections for the given ip
*
* @param ip the ipAddress to get the connections for.
* @return count
*/
public synchronized int getConnectionPerIP(String ip) {
Integer value = (Integer) connectionPerIP.get(ip);
if (value == null) {
return 0;
}
return value.intValue();
}
bye
Norman
Bernd Fondermann schrieb:
> On 11/9/06, norman@apache.org <no...@apache.org> wrote:
>> Author: norman
>> + public synchronized void addConnectionPerIP (String ip) {
>> +
>> connectionPerIP.put(ip,Integer.toString(getConnectionPerIP(ip) +1));
>> + }
>> +
>> + public synchronized int getConnectionPerIP(String ip) {
>> + int count = 0;
>> + String curCount = null;
>> + Object c = connectionPerIP.get(ip);
>> +
>> + if (c != null) {
>> + curCount = c.toString();
>> +
>> + if (curCount != null) {
>> + return Integer.parseInt(curCount);
>> + }
>> + }
>> + return count;
>> + }
>> +
>
> BTW, while I like that ConnectionPerIpMap is extracted as a separate
> class, why is String used as a value type and not Integer? This is
> making life harder than it should be with all its casts and
> conversions.
>
> Alternatively, to reduce object creation on increment and conversions
> even more, something like int[] counter = int[] {0} could be used
> internally. arrays are objects if I am not mistaken and can be put
> into maps.
>
> Bernd
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
> For additional commands, e-mail: server-dev-help@james.apache.org
>
> !EXCUBATOR:1,455c42e253071330016851!
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org