Manage Tomcat queued connections

Adding two tomcat server properties:
- server.tomcat.accept-count
- server.tomcat.max-connections

Closes gh-6433
pull/7164/head
Aurélien Leboulanger 8 years ago committed by Stephane Nicoll
parent d5b2f67b5f
commit 7efdb91641

@ -86,6 +86,7 @@ import org.springframework.util.StringUtils;
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Quinten De Swaef * @author Quinten De Swaef
* @author Venil Noronha * @author Venil Noronha
* @author Aurélien Leboulanger
*/ */
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties public class ServerProperties
@ -656,6 +657,19 @@ public class ServerProperties
*/ */
private Charset uriEncoding; private Charset uriEncoding;
/**
* Maximum amount of connections accept and process.
* <p>Once the limit has been reached,
* the operating system may still accept connections based on the @link{acceptCount} setting.</p>
*/
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() { public int getMaxThreads() {
return this.maxThreads; return this.maxThreads;
} }
@ -748,6 +762,22 @@ public class ServerProperties
this.uriEncoding = uriEncoding; 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, void customizeTomcat(ServerProperties serverProperties,
TomcatEmbeddedServletContainerFactory factory) { TomcatEmbeddedServletContainerFactory factory) {
if (getBasedir() != null) { if (getBasedir() != null) {
@ -782,6 +812,40 @@ public class ServerProperties
if (this.redirectContextRoot != null) { if (this.redirectContextRoot != null) {
customizeRedirectContextRoot(factory, this.redirectContextRoot); 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( private void customizeConnectionTimeout(

@ -324,6 +324,22 @@ public class ServerPropertiesTests {
assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10); assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10);
} }
@Test
public void testCustomizeTomcatAcceptCount() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.accept-count", "10");
bindProperties(map);
assertThat(this.properties.getTomcat().getAcceptCount()).isEqualTo(10);
}
@Test
public void testCustomizeTomcatMaxConnections() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.max-connections", "5");
bindProperties(map);
assertThat(this.properties.getTomcat().getMaxConnections()).isEqualTo(5);
}
@Test @Test
public void customizeTomcatDisplayName() throws Exception { public void customizeTomcatDisplayName() throws Exception {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();

Loading…
Cancel
Save