Author: norman
Date: Thu Apr 29 15:56:38 2010
New Revision: 939355

URL: http://svn.apache.org/viewvc?rev=939355&view=rev
Log:
Fix connection limiting when using netty

Modified:
    
james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/AbstractChannelPipelineFactory.java
    
james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/ConnectionLimitUpstreamHandler.java
    
james/server/trunk/smtpserver/src/test/java/org/apache/james/smtpserver/NioSMTPServerTest.java

Modified: 
james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/AbstractChannelPipelineFactory.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/AbstractChannelPipelineFactory.java?rev=939355&r1=939354&r2=939355&view=diff
==============================================================================
--- 
james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/AbstractChannelPipelineFactory.java
 (original)
+++ 
james/server/trunk/netty-socket/src/main/java/org/apache/james/socket/netty/AbstractChannelPipelineFactory.java
 Thu Apr 29 15:56:38 2010
@@ -41,6 +41,13 @@ public abstract class AbstractChannelPip
 
     public final static int MAX_LINE_LENGTH = 8192;
     private final Timer timer = new HashedWheelTimer();
+    private final ConnectionLimitUpstreamHandler connectionLimitHandler;
+    private final ConnectionPerIpLimitUpstreamHandler 
connectionPerIpLimitHandler;
+    
+    public AbstractChannelPipelineFactory() {
+        connectionLimitHandler = new 
ConnectionLimitUpstreamHandler(getMaxConnections());
+        connectionPerIpLimitHandler = new 
ConnectionPerIpLimitUpstreamHandler(getMaxConnectionsPerIP());
+    }
     /*
      * (non-Javadoc)
      * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
@@ -49,9 +56,9 @@ public abstract class AbstractChannelPip
         // Create a default pipeline implementation.
         ChannelPipeline pipeline = pipeline();
         
-        pipeline.addLast("connectionLimit", new 
ConnectionLimitUpstreamHandler(getMaxConnections()));
+        pipeline.addLast("connectionLimit", connectionLimitHandler);
 
-        pipeline.addLast("connectionPerIpLimit", new 
ConnectionPerIpLimitUpstreamHandler(getMaxConnectionsPerIP()));
+        pipeline.addLast("connectionPerIpLimit", connectionPerIpLimitHandler);
 
         
         // Add the text line decoder which limit the max line length, don't 
strip the delimiter and use CRLF as delimiter

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=939355&r1=939354&r2=939355&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 15:56:38 2010
@@ -33,18 +33,19 @@ import org.jboss.netty.channel.SimpleCha
 @ChannelPipelineCoverage("all")
 public class ConnectionLimitUpstreamHandler extends 
SimpleChannelUpstreamHandler{
 
-    private final AtomicInteger connections = new AtomicInteger(0);
+    private static final AtomicInteger connections = new AtomicInteger(0);
     private final int maxConnections;
     
     public ConnectionLimitUpstreamHandler(int maxConnections) {
         this.maxConnections = maxConnections;
     }
+    
     @Override
     public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) 
throws Exception {
         if (maxConnections > 0) {
             int currentCount = connections.getAndIncrement();
             
-            if (currentCount > maxConnections) {
+            if (currentCount + 1 > maxConnections) {
                 ctx.getChannel().close();
                 connections.decrementAndGet();
             }

Modified: 
james/server/trunk/smtpserver/src/test/java/org/apache/james/smtpserver/NioSMTPServerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/test/java/org/apache/james/smtpserver/NioSMTPServerTest.java?rev=939355&r1=939354&r2=939355&view=diff
==============================================================================
--- 
james/server/trunk/smtpserver/src/test/java/org/apache/james/smtpserver/NioSMTPServerTest.java
 (original)
+++ 
james/server/trunk/smtpserver/src/test/java/org/apache/james/smtpserver/NioSMTPServerTest.java
 Thu Apr 29 15:56:38 2010
@@ -70,5 +70,35 @@ public class NioSMTPServerTest extends A
         smtpProtocol.quit();
         smtpProtocol.disconnect();
     }
+    
+    public void testConnectionLimit() throws Exception {
+        m_testConfiguration.setConnectionLimit(2);
+        finishSetUp(m_testConfiguration);
+
+        SMTPClient smtpProtocol = new SMTPClient();
+        smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
+        SMTPClient smtpProtocol2 = new SMTPClient();
+        smtpProtocol2.connect("127.0.0.1", m_smtpListenerPort);
+        
+        SMTPClient smtpProtocol3 = new SMTPClient();
+
+        try {
+            smtpProtocol3.connect("127.0.0.1", m_smtpListenerPort);
+            Thread.sleep(3000);
+            fail("Shold disconnect connection 3");
+        } catch (Exception e) {
+            
+        }
+        
+        smtpProtocol.quit();
+        smtpProtocol.disconnect();
+        smtpProtocol2.quit();
+        smtpProtocol2.disconnect();
+        
+        smtpProtocol3.connect("127.0.0.1", m_smtpListenerPort);
+        Thread.sleep(3000);
+
+       
+    }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to