Allow connection timeout to be configured via the environment

Closes gh-6072
This commit is contained in:
Misagh Moayyed 2016-05-30 10:48:28 -07:00 committed by Andy Wilkinson
parent 9891eb5432
commit 6007d7efc1
2 changed files with 59 additions and 4 deletions

View File

@ -30,6 +30,7 @@ import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import javax.validation.constraints.NotNull;
import io.undertow.Undertow;
import io.undertow.Undertow.Builder;
import io.undertow.UndertowOptions;
import org.apache.catalina.Context;
@ -43,6 +44,7 @@ import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.HandlerWrapper;
@ -145,6 +147,13 @@ public class ServerProperties
*/
private int maxHttpPostSize = 0; // bytes
/**
* The number of milliseconds connectors will wait for another HTTP request before closing the connection.
* The default value is to use the value that has been set for the connectionTimeout attribute.
* Use a value of -1 to indicate no (i.e. infinite) timeout.
*/
private int connectionTimeout = -1;
private Session session = new Session();
@NestedConfigurationProperty
@ -366,6 +375,14 @@ public class ServerProperties
return (platform == null ? false : platform.isUsingForwardHeaders());
}
public int getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(final int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public ErrorProperties getError() {
return this.error;
}
@ -769,6 +786,16 @@ public class ServerProperties
if (getUriEncoding() != null) {
factory.setUriEncoding(getUriEncoding());
}
customizeConnectionTimeout(serverProperties, factory);
}
private void customizeConnectionTimeout(final ServerProperties serverProperties, final TomcatEmbeddedServletContainerFactory factory) {
for (Connector connector : factory.getAdditionalTomcatConnectors()) {
if (connector.getProtocolHandler() instanceof AbstractProtocol) {
final AbstractProtocol handler = (AbstractProtocol) connector.getProtocolHandler();
handler.setConnectionTimeout(serverProperties.getConnectionTimeout());
}
}
}
private void customizeBackgroundProcessorDelay(
@ -978,8 +1005,8 @@ public class ServerProperties
this.selectors = selectors;
}
void customizeJetty(ServerProperties serverProperties,
JettyEmbeddedServletContainerFactory factory) {
void customizeJetty(final ServerProperties serverProperties,
JettyEmbeddedServletContainerFactory factory) {
factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders());
if (this.acceptors != null) {
factory.setAcceptors(this.acceptors);
@ -994,6 +1021,22 @@ public class ServerProperties
if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
}
customizeConnectionTimeout(serverProperties, factory);
}
private void customizeConnectionTimeout(final ServerProperties serverProperties,
final JettyEmbeddedServletContainerFactory factory) {
factory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(final Server server) {
for (org.eclipse.jetty.server.Connector connector : server.getConnectors()) {
if (connector instanceof ServerConnector) {
ServerConnector serverConnector = (ServerConnector) connector;
serverConnector.setIdleTimeout(serverProperties.getConnectionTimeout());
}
}
}
});
}
private void customizeMaxHttpHeaderSize(
@ -1149,8 +1192,8 @@ public class ServerProperties
return this.accesslog;
}
void customizeUndertow(ServerProperties serverProperties,
UndertowEmbeddedServletContainerFactory factory) {
void customizeUndertow(final ServerProperties serverProperties,
UndertowEmbeddedServletContainerFactory factory) {
if (this.bufferSize != null) {
factory.setBufferSize(this.bufferSize);
}
@ -1183,6 +1226,17 @@ public class ServerProperties
if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
}
customizeConnectionTimeout(serverProperties, factory);
}
private void customizeConnectionTimeout(final ServerProperties serverProperties,
final UndertowEmbeddedServletContainerFactory factory) {
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Builder builder) {
builder.setSocketOption(UndertowOptions.NO_REQUEST_TIMEOUT, serverProperties.getConnectionTimeout());
}
});
}
private void customizeMaxHttpHeaderSize(

View File

@ -166,6 +166,7 @@ content into your application; rather pick only the properties that you need.
server.server-header= # Value to use for the Server response header (no header is sent if empty)
server.servlet-path=/ # Path of the main dispatcher servlet.
server.use-forward-headers= # If X-Forwarded-* headers should be applied to the HttpRequest.
server.connectionTimeout=-1 # The number of milliseconds connectors will wait for another HTTP request before closing the connection.
server.session.cookie.comment= # Comment for the session cookie.
server.session.cookie.domain= # Domain for the session cookie.
server.session.cookie.http-only= # "HttpOnly" flag for the session cookie.