Skip to content

Commit 7efdb91

Browse files
herausnicoll
authored andcommitted
Manage Tomcat queued connections
Adding two tomcat server properties: - server.tomcat.accept-count - server.tomcat.max-connections Closes gh-6433
1 parent d5b2f67 commit 7efdb91

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
* @author Eddú Meléndez
8787
* @author Quinten De Swaef
8888
* @author Venil Noronha
89+
* @author Aurélien Leboulanger
8990
*/
9091
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
9192
public class ServerProperties
@@ -656,6 +657,19 @@ public static class Tomcat {
656657
*/
657658
private Charset uriEncoding;
658659

660+
/**
661+
* Maximum amount of connections accept and process.
662+
* <p>Once the limit has been reached,
663+
* the operating system may still accept connections based on the @link{acceptCount} setting.</p>
664+
*/
665+
private int maxConnections = 0;
666+
667+
/**
668+
* Maximum queue length for incoming connection requests when all possible request processing threads are in use.
669+
* Any requests received when the queue is full will be refused.
670+
*/
671+
private int acceptCount = 0;
672+
659673
public int getMaxThreads() {
660674
return this.maxThreads;
661675
}
@@ -748,6 +762,22 @@ public void setUriEncoding(Charset uriEncoding) {
748762
this.uriEncoding = uriEncoding;
749763
}
750764

765+
public int getMaxConnections() {
766+
return this.maxConnections;
767+
}
768+
769+
public void setMaxConnections(int maxConnections) {
770+
this.maxConnections = maxConnections;
771+
}
772+
773+
public int getAcceptCount() {
774+
return this.acceptCount;
775+
}
776+
777+
public void setAcceptCount(int acceptCount) {
778+
this.acceptCount = acceptCount;
779+
}
780+
751781
void customizeTomcat(ServerProperties serverProperties,
752782
TomcatEmbeddedServletContainerFactory factory) {
753783
if (getBasedir() != null) {
@@ -782,6 +812,40 @@ void customizeTomcat(ServerProperties serverProperties,
782812
if (this.redirectContextRoot != null) {
783813
customizeRedirectContextRoot(factory, this.redirectContextRoot);
784814
}
815+
if (this.maxConnections > 0) {
816+
customizeMaxConnections(factory);
817+
}
818+
if (this.acceptCount > 0) {
819+
customizeAcceptCount(factory);
820+
}
821+
}
822+
823+
private void customizeAcceptCount(TomcatEmbeddedServletContainerFactory factory) {
824+
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
825+
826+
@Override
827+
public void customize(Connector connector) {
828+
ProtocolHandler handler = connector.getProtocolHandler();
829+
if (handler instanceof AbstractProtocol) {
830+
AbstractProtocol protocol = (AbstractProtocol) handler;
831+
protocol.setBacklog(Tomcat.this.acceptCount);
832+
}
833+
}
834+
});
835+
}
836+
837+
private void customizeMaxConnections(TomcatEmbeddedServletContainerFactory factory) {
838+
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
839+
840+
@Override
841+
public void customize(Connector connector) {
842+
ProtocolHandler handler = connector.getProtocolHandler();
843+
if (handler instanceof AbstractProtocol) {
844+
AbstractProtocol protocol = (AbstractProtocol) handler;
845+
protocol.setMaxConnections(Tomcat.this.maxConnections);
846+
}
847+
}
848+
});
785849
}
786850

787851
private void customizeConnectionTimeout(

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,22 @@ public void testCustomizeTomcatMinSpareThreads() throws Exception {
324324
assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10);
325325
}
326326

327+
@Test
328+
public void testCustomizeTomcatAcceptCount() throws Exception {
329+
Map<String, String> map = new HashMap<String, String>();
330+
map.put("server.tomcat.accept-count", "10");
331+
bindProperties(map);
332+
assertThat(this.properties.getTomcat().getAcceptCount()).isEqualTo(10);
333+
}
334+
335+
@Test
336+
public void testCustomizeTomcatMaxConnections() throws Exception {
337+
Map<String, String> map = new HashMap<String, String>();
338+
map.put("server.tomcat.max-connections", "5");
339+
bindProperties(map);
340+
assertThat(this.properties.getTomcat().getMaxConnections()).isEqualTo(5);
341+
}
342+
327343
@Test
328344
public void customizeTomcatDisplayName() throws Exception {
329345
Map<String, String> map = new HashMap<String, String>();

0 commit comments

Comments
 (0)