Skip to content

Manage Tomcat queued connections #6571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
* @author Eddú Meléndez
* @author Quinten De Swaef
* @author Venil Noronha
* @author Aurélien Leboulanger
*/
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
Expand Down Expand Up @@ -657,6 +658,19 @@ public static class Tomcat {
*/
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() {
return this.maxThreads;
}
Expand Down Expand Up @@ -772,6 +786,22 @@ public void setUriEncoding(Charset 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,
TomcatEmbeddedServletContainerFactory factory) {
if (getBasedir() != null) {
Expand Down Expand Up @@ -806,6 +836,40 @@ void customizeTomcat(ServerProperties 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ public void testCustomizeTomcatMinSpareThreads() throws Exception {
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
public void customizeTomcatDisplayName() throws Exception {
Map<String, String> map = new HashMap<String, String>();
Expand Down