From a386ceefd66b619c7716bf362b0726da7559db40 Mon Sep 17 00:00:00 2001 From: fhanik Date: Mon, 20 Dec 2010 16:42:13 +0000 Subject: [PATCH] Implement a maxConnection threshold for the JIoEndpoint, to be able to constraint how many connections the connector will accept git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1051202 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/tomcat/util/net/JIoEndpoint.java | 28 +++- .../catalina/connector/TestMaxConnections.java | 180 +++++++++++++++++++++ .../apache/catalina/startup/SimpleHttpClient.java | 12 +- webapps/docs/config/http.xml | 9 ++ 4 files changed, 219 insertions(+), 10 deletions(-) create mode 100644 test/org/apache/catalina/connector/TestMaxConnections.java diff --git a/java/org/apache/tomcat/util/net/JIoEndpoint.java b/java/org/apache/tomcat/util/net/JIoEndpoint.java index 23fa87c10..ecfa472c6 100644 --- a/java/org/apache/tomcat/util/net/JIoEndpoint.java +++ b/java/org/apache/tomcat/util/net/JIoEndpoint.java @@ -32,6 +32,7 @@ import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState; +import org.apache.tomcat.util.threads.CounterLatch; /** @@ -107,6 +108,8 @@ public class JIoEndpoint extends AbstractEndpoint { // Not supported return false; } + + protected CounterLatch connectionCounterLatch = null; // ------------------------------------------------ Handler Inner Interface @@ -197,20 +200,24 @@ public class JIoEndpoint extends AbstractEndpoint { break; } try { + //if we have reached max connections, wait + connectionCounterLatch.await(); // Accept the next incoming connection from the server socket Socket socket = serverSocketFactory.acceptSocket(serverSocket); // Configure the socket if (setSocketOptions(socket)) { - // Hand this socket off to an appropriate processor - if (!processSocket(socket)) { - // Close socket right away - try { - socket.close(); - } catch (IOException e) { - // Ignore + // Hand this socket off to an appropriate processor + if (!processSocket(socket)) { + // Close socket right away + try { + socket.close(); + } catch (IOException e) { + // Ignore + } + } else { + connectionCounterLatch.countUp(); } - } } else { // Close socket right away try { @@ -286,6 +293,7 @@ public class JIoEndpoint extends AbstractEndpoint { if (log.isTraceEnabled()) { log.trace("Closing socket:"+socket); } + connectionCounterLatch.countDown(); try { socket.getSocket().close(); } catch (IOException e) { @@ -373,6 +381,8 @@ public class JIoEndpoint extends AbstractEndpoint { if (getExecutor() == null) { createExecutor(); } + + connectionCounterLatch = new CounterLatch(0,getMaxConnections()); // Start acceptor threads for (int i = 0; i < acceptorThreadCount; i++) { @@ -394,6 +404,8 @@ public class JIoEndpoint extends AbstractEndpoint { @Override public void stopInternal() { + connectionCounterLatch.releaseAll(); + connectionCounterLatch = null; if (!paused) { pause(); } diff --git a/test/org/apache/catalina/connector/TestMaxConnections.java b/test/org/apache/catalina/connector/TestMaxConnections.java new file mode 100644 index 000000000..7bd2491ef --- /dev/null +++ b/test/org/apache/catalina/connector/TestMaxConnections.java @@ -0,0 +1,180 @@ +/* + * 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.catalina.connector; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.catalina.Context; +import org.apache.catalina.startup.SimpleHttpClient; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; + +public class TestMaxConnections extends TomcatBaseTest{ + + Tomcat tomcat = null; + static int soTimeout = 3000; + static int connectTimeout = 1000; + + public void test1() throws Exception { + init(); + start(); + ConnectThread[] t = new ConnectThread[10]; + int passcount = 0; + int connectfail = 0; + for (int i=0; i + +

The maximum number of connections that the server will accept and process + at any given time. When this number has been reached, the server will not accept any more + connections until the number of connections reach below this value. The operating system may still accept connections based + on the acceptCount setting. + This setting is currently only applicable to the blocking Java connectors (AJP/HTTP). + Default value is 10000.

+
+

Limits the total length of trailing headers in the last chunk of a chunked HTTP request. If the value is -1, no limit will be -- 2.11.0