From 3059f0e2e233000fcd8f8fbd7a0d376b7bbf2b0c Mon Sep 17 00:00:00 2001 From: artsiom Date: Thu, 1 Nov 2018 11:49:31 +0300 Subject: [PATCH 1/2] Add configuration property for configuring Tomcat's processor cache See gh-15054 --- .../boot/autoconfigure/web/ServerProperties.java | 15 +++++++++++++++ .../TomcatWebServerFactoryCustomizer.java | 9 +++++++++ .../TomcatWebServerFactoryCustomizerTests.java | 6 ++++++ .../asciidoc/appendix-application-properties.adoc | 1 + 4 files changed, 31 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 72aa67f64e9..682dda50c0d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -56,6 +56,7 @@ import org.springframework.util.unit.DataSize; * @author Brian Clozel * @author Olivier Lamy * @author Chentao Qu + * @author Artsiom Yudovin */ @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties { @@ -369,6 +370,12 @@ public class ServerProperties { */ private int acceptCount = 100; + /** + * Maximum number of idle processors that will be retained in the cache and reused + * with a subsequent request. + */ + private int processorCache = 200; + /** * Comma-separated list of additional patterns that match jars to ignore for TLD * scanning. The special '?' and '*' characters can be used in the pattern to @@ -524,6 +531,14 @@ public class ServerProperties { this.acceptCount = acceptCount; } + public int getProcessorCache() { + return this.processorCache; + } + + public void setProcessorCache(int processorCache) { + this.processorCache = processorCache; + } + public List getAdditionalTldSkipPatterns() { return this.additionalTldSkipPatterns; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index bc5a438df15..903bba08664 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -108,6 +108,8 @@ public class TomcatWebServerFactoryCustomizer implements .to((maxConnections) -> customizeMaxConnections(factory, maxConnections)); propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive) .to((acceptCount) -> customizeAcceptCount(factory, acceptCount)); + propertyMapper.from(tomcatProperties::getProcessorCache).when(this::isPositive) + .to((processorCache) -> customizeProcessorCache(factory, processorCache)); customizeStaticResources(factory); customizeErrorReportValve(properties.getError(), factory); } @@ -156,6 +158,13 @@ public class TomcatWebServerFactoryCustomizer implements }); } + private void customizeProcessorCache(ConfigurableTomcatWebServerFactory factory, + int processorCache) { + factory.addConnectorCustomizers(( + connector) -> ((AbstractHttp11Protocol) connector.getProtocolHandler()) + .setProcessorCache(processorCache)); + } + private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory) { Tomcat tomcatProperties = this.serverProperties.getTomcat(); String protocolHeader = tomcatProperties.getProtocolHeader(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java index 5f4b7615f1f..13030e9cb18 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java @@ -87,6 +87,12 @@ public class TomcatWebServerFactoryCustomizerTests { .isEqualTo(10)); } + @Test + public void customProcessorCache() { + bind("server.tomcat.processor-cache=100"); + assertThat(this.serverProperties.getTomcat().getProcessorCache()).isEqualTo(100); + } + @Test public void customBackgroundProcessorDelay() { bind("server.tomcat.background-processor-delay=5"); diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index b1b0eedf422..32e6246746b 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -276,6 +276,7 @@ content into your application. Rather, pick only the properties that you need. server.tomcat.max-threads=200 # Maximum amount of worker threads. server.tomcat.min-spare-threads=10 # Minimum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. + server.tomcat.processor-cache= # Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL. server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path. From 1b40b0edf112b764db773b671a27414ddcedf3fe Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 3 Dec 2018 11:13:40 +0100 Subject: [PATCH 2/2] Polish contribution Closes gh-15054 --- .../embedded/TomcatWebServerFactoryCustomizer.java | 14 +++++++------- .../autoconfigure/web/ServerPropertiesTests.java | 6 ++++++ .../asciidoc/appendix-application-properties.adoc | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index 903bba08664..04b965d1c9c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -136,6 +136,13 @@ public class TomcatWebServerFactoryCustomizer implements }); } + private void customizeProcessorCache(ConfigurableTomcatWebServerFactory factory, + int processorCache) { + factory.addConnectorCustomizers(( + connector) -> ((AbstractHttp11Protocol) connector.getProtocolHandler()) + .setProcessorCache(processorCache)); + } + private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory, int maxConnections) { factory.addConnectorCustomizers((connector) -> { @@ -158,13 +165,6 @@ public class TomcatWebServerFactoryCustomizer implements }); } - private void customizeProcessorCache(ConfigurableTomcatWebServerFactory factory, - int processorCache) { - factory.addConnectorCustomizers(( - connector) -> ((AbstractHttp11Protocol) connector.getProtocolHandler()) - .setProcessorCache(processorCache)); - } - private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory) { Tomcat tomcatProperties = this.serverProperties.getTomcat(); String protocolHeader = tomcatProperties.getProtocolHeader(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 64bcafe2508..2872caddb43 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -206,6 +206,12 @@ public class ServerPropertiesTests { .isEqualTo(getDefaultProtocol().getAcceptCount()); } + @Test + public void tomcatProcessorCacheMatchesProtocolDefault() throws Exception { + assertThat(this.properties.getTomcat().getProcessorCache()) + .isEqualTo(getDefaultProtocol().getProcessorCache()); + } + @Test public void tomcatMaxConnectionsMatchesProtocolDefault() throws Exception { assertThat(this.properties.getTomcat().getMaxConnections()) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 32e6246746b..57c33719e66 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -276,7 +276,7 @@ content into your application. Rather, pick only the properties that you need. server.tomcat.max-threads=200 # Maximum amount of worker threads. server.tomcat.min-spare-threads=10 # Minimum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. - server.tomcat.processor-cache= # Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. + server.tomcat.processor-cache=200 # Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL. server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path.