Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration properties for configuring Tomcat's keep-alive timeout and max keep-alive requests #25815

Closed
wants to merge 1 commit into from

Conversation

parviz-93
Copy link
Contributor

and maxKeepAliveRequests
See #23539

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 27, 2021
@parviz-93 parviz-93 force-pushed the tomcat-23539 branch 2 times, most recently from e04d5a1 to 3784c68 Compare March 27, 2021 20:47
@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 28, 2021
@snicoll snicoll added this to the 2.5.x milestone Mar 28, 2021
@wilkinsona wilkinsona self-assigned this Apr 6, 2021
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.0-RC1 Apr 6, 2021
@wilkinsona wilkinsona changed the title Add configuration properties for configuring Tomcat's keepAliveTimeout Add configuration properties for configuring Tomcat's keep-alive timeout and max keep-alive requests Apr 6, 2021
@wilkinsona wilkinsona closed this in e731462 Apr 6, 2021
@wilkinsona
Copy link
Member

@parviz-93 Thank you very much for making your first contribution to Spring Boot. Your changes have now been merged along with a small polishing commit.

@parviz-93
Copy link
Contributor Author

@wilkinsona Thanks a lot for this experience. Thanks a lot for this experience. If there are still such tasks, I am ready to contribute to my favorite framework

@parviz-93 parviz-93 deleted the tomcat-23539 branch April 6, 2021 19:54
@aplatt
Copy link

aplatt commented Mar 17, 2022

It looks like the keep-alive timeout only worked for HTTP/1.1. The timeout for HTTP/2 was still 20s. This is how I got around it based on a @shunminli example:

@Configuration
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory>
{
    @Value("${server.http.keep-alive-timeout:}")
    private int httpKeepAliveTimeout;

    @Value("${server.http2.keep-alive-timeout:}")
    private long http2KeepAliveTimeout;

    private static final Logger log = LoggerFactory.getLogger(TomcatCustomizer.class);

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers(connector -> {
            AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) connector.getProtocolHandler();

            int originMaxKeepAliveRequests = protocol.getMaxKeepAliveRequests();
            protocol.setMaxKeepAliveRequests(-1);
            int originKeepAliveTimeout = protocol.getKeepAliveTimeout();
            protocol.setKeepAliveTimeout(httpKeepAliveTimeout);

            log.info(
                    "####################################################################################");
            log.info("#");
            log.info("# TomcatCustomizer HTTP");
            log.info("#");
            log.info("# origin maxKeepAliveRequests {}", originMaxKeepAliveRequests);
            log.info("# custom maxKeepAliveRequests {}", protocol.getMaxKeepAliveRequests());
            log.info("# origin keepalive timeout: {} ms", originKeepAliveTimeout);
            log.info("# keepalive timeout: {} ms", protocol.getKeepAliveTimeout());
            log.info("# connection timeout: {} ms", protocol.getConnectionTimeout());
            log.info("# max connections: {}", protocol.getMaxConnections());
            log.info("#");
            log.info(
                    "####################################################################################");

        });
        factory.addConnectorCustomizers(connector -> {
            Http2Protocol protocol = (Http2Protocol) connector.findUpgradeProtocols()[0];
            long originKeepAliveTimeout = protocol.getKeepAliveTimeout();
            protocol.setKeepAliveTimeout(http2KeepAliveTimeout);

            log.info(
                    "####################################################################################");
            log.info("#");
            log.info("# TomcatCustomizer HTTP/2");
            log.info("#");
            log.info("# origin keepalive timeout: {} ms", originKeepAliveTimeout);
            log.info("# keepalive timeout: {} ms", protocol.getKeepAliveTimeout());
            log.info("#");
            log.info(
                    "####################################################################################");

        });
    }
}

@bclozel
Copy link
Member

bclozel commented Mar 17, 2022

@aplatt it sounds like these properties should also cover the HTTP/2 protocol. Could you create a new issue for that?

@aplatt
Copy link

aplatt commented Mar 17, 2022

I created issue #30267

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants