From 9135e6e25a4c35c270e13da0082e91805071bc07 Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Wed, 22 May 2024 14:16:47 +0200 Subject: [PATCH] Make disabling async a valid option not just for testing (#700) For virtual threads you don't really need async enabled. This adds an option to disable it. --- .../micronaut/servlet/jetty/JettyFactory.java | 5 ++- .../jetty/JettyResponseEncoderSpec.groovy | 2 +- .../servlet/tomcat/TomcatFactory.java | 4 +-- .../servlet/undertow/UndertowFactory.java | 4 +-- .../servlet/http/ServletConfiguration.java | 9 +++++ .../engine/MicronautServletConfiguration.java | 34 ++++++++++++++++++- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/http-server-jetty/src/main/java/io/micronaut/servlet/jetty/JettyFactory.java b/http-server-jetty/src/main/java/io/micronaut/servlet/jetty/JettyFactory.java index c8e04b108..559bf2f06 100644 --- a/http-server-jetty/src/main/java/io/micronaut/servlet/jetty/JettyFactory.java +++ b/http-server-jetty/src/main/java/io/micronaut/servlet/jetty/JettyFactory.java @@ -117,10 +117,9 @@ protected Server jettyServer( final ServletHolder servletHolder = new ServletHolder(new DefaultMicronautServlet(applicationContext)); contextHandler.addServlet(servletHolder, configuration.getMapping()); - Boolean isAsync = applicationContext.getEnvironment() - .getProperty("micronaut.server.testing.async", Boolean.class, true); + boolean isAsync = configuration.isAsyncSupported(); if (Boolean.FALSE.equals(isAsync)) { - LOG.warn("Async support disabled for testing purposes."); + LOG.debug("Servlet async mode is disabled"); } servletHolder.setAsyncSupported(isAsync); diff --git a/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyResponseEncoderSpec.groovy b/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyResponseEncoderSpec.groovy index 3379a41d7..ecd95a74b 100644 --- a/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyResponseEncoderSpec.groovy +++ b/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyResponseEncoderSpec.groovy @@ -23,7 +23,7 @@ import spock.lang.Specification @MicronautTest @Property(name = "spec.name", value = SPEC_NAME) -@Property(name = "micronaut.server.testing.async", value = "false") +@Property(name = "micronaut.servlet.async-supported", value = "false") class JettyResponseEncoderSpec extends Specification { private static final String SPEC_NAME = "JettyResponseEncoderSpec" diff --git a/http-server-tomcat/src/main/java/io/micronaut/servlet/tomcat/TomcatFactory.java b/http-server-tomcat/src/main/java/io/micronaut/servlet/tomcat/TomcatFactory.java index 8658fda18..57c1aa99a 100644 --- a/http-server-tomcat/src/main/java/io/micronaut/servlet/tomcat/TomcatFactory.java +++ b/http-server-tomcat/src/main/java/io/micronaut/servlet/tomcat/TomcatFactory.java @@ -106,9 +106,9 @@ protected Tomcat tomcatServer(Connector connector, MicronautServletConfiguration new DefaultMicronautServlet(getApplicationContext()) ); - Boolean isAsync = getApplicationContext().getEnvironment().getProperty("micronaut.server.testing.async", Boolean.class, true); + boolean isAsync = configuration.isAsyncSupported(); if (Boolean.FALSE.equals(isAsync)) { - LOG.warn("Async support disabled for testing purposes."); + LOG.debug("Servlet async mode is disabled"); } servlet.setAsyncSupported(isAsync); servlet.addMapping(configuration.getMapping()); diff --git a/http-server-undertow/src/main/java/io/micronaut/servlet/undertow/UndertowFactory.java b/http-server-undertow/src/main/java/io/micronaut/servlet/undertow/UndertowFactory.java index 8df0dceb6..c2fee72a3 100644 --- a/http-server-undertow/src/main/java/io/micronaut/servlet/undertow/UndertowFactory.java +++ b/http-server-undertow/src/main/java/io/micronaut/servlet/undertow/UndertowFactory.java @@ -222,9 +222,9 @@ public void release() { } } ); - Boolean isAsync = getApplicationContext().getEnvironment().getProperty("micronaut.server.testing.async", Boolean.class, true); + boolean isAsync = servletConfiguration.isAsyncSupported(); if (Boolean.FALSE.equals(isAsync)) { - LOG.warn("Async support disabled for testing purposes."); + LOG.debug("Servlet async mode is disabled"); } servletInfo.setAsyncSupported(isAsync); servletInfo.addMapping(servletConfiguration.getMapping()); diff --git a/servlet-core/src/main/java/io/micronaut/servlet/http/ServletConfiguration.java b/servlet-core/src/main/java/io/micronaut/servlet/http/ServletConfiguration.java index 798e3ef4b..cc6ab9128 100644 --- a/servlet-core/src/main/java/io/micronaut/servlet/http/ServletConfiguration.java +++ b/servlet-core/src/main/java/io/micronaut/servlet/http/ServletConfiguration.java @@ -29,4 +29,13 @@ public interface ServletConfiguration { * @return True if it is. */ boolean isAsyncFileServingEnabled(); + + /** + * Whether to do request processing asynchronously by default (defaults to {@code true}). + * @return True whether async is enabled + * @since 4.8.0 + */ + default boolean isAsyncSupported() { + return true; + } } diff --git a/servlet-engine/src/main/java/io/micronaut/servlet/engine/MicronautServletConfiguration.java b/servlet-engine/src/main/java/io/micronaut/servlet/engine/MicronautServletConfiguration.java index 754704bc5..5e0a16360 100644 --- a/servlet-engine/src/main/java/io/micronaut/servlet/engine/MicronautServletConfiguration.java +++ b/servlet-engine/src/main/java/io/micronaut/servlet/engine/MicronautServletConfiguration.java @@ -17,10 +17,13 @@ import io.micronaut.context.annotation.ConfigurationInject; import io.micronaut.context.annotation.ConfigurationProperties; +import io.micronaut.context.annotation.Property; import io.micronaut.context.env.Environment; import io.micronaut.core.annotation.NonNull; +import io.micronaut.core.annotation.Nullable; import io.micronaut.core.bind.annotation.Bindable; import io.micronaut.core.naming.Named; +import io.micronaut.core.util.StringUtils; import io.micronaut.http.server.HttpServerConfiguration; import io.micronaut.servlet.http.ServletConfiguration; @@ -46,6 +49,8 @@ public class MicronautServletConfiguration implements Named, ServletConfiguratio private final String name; private boolean asyncFileServingEnabled = true; + private boolean asyncSupported = true; + /** * Default constructor. @@ -73,6 +78,33 @@ public MicronautServletConfiguration( } } + @Override + public boolean isAsyncSupported() { + return asyncSupported; + } + + /** + * Set whether async is supported or not. + * @param asyncSupported True if async is supported. + */ + public void setAsyncSupported(boolean asyncSupported) { + this.asyncSupported = asyncSupported; + } + + /** + * Legacy property to disable async for testing. + * + * @param asyncSupported Is async supported + * @deprecated Use {@link #setAsyncSupported(boolean)} instead + */ + @Deprecated(forRemoval = true, since = "4.8.0") + @Property(name = "micronaut.server.testing.async") + public void setTestAsyncSupported(@Nullable Boolean asyncSupported) { + if (asyncSupported != null) { + this.asyncSupported = asyncSupported; + } + } + /** * @return The servlet mapping. */ @@ -103,6 +135,6 @@ public void setAsyncFileServingEnabled(boolean enabled) { @Override public boolean isAsyncFileServingEnabled() { - return asyncFileServingEnabled; + return asyncSupported && asyncFileServingEnabled; } }