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 2010/04/29 19:21:37 UTC

svn commit: r939390 - in /james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty: ConnectionLimitUpstreamHandler.java ConnectionPerIpLimitUpstreamHandler.java

Author: norman
Date: Thu Apr 29 17:21:37 2010
New Revision: 939390

URL: http://svn.apache.org/viewvc?rev=939390&view=rev
Log:
Fix connection per ip handler for netty
Add some javadocs

Modified:
    james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionLimitUpstreamHandler.java
    james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionPerIpLimitUpstreamHandler.java

Modified: james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionLimitUpstreamHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionLimitUpstreamHandler.java?rev=939390&r1=939389&r2=939390&view=diff
==============================================================================
--- james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionLimitUpstreamHandler.java (original)
+++ james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionLimitUpstreamHandler.java Thu Apr 29 17:21:37 2010
@@ -21,13 +21,16 @@ package org.apache.james.socket.netty;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.ChannelPipeline;
 import org.jboss.netty.channel.ChannelPipelineCoverage;
 import org.jboss.netty.channel.ChannelStateEvent;
 import org.jboss.netty.channel.ChannelUpstreamHandler;
 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
 
 /**
- * {@link ChannelUpstreamHandler} which limit the concurrent connection
+ * {@link ChannelUpstreamHandler} which limit the concurrent connection. 
+ * 
+ * This handler must be used as singleton when adding it to the {@link ChannelPipeline} to work correctly
  *
  */
 @ChannelPipelineCoverage("all")

Modified: james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionPerIpLimitUpstreamHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionPerIpLimitUpstreamHandler.java?rev=939390&r1=939389&r2=939390&view=diff
==============================================================================
--- james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionPerIpLimitUpstreamHandler.java (original)
+++ james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionPerIpLimitUpstreamHandler.java Thu Apr 29 17:21:37 2010
@@ -19,10 +19,11 @@
 package org.apache.james.socket.netty;
 
 import java.net.InetSocketAddress;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.ChannelPipeline;
 import org.jboss.netty.channel.ChannelPipelineCoverage;
 import org.jboss.netty.channel.ChannelStateEvent;
 import org.jboss.netty.channel.ChannelUpstreamHandler;
@@ -30,36 +31,43 @@ import org.jboss.netty.channel.SimpleCha
 
 /**
  * {@link ChannelUpstreamHandler} which limit connections per IP
+ * 
+ * This handler must be used as singleton when adding it to the {@link ChannelPipeline} to work correctly
  *
  */
 @ChannelPipelineCoverage("all")
 public class ConnectionPerIpLimitUpstreamHandler extends SimpleChannelUpstreamHandler{
 
-    private final ConcurrentHashMap<String, AtomicInteger> connections = new ConcurrentHashMap<String, AtomicInteger>();
+    private final Map<String, Integer> connections = new HashMap<String, Integer>();
     private final int maxConnectionsPerIp;
     
     public ConnectionPerIpLimitUpstreamHandler(int maxConnectionsPerIp) {
         this.maxConnectionsPerIp = maxConnectionsPerIp;
     }
+    
     @Override
     public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
 
         if (maxConnectionsPerIp > 0) {
             InetSocketAddress remoteAddress = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
             String remoteIp = remoteAddress.getAddress().getHostAddress();
-            AtomicInteger count = connections.get(remoteIp);
+            synchronized (connections) {
+                Integer count = connections.get(remoteIp);
 
-            if (count == null) {
-                count = new AtomicInteger(1);
-                connections.put(remoteIp, count);
-            } else {
-                int connectionCount = count.incrementAndGet();
-                if (connectionCount > maxConnectionsPerIp) {
-                    ctx.getChannel().close();
-                    count.decrementAndGet(); 
+                if (count == null) {
+                    count = new Integer(1);
+                    connections.put(remoteIp, count);
+                } else {
+                    count++;
+                    if (count > maxConnectionsPerIp) {
+                        ctx.getChannel().close();
+                        count--;
+                    }
+                    connections.put(remoteIp, count);
                 }
+               
             }
-           
+            
         }
         
         super.channelOpen(ctx, e);
@@ -69,9 +77,13 @@ public class ConnectionPerIpLimitUpstrea
         if (maxConnectionsPerIp > 0) {
             InetSocketAddress remoteAddress = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
             String remoteIp = remoteAddress.getAddress().getHostAddress();
-            AtomicInteger count = connections.get(remoteIp);
-            if (count != null)
-                count.decrementAndGet();
+            synchronized (connections) {
+                Integer count = connections.get(remoteIp);
+                if (count != null) {
+                    count--;
+                    connections.put(remoteIp, count);
+                }              
+            }
         }
         super.channelClosed(ctx, e);
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org