Skip to content

Commit

Permalink
Apply server.tomcat.keep-alive-timeout to HTTP/2
Browse files Browse the repository at this point in the history
Closes gh-30267
  • Loading branch information
wilkinsona committed Mar 18, 2022
1 parent 2b75ea5 commit 1669062
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,9 @@
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.UpgradeProtocol;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.apache.coyote.http2.Http2Protocol;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute;
Expand Down Expand Up @@ -147,6 +149,11 @@ private void customizeProcessorCache(ConfigurableTomcatWebServerFactory factory,
private void customizeKeepAliveTimeout(ConfigurableTomcatWebServerFactory factory, Duration keepAliveTimeout) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
for (UpgradeProtocol upgradeProtocol : handler.findUpgradeProtocols()) {
if (upgradeProtocol instanceof Http2Protocol) {
((Http2Protocol) upgradeProtocol).setKeepAliveTimeout(keepAliveTimeout.toMillis());
}
}
if (handler instanceof AbstractProtocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setKeepAliveTimeout((int) keepAliveTimeout.toMillis());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ajp.AbstractAjpProtocol;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.apache.coyote.http2.Http2Protocol;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -106,6 +107,22 @@ void customKeepAliveTimeout() {
.isEqualTo(30));
}

@Test
void defaultKeepAliveTimeoutWithHttp2() {
bind("server.http2.enabled=true");
customizeAndRunServer((server) -> assertThat(
((Http2Protocol) server.getTomcat().getConnector().findUpgradeProtocols()[0]).getKeepAliveTimeout())
.isEqualTo(20000L));
}

@Test
void customKeepAliveTimeoutWithHttp2() {
bind("server.tomcat.keep-alive-timeout=30s", "server.http2.enabled=true");
customizeAndRunServer((server) -> assertThat(
((Http2Protocol) server.getTomcat().getConnector().findUpgradeProtocols()[0]).getKeepAliveTimeout())
.isEqualTo(30000L));
}

@Test
void customMaxKeepAliveRequests() {
bind("server.tomcat.max-keep-alive-requests=-1");
Expand Down Expand Up @@ -514,6 +531,7 @@ private TomcatWebServer customizeAndGetServer() {

private TomcatServletWebServerFactory customizeAndGetFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
factory.setHttp2(this.serverProperties.getHttp2());
this.customizer.customize(factory);
return factory;
}
Expand Down

0 comments on commit 1669062

Please sign in to comment.