diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 5a741a060ce..e55a4a77aeb 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -86,6 +86,7 @@ import org.springframework.util.StringUtils; * @author Eddú Meléndez * @author Quinten De Swaef * @author Venil Noronha + * @author Aurélien Leboulanger */ @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties @@ -656,6 +657,19 @@ public class ServerProperties */ private Charset uriEncoding; + /** + * Maximum number of connections that the server will accept and process + * at any given time. Once the limit has been reached, the operating system + * may still accept connections based on the "acceptCount" property. + */ + private int maxConnections = 0; + + /** + * Maximum queue length for incoming connection requests when all possible + * request processing threads are in use. + */ + private int acceptCount = 0; + public int getMaxThreads() { return this.maxThreads; } @@ -748,6 +762,22 @@ public class ServerProperties this.uriEncoding = uriEncoding; } + public int getMaxConnections() { + return this.maxConnections; + } + + public void setMaxConnections(int maxConnections) { + this.maxConnections = maxConnections; + } + + public int getAcceptCount() { + return this.acceptCount; + } + + public void setAcceptCount(int acceptCount) { + this.acceptCount = acceptCount; + } + void customizeTomcat(ServerProperties serverProperties, TomcatEmbeddedServletContainerFactory factory) { if (getBasedir() != null) { @@ -782,6 +812,40 @@ public class ServerProperties if (this.redirectContextRoot != null) { customizeRedirectContextRoot(factory, this.redirectContextRoot); } + if (this.maxConnections > 0) { + customizeMaxConnections(factory); + } + if (this.acceptCount > 0) { + customizeAcceptCount(factory); + } + } + + private void customizeAcceptCount(TomcatEmbeddedServletContainerFactory factory) { + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + + @Override + public void customize(Connector connector) { + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof AbstractProtocol) { + AbstractProtocol protocol = (AbstractProtocol) handler; + protocol.setBacklog(Tomcat.this.acceptCount); + } + } + }); + } + + private void customizeMaxConnections(TomcatEmbeddedServletContainerFactory factory) { + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + + @Override + public void customize(Connector connector) { + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof AbstractProtocol) { + AbstractProtocol protocol = (AbstractProtocol) handler; + protocol.setMaxConnections(Tomcat.this.maxConnections); + } + } + }); } private void customizeConnectionTimeout( diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 177c5f2dda9..dd276071fc4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -32,6 +32,7 @@ import javax.servlet.SessionTrackingMode; import org.apache.catalina.Context; import org.apache.catalina.Valve; import org.apache.catalina.valves.RemoteIpValve; +import org.apache.coyote.AbstractProtocol; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -435,6 +436,34 @@ public class ServerPropertiesTests { assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1"); } + @Test + public void customTomcatAcceptCount() { + Map map = new HashMap(); + map.put("server.tomcat.accept-count", "10"); + bindProperties(map); + + TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory(); + this.properties.customize(container); + TomcatEmbeddedServletContainer embeddedContainer = + (TomcatEmbeddedServletContainer) container.getEmbeddedServletContainer(); + assertThat(((AbstractProtocol) embeddedContainer.getTomcat().getConnector() + .getProtocolHandler()).getBacklog()).isEqualTo(10); + } + + @Test + public void customTomcatMaxConnections() { + Map map = new HashMap(); + map.put("server.tomcat.max-connections", "5"); + bindProperties(map); + + TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory(); + this.properties.customize(container); + TomcatEmbeddedServletContainer embeddedContainer = + (TomcatEmbeddedServletContainer) container.getEmbeddedServletContainer(); + assertThat(((AbstractProtocol) embeddedContainer.getTomcat().getConnector() + .getProtocolHandler()).getMaxConnections()).isEqualTo(5); + } + @Test public void defaultUseForwardHeadersUndertow() throws Exception { UndertowEmbeddedServletContainerFactory container = spy( diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 4750e0db17b..d3a3e4c8df0 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -193,6 +193,7 @@ content into your application; rather pick only the properties that you need. server.ssl.trust-store-password= # Password used to access the trust store. server.ssl.trust-store-provider= # Provider for the trust store. server.ssl.trust-store-type= # Type of the trust store. + server.tomcat.accept-count= # Maximum queue length for incoming connection requests when all possible request processing threads are in use. server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute. server.tomcat.accesslog.enabled=false # Enable access log. server.tomcat.accesslog.pattern=common # Format pattern for access logs. @@ -208,6 +209,7 @@ content into your application; rather pick only the properties that you need. 172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses. + server.tomcat.max-connections= # Maximum number of connections that the server will accept and process at any given time. server.tomcat.max-threads=0 # Maximum amount of worker threads. server.tomcat.min-spare-threads=0 # Minimum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.