refactor latch usage, since its shared by all connectors
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 21 Dec 2010 17:40:57 +0000 (17:40 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 21 Dec 2010 17:40:57 +0000 (17:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1051578 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/tomcat/util/net/AbstractEndpoint.java
java/org/apache/tomcat/util/net/AprEndpoint.java
java/org/apache/tomcat/util/net/JIoEndpoint.java
java/org/apache/tomcat/util/net/NioEndpoint.java

index c3dcda0..8e80c3b 100644 (file)
@@ -126,7 +126,7 @@ public abstract class AbstractEndpoint {
     /**
      * counter for nr of connections handled by an endpoint
      */
-    protected volatile CounterLatch connectionCounterLatch = null;
+    private volatile CounterLatch connectionCounterLatch = null;
 
     /**
      * Socket properties
@@ -576,6 +576,43 @@ public abstract class AbstractEndpoint {
 
     protected abstract Log getLog();
     public abstract boolean getUseSendfile();
+    
+    protected CounterLatch initializeConnectionLatch() {
+        if (connectionCounterLatch==null) {
+            connectionCounterLatch = new CounterLatch(0,getMaxConnections());
+        }
+        return connectionCounterLatch;
+    }
+    
+    protected void releaseConnectionLatch() {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) latch.releaseAll();
+        connectionCounterLatch = null;
+    }
+    
+    protected void awaitConnection() throws InterruptedException {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) latch.await();
+    }
+    
+    protected long countUpConnection() {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) return latch.countUp();
+        else return -1;
+    }
+    
+    protected long countDownConnection() {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) {
+            long result = latch.countDown();
+            if (result<0) {
+                getLog().warn("Incorrect connection count, multiple socket.close called on the same socket." );
+            }
+            return result;
+        } else return -1;
+    }
+    
+    
 
     // --------------------  SSL related properties --------------------
 
index 393b44e..c4025f3 100644 (file)
@@ -40,6 +40,7 @@ import org.apache.tomcat.jni.SSLSocket;
 import org.apache.tomcat.jni.Socket;
 import org.apache.tomcat.jni.Status;
 import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.threads.CounterLatch;
 
 
 /**
@@ -531,6 +532,8 @@ public class AprEndpoint extends AbstractEndpoint {
             if (getExecutor() == null) {
                 createExecutor();
             }
+            
+            initializeConnectionLatch();
 
             // Start poller threads
             pollers = new Poller[pollerThreadCount];
@@ -592,6 +595,7 @@ public class AprEndpoint extends AbstractEndpoint {
      */
     @Override
     public void stopInternal() {
+        releaseConnectionLatch();
         if (!paused) {
             pause();
         }
@@ -885,6 +889,7 @@ public class AprEndpoint extends AbstractEndpoint {
             // parent pool or acceptor socket.
             // In any case disable double free which would cause JVM core.
             Socket.destroy(socket);
+            countDownConnection();
         }
     }
 
@@ -926,8 +931,12 @@ public class AprEndpoint extends AbstractEndpoint {
                     break;
                 }
                 try {
+                    //if we have reached max connections, wait
+                    awaitConnection();
                     // Accept the next incoming connection from the server socket
                     long socket = Socket.accept(serverSock);
+                    //increment socket count
+                    countUpConnection();
                     /*
                      * In the case of a deferred accept unlockAccept needs to
                      * send data. This data will be rubbish, so destroy the
index 1cacd90..f7e2e9b 100644 (file)
@@ -201,7 +201,7 @@ public class JIoEndpoint extends AbstractEndpoint {
                 }
                 try {
                     //if we have reached max connections, wait
-                    connectionCounterLatch.await();
+                    awaitConnection();                    
                     // Accept the next incoming connection from the server socket
                     Socket socket = serverSocketFactory.acceptSocket(serverSocket);
                     
@@ -216,7 +216,7 @@ public class JIoEndpoint extends AbstractEndpoint {
                                 // Ignore
                             }
                         } else {
-                            connectionCounterLatch.countUp();
+                            countUpConnection();
                         }
                     } else {
                         // Close socket right away
@@ -293,7 +293,7 @@ public class JIoEndpoint extends AbstractEndpoint {
                         if (log.isTraceEnabled()) {
                             log.trace("Closing socket:"+socket);
                         }
-                        connectionCounterLatch.countDown();
+                        countDownConnection();
                         try {
                             socket.getSocket().close();
                         } catch (IOException e) {
@@ -382,7 +382,7 @@ public class JIoEndpoint extends AbstractEndpoint {
                 createExecutor();
             }
             
-            connectionCounterLatch = new CounterLatch(0,getMaxConnections());
+            initializeConnectionLatch();
 
             // Start acceptor threads
             for (int i = 0; i < acceptorThreadCount; i++) {
@@ -404,8 +404,7 @@ public class JIoEndpoint extends AbstractEndpoint {
 
     @Override
     public void stopInternal() {
-        connectionCounterLatch.releaseAll();
-        connectionCounterLatch = null;
+        releaseConnectionLatch();
         if (!paused) {
             pause();
         }
index ac56e62..4fe43fa 100644 (file)
@@ -567,13 +567,14 @@ public class NioEndpoint extends AbstractEndpoint {
         if (!running) {
             running = true;
             paused = false;
-            
-            connectionCounterLatch = new CounterLatch(0, getMaxConnections()); 
+             
             // Create worker collection
             if ( getExecutor() == null ) {
                 createExecutor();
             }
 
+            initializeConnectionLatch();
+            
             // Start poller threads
             pollers = new Poller[getPollerThreadCount()];
             for (int i=0; i<pollers.length; i++) {
@@ -600,8 +601,7 @@ public class NioEndpoint extends AbstractEndpoint {
      */
     @Override
     public void stopInternal() {
-        connectionCounterLatch.releaseAll();
-        connectionCounterLatch = null;
+        releaseConnectionLatch();
         if (!paused) {
             pause();
         }
@@ -813,7 +813,7 @@ public class NioEndpoint extends AbstractEndpoint {
                 }
                 try {
                     //if we have reached max connections, wait
-                    connectionCounterLatch.await();
+                    awaitConnection();
                     // Accept the next incoming connection from the server socket
                     SocketChannel socket = serverSock.accept();
                     // Hand this socket off to an appropriate processor
@@ -831,7 +831,7 @@ public class NioEndpoint extends AbstractEndpoint {
                                     log.debug("", ix);
                             }
                         } else {
-                            connectionCounterLatch.countUp();
+                            countUpConnection();
                         }
                     }
                 } catch (SocketTimeoutException sx) {
@@ -1066,9 +1066,7 @@ public class NioEndpoint extends AbstractEndpoint {
                 try {if (ka!=null && ka.getSendfileData()!=null && ka.getSendfileData().fchannel!=null && ka.getSendfileData().fchannel.isOpen()) ka.getSendfileData().fchannel.close();}catch (Exception ignore){}
                 if (ka!=null) {
                     ka.reset();
-                    if (connectionCounterLatch.countDown()<0) {
-                        log.warn("Incorrect connection count, multiple cancel called on the same key?" );
-                    }
+                    countDownConnection(); 
                 }
             } catch (Throwable e) {
                 ExceptionUtils.handleThrowable(e);