Skip to content

Commit 26719a2

Browse files
committed
Merge branch '2.5.x' into 2.6.x
Closes gh-30321
2 parents 3c117ca + 1669062 commit 26719a2

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;
@@ -149,6 +151,11 @@ private void customizeProcessorCache(ConfigurableTomcatWebServerFactory factory,
149151
private void customizeKeepAliveTimeout(ConfigurableTomcatWebServerFactory factory, Duration keepAliveTimeout) {
150152
factory.addConnectorCustomizers((connector) -> {
151153
ProtocolHandler handler = connector.getProtocolHandler();
154+
for (UpgradeProtocol upgradeProtocol : handler.findUpgradeProtocols()) {
155+
if (upgradeProtocol instanceof Http2Protocol) {
156+
((Http2Protocol) upgradeProtocol).setKeepAliveTimeout(keepAliveTimeout.toMillis());
157+
}
158+
}
152159
if (handler instanceof AbstractProtocol) {
153160
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
154161
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");
@@ -522,6 +539,7 @@ private TomcatWebServer customizeAndGetServer() {
522539

523540
private TomcatServletWebServerFactory customizeAndGetFactory() {
524541
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
542+
factory.setHttp2(this.serverProperties.getHttp2());
525543
this.customizer.customize(factory);
526544
return factory;
527545
}

0 commit comments

Comments
 (0)