Skip to content

Commit 1669062

Browse files
committed
Apply server.tomcat.keep-alive-timeout to HTTP/2
Closes gh-30267
1 parent 2b75ea5 commit 1669062

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,9 @@
2626
import org.apache.catalina.valves.RemoteIpValve;
2727
import org.apache.coyote.AbstractProtocol;
2828
import org.apache.coyote.ProtocolHandler;
29+
import org.apache.coyote.UpgradeProtocol;
2930
import org.apache.coyote.http11.AbstractHttp11Protocol;
31+
import org.apache.coyote.http2.Http2Protocol;
3032

3133
import org.springframework.boot.autoconfigure.web.ErrorProperties;
3234
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute;
@@ -147,6 +149,11 @@ private void customizeProcessorCache(ConfigurableTomcatWebServerFactory factory,
147149
private void customizeKeepAliveTimeout(ConfigurableTomcatWebServerFactory factory, Duration keepAliveTimeout) {
148150
factory.addConnectorCustomizers((connector) -> {
149151
ProtocolHandler handler = connector.getProtocolHandler();
152+
for (UpgradeProtocol upgradeProtocol : handler.findUpgradeProtocols()) {
153+
if (upgradeProtocol instanceof Http2Protocol) {
154+
((Http2Protocol) upgradeProtocol).setKeepAliveTimeout(keepAliveTimeout.toMillis());
155+
}
156+
}
150157
if (handler instanceof AbstractProtocol) {
151158
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
152159
protocol.setKeepAliveTimeout((int) keepAliveTimeout.toMillis());

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.apache.coyote.AbstractProtocol;
2929
import org.apache.coyote.ajp.AbstractAjpProtocol;
3030
import org.apache.coyote.http11.AbstractHttp11Protocol;
31+
import org.apache.coyote.http2.Http2Protocol;
3132
import org.junit.jupiter.api.BeforeEach;
3233
import org.junit.jupiter.api.Test;
3334

@@ -106,6 +107,22 @@ void customKeepAliveTimeout() {
106107
.isEqualTo(30));
107108
}
108109

110+
@Test
111+
void defaultKeepAliveTimeoutWithHttp2() {
112+
bind("server.http2.enabled=true");
113+
customizeAndRunServer((server) -> assertThat(
114+
((Http2Protocol) server.getTomcat().getConnector().findUpgradeProtocols()[0]).getKeepAliveTimeout())
115+
.isEqualTo(20000L));
116+
}
117+
118+
@Test
119+
void customKeepAliveTimeoutWithHttp2() {
120+
bind("server.tomcat.keep-alive-timeout=30s", "server.http2.enabled=true");
121+
customizeAndRunServer((server) -> assertThat(
122+
((Http2Protocol) server.getTomcat().getConnector().findUpgradeProtocols()[0]).getKeepAliveTimeout())
123+
.isEqualTo(30000L));
124+
}
125+
109126
@Test
110127
void customMaxKeepAliveRequests() {
111128
bind("server.tomcat.max-keep-alive-requests=-1");
@@ -514,6 +531,7 @@ private TomcatWebServer customizeAndGetServer() {
514531

515532
private TomcatServletWebServerFactory customizeAndGetFactory() {
516533
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
534+
factory.setHttp2(this.serverProperties.getHttp2());
517535
this.customizer.customize(factory);
518536
return factory;
519537
}

0 commit comments

Comments
 (0)