From 6007d7efc1b505c1ff19e28a3795de6699c13fb8 Mon Sep 17 00:00:00 2001 From: Misagh Moayyed Date: Mon, 30 May 2016 10:48:28 -0700 Subject: [PATCH] Allow connection timeout to be configured via the environment Closes gh-6072 --- .../autoconfigure/web/ServerProperties.java | 62 +++++++++++++++++-- .../appendix-application-properties.adoc | 1 + 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index ee720c8fb03..396c473c60f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -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( diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index e0d915265c3..e13e386df10 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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.