I have an angular
web application that uses Spring boot with an embedded tomcat
server in the backend. I want to keep established http connections alive longer to improve the response time of subsequent http requests. With http/1.1
a browser is told to keep the http connection alive by adding Connection: Keep-Alive
and something like Keep-Alive: timeout=5, max=1000
to the response header
. However connection-specific header fields such as Connection and Keep-Alive are prohibited in HTTP/2. Because of that Chrome and Firefox ignore them in HTTP/2 responses. With HTTP/2, connection-specific metadata should be conveyed by other means.
I can't find anywhere what those 'other means' should be though. Nor can i find anywhere how to configure an embedded Tomcat 9 server to add Keep-alive
meta data to a HTTP/2
response. This is how tomcat is configured right now:
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) connector
.getProtocolHandler();
protocolHandler.setDisableUploadTimeout(false);
protocolHandler.setConnectionUploadTimeout(5000);
protocolHandler.setKeepAliveTimeout(4000);
protocolHandler.setMaxKeepAliveRequests(200);
protocolHandler.setUseKeepAliveResponseHeader(true);
}
});
}
But these settings aren't going to work if i want to use HTTP/2
. Any ideas?
If a server is running out of TCP connections it shouldn’t really keep and old one that is not being used alive rather than accept a new one, in general.
What do you mean by that? That http connections shouldn't be kept alive for too long? Right now I keep an http connection alive for 15 minutes. Would that cause the server to easily run out of tcp connections? I want to keep the connection alive for that long because that way the browser won't have to do a https handshake very often. I've also read that this approach will save me money because less data has to be transferred. – Herwick