Commit 43867323 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #6072 from Misagh Moayyed

* gh-6072:
  Polish “Allow connection timeout to be configured via the environment”
  Allow connection timeout to be configured via the environment
parents 9891eb54 94ba6bda
...@@ -39,6 +39,7 @@ import org.apache.catalina.valves.RemoteIpValve; ...@@ -39,6 +39,7 @@ import org.apache.catalina.valves.RemoteIpValve;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler; import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.AbstractHttp11Protocol; import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration;
...@@ -145,6 +146,13 @@ public class ServerProperties ...@@ -145,6 +146,13 @@ public class ServerProperties
*/ */
private int maxHttpPostSize = 0; // bytes private int maxHttpPostSize = 0; // bytes
/**
* Time in milliseconds that connectors will wait for another HTTP request before
* closing the connection. When not set, the connector's container-specific default
* will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.
*/
private Integer connectionTimeout;
private Session session = new Session(); private Session session = new Session();
@NestedConfigurationProperty @NestedConfigurationProperty
...@@ -366,6 +374,14 @@ public class ServerProperties ...@@ -366,6 +374,14 @@ public class ServerProperties
return (platform == null ? false : platform.isUsingForwardHeaders()); return (platform == null ? false : platform.isUsingForwardHeaders());
} }
public Integer getConnectionTimeout() {
return this.connectionTimeout;
}
public void setConnectionTimeout(Integer connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public ErrorProperties getError() { public ErrorProperties getError() {
return this.error; return this.error;
} }
...@@ -769,6 +785,21 @@ public class ServerProperties ...@@ -769,6 +785,21 @@ public class ServerProperties
if (getUriEncoding() != null) { if (getUriEncoding() != null) {
factory.setUriEncoding(getUriEncoding()); factory.setUriEncoding(getUriEncoding());
} }
if (serverProperties.getConnectionTimeout() != null) {
customizeConnectionTimeout(factory,
serverProperties.getConnectionTimeout());
}
}
private void customizeConnectionTimeout(
TomcatEmbeddedServletContainerFactory factory, int connectionTimeout) {
for (Connector connector : factory.getAdditionalTomcatConnectors()) {
if (connector.getProtocolHandler() instanceof AbstractProtocol) {
AbstractProtocol<?> handler = (AbstractProtocol<?>) connector
.getProtocolHandler();
handler.setConnectionTimeout(connectionTimeout);
}
}
} }
private void customizeBackgroundProcessorDelay( private void customizeBackgroundProcessorDelay(
...@@ -978,7 +1009,7 @@ public class ServerProperties ...@@ -978,7 +1009,7 @@ public class ServerProperties
this.selectors = selectors; this.selectors = selectors;
} }
void customizeJetty(ServerProperties serverProperties, void customizeJetty(final ServerProperties serverProperties,
JettyEmbeddedServletContainerFactory factory) { JettyEmbeddedServletContainerFactory factory) {
factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders()); factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders());
if (this.acceptors != null) { if (this.acceptors != null) {
...@@ -994,6 +1025,28 @@ public class ServerProperties ...@@ -994,6 +1025,28 @@ public class ServerProperties
if (serverProperties.getMaxHttpPostSize() > 0) { if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize()); customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
} }
if (serverProperties.getConnectionTimeout() != null) {
customizeConnectionTimeout(factory,
serverProperties.getConnectionTimeout());
}
}
private void customizeConnectionTimeout(
JettyEmbeddedServletContainerFactory factory,
final int connectionTimeout) {
factory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
for (org.eclipse.jetty.server.Connector connector : server
.getConnectors()) {
if (connector instanceof AbstractConnector) {
((AbstractConnector) connector)
.setIdleTimeout(connectionTimeout);
}
}
}
});
} }
private void customizeMaxHttpHeaderSize( private void customizeMaxHttpHeaderSize(
...@@ -1149,7 +1202,7 @@ public class ServerProperties ...@@ -1149,7 +1202,7 @@ public class ServerProperties
return this.accesslog; return this.accesslog;
} }
void customizeUndertow(ServerProperties serverProperties, void customizeUndertow(final ServerProperties serverProperties,
UndertowEmbeddedServletContainerFactory factory) { UndertowEmbeddedServletContainerFactory factory) {
if (this.bufferSize != null) { if (this.bufferSize != null) {
factory.setBufferSize(this.bufferSize); factory.setBufferSize(this.bufferSize);
...@@ -1183,6 +1236,23 @@ public class ServerProperties ...@@ -1183,6 +1236,23 @@ public class ServerProperties
if (serverProperties.getMaxHttpPostSize() > 0) { if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize()); customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
} }
if (serverProperties.getConnectionTimeout() != null) {
customizeConnectionTimeout(factory,
serverProperties.getConnectionTimeout());
}
}
private void customizeConnectionTimeout(
UndertowEmbeddedServletContainerFactory factory,
final int connectionTimeout) {
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Builder builder) {
builder.setSocketOption(UndertowOptions.NO_REQUEST_TIMEOUT,
connectionTimeout);
}
});
} }
private void customizeMaxHttpHeaderSize( private void customizeMaxHttpHeaderSize(
......
...@@ -149,6 +149,7 @@ content into your application; rather pick only the properties that you need. ...@@ -149,6 +149,7 @@ content into your application; rather pick only the properties that you need.
server.compression.excluded-user-agents= # List of user-agents to exclude from compression. server.compression.excluded-user-agents= # List of user-agents to exclude from compression.
server.compression.mime-types= # Comma-separated list of MIME types that should be compressed. For instance `text/html,text/css,application/json` server.compression.mime-types= # Comma-separated list of MIME types that should be compressed. For instance `text/html,text/css,application/json`
server.compression.min-response-size= # Minimum response size that is required for compression to be performed. For instance 2048 server.compression.min-response-size= # Minimum response size that is required for compression to be performed. For instance 2048
server.connection-timeout= # Time in milliseconds that connectors will wait for another HTTP request before closing the connection. When not set, the connector's container-specific default will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.
server.context-parameters.*= # Servlet context init parameters. For instance `server.context-parameters.a=alpha` server.context-parameters.*= # Servlet context init parameters. For instance `server.context-parameters.a=alpha`
server.context-path= # Context path of the application. server.context-path= # Context path of the application.
server.display-name=application # Display name of the application. server.display-name=application # Display name of the application.
...@@ -166,6 +167,7 @@ content into your application; rather pick only the properties that you need. ...@@ -166,6 +167,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.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.servlet-path=/ # Path of the main dispatcher servlet.
server.use-forward-headers= # If X-Forwarded-* headers should be applied to the HttpRequest. 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.comment= # Comment for the session cookie.
server.session.cookie.domain= # Domain for the session cookie. server.session.cookie.domain= # Domain for the session cookie.
server.session.cookie.http-only= # "HttpOnly" flag for the session cookie. server.session.cookie.http-only= # "HttpOnly" flag for the session cookie.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment