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 5a741a060c..44e07102d3 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 amount of connections accept and process. + *
Once the limit has been reached, + * the operating system may still accept connections based on the @link{acceptCount} setting.
+ */ + private int maxConnections = 0; + + /** + * Maximum queue length for incoming connection requests when all possible request processing threads are in use. + * Any requests received when the queue is full will be refused. + */ + 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 177c5f2dda..3de4bbddea 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 @@ -324,6 +324,22 @@ public class ServerPropertiesTests { assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10); } + @Test + public void testCustomizeTomcatAcceptCount() throws Exception { + Map