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

Added support for configuring Tomcat HTTP2 max stream size in Spring boot #14702

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class ServletConfig implements Serializable {
*/
private Boolean enabled;

/**
* Maximum concurrent streams.
* <p>For HTTP/2
* <p>Note that the default value for tomcat is 20. Highly recommended to change it to {@link Integer#MAX_VALUE}
* <p>If set to zero or a negative number, the actual value will be set to {@link Integer#MAX_VALUE}.
*/
private Integer maxConcurrentStreams;

/**
* The URL patterns that the servlet filter will be registered for.
* <p>The default value is '/*'.
Expand All @@ -48,6 +56,14 @@ public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}

public Integer getMaxConcurrentStreams() {
return maxConcurrentStreams;
}

public void setMaxConcurrentStreams(Integer maxConcurrentStreams) {
this.maxConcurrentStreams = maxConcurrentStreams;
}

public String[] getFilterUrlPatterns() {
return filterUrlPatterns;
}
Expand Down
7 changes: 6 additions & 1 deletion dubbo-spring-boot/dubbo-spring-boot-3-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>provided</scope>
oxsean marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
import org.apache.dubbo.rpc.protocol.tri.servlet.jakarta.TripleFilter;

import jakarta.servlet.Filter;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.UpgradeProtocol;
import org.apache.coyote.http2.Http2Protocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
Expand All @@ -39,7 +44,7 @@ public class DubboTriple3AutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Filter.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnProperty(prefix = PREFIX, name = "enabled")
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true")
public static class TripleServletConfiguration {

@Bean
Expand All @@ -54,5 +59,23 @@ public FilterRegistrationBean<TripleFilter> tripleProtocolFilter(
registrationBean.setOrder(order);
return registrationBean;
}

@Bean
@ConditionalOnClass(Http2Protocol.class)
@ConditionalOnProperty(prefix = PREFIX, name = "max-concurrent-streams")
public WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory> tripleTomcatHttp2Customizer(
@Value("${" + PREFIX + ".max-concurrent-streams}") int maxConcurrentStreams) {
return factory -> factory.addConnectorCustomizers(connector -> {
ProtocolHandler handler = connector.getProtocolHandler();
for (UpgradeProtocol upgradeProtocol : handler.findUpgradeProtocols()) {
if (upgradeProtocol instanceof Http2Protocol) {
Http2Protocol protocol = (Http2Protocol) upgradeProtocol;
int value = maxConcurrentStreams <= 0 ? Integer.MAX_VALUE : maxConcurrentStreams;
protocol.setMaxConcurrentStreams(value);
protocol.setMaxConcurrentStreamExecution(value);
}
}
});
}
}
}
5 changes: 5 additions & 0 deletions dubbo-spring-boot/dubbo-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- micrometer -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@

import javax.servlet.Filter;

import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.UpgradeProtocol;
import org.apache.coyote.http2.Http2Protocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
Expand All @@ -40,7 +45,7 @@ public class DubboTripleAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Filter.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnProperty(prefix = PREFIX, name = "enabled")
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true")
public static class TripleServletConfiguration {

@Bean
Expand All @@ -55,5 +60,23 @@ public FilterRegistrationBean<TripleFilter> tripleProtocolFilter(
registrationBean.setOrder(order);
return registrationBean;
}

@Bean
@ConditionalOnClass(Http2Protocol.class)
@ConditionalOnProperty(prefix = PREFIX, name = "max-concurrent-streams")
public WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory> tripleTomcatHttp2Customizer(
@Value("${" + PREFIX + ".max-concurrent-streams}") int maxConcurrentStreams) {
return factory -> factory.addConnectorCustomizers(connector -> {
ProtocolHandler handler = connector.getProtocolHandler();
for (UpgradeProtocol upgradeProtocol : handler.findUpgradeProtocols()) {
if (upgradeProtocol instanceof Http2Protocol) {
Http2Protocol protocol = (Http2Protocol) upgradeProtocol;
int value = maxConcurrentStreams <= 0 ? Integer.MAX_VALUE : maxConcurrentStreams;
protocol.setMaxConcurrentStreams(value);
protocol.setMaxConcurrentStreamExecution(value);
}
}
});
}
}
}
Loading