diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java index d1e97909e..b6b9b037d 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java @@ -38,8 +38,13 @@ * seconds has already been used up. */ public class AsyncInitializationWrapper extends InitializationWrapper { + private static final int DEFAULT_INIT_GRACE_TIME_MS = 150; private static final String INIT_GRACE_TIME_ENVIRONMENT_VARIABLE_NAME = "AWS_SERVERLESS_JAVA_CONTAINER_INIT_GRACE_TIME"; + private static final String INITIALIZATION_TYPE_ENVIRONMENT_VARIABLE_NAME = "AWS_LAMBDA_INITIALIZATION_TYPE"; + private static final String INITIALIZATION_TYPE_ON_DEMAND = "on-demand"; + private static final String INITIALIZATION_TYPE = System.getenv().getOrDefault(INITIALIZATION_TYPE_ENVIRONMENT_VARIABLE_NAME,INITIALIZATION_TYPE_ON_DEMAND); + private static final boolean ASYNC_INIT_DISABLED = !INITIALIZATION_TYPE.equals(INITIALIZATION_TYPE_ON_DEMAND); private static final int INIT_GRACE_TIME_MS = Integer.parseInt(System.getenv().getOrDefault( INIT_GRACE_TIME_ENVIRONMENT_VARIABLE_NAME, Integer.toString(DEFAULT_INIT_GRACE_TIME_MS))); private static final int LAMBDA_MAX_INIT_TIME_MS = 10_000; @@ -48,6 +53,7 @@ public class AsyncInitializationWrapper extends InitializationWrapper { private final long actualStartTime; private Logger log = LoggerFactory.getLogger(AsyncInitializationWrapper.class); + /** * Creates a new instance of the async initializer. * @param startTime The epoch ms start time of the Lambda function, this should be measured as close as possible to @@ -67,6 +73,11 @@ public AsyncInitializationWrapper() { @Override public void start(LambdaContainerHandler handler) throws ContainerInitializationException { + if(ASYNC_INIT_DISABLED){ + log.info("Async init disabled due to \"{}\" initialization", INITIALIZATION_TYPE); + super.start(handler); + return; + } initializationLatch = new CountDownLatch(1); AsyncInitializer initializer = new AsyncInitializer(initializationLatch, handler); Thread initThread = new Thread(initializer); @@ -96,6 +107,9 @@ public long getActualStartTimeMs() { @Override public CountDownLatch getInitializationLatch() { + if(ASYNC_INIT_DISABLED){ + return super.getInitializationLatch(); + } return initializationLatch; } diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java index b44ec7f26..adacd2e1f 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java @@ -92,7 +92,7 @@ protected void validate() throws ContainerInitializationException { * @return A populated builder */ public Builder defaultProxy() { - initializationWrapper(new InitializationWrapper()) + initializationWrapper(new AsyncInitializationWrapper()) .requestReader((RequestReader) new AwsProxyHttpServletRequestReader()) .responseWriter((ResponseWriter) new AwsProxyHttpServletResponseWriter()) .securityContextWriter((SecurityContextWriter) new AwsProxySecurityContextWriter()) @@ -108,7 +108,7 @@ public Builder defaultProxy() { * @return A populated builder */ public Builder defaultHttpApiV2Proxy() { - initializationWrapper(new InitializationWrapper()) + initializationWrapper(new AsyncInitializationWrapper()) .requestReader((RequestReader) new AwsHttpApiV2HttpServletRequestReader()) .responseWriter((ResponseWriter) new AwsProxyHttpServletResponseWriter(true)) .securityContextWriter((SecurityContextWriter) new AwsHttpApiV2SecurityContextWriter()) @@ -165,7 +165,7 @@ public Builder responseTypeClass(Class responseType) { /** * Uses an async initializer with the given start time to calculate the 10 seconds timeout. * - * @deprecated As of release 1.5 this method is deprecated in favor of the parameters-less one {@link ServletLambdaContainerHandlerBuilder#asyncInit()}. + * @deprecated As of release 2.0.0 this method is deprecated. Initializer is always async if running in on-demand. * @param actualStartTime An epoch in milliseconds that should be used to calculate the 10 seconds timeout since the start of the application * @return A builder configured to use the async initializer */ @@ -178,6 +178,7 @@ public Builder asyncInit(long actualStartTime) { /** * Uses a new {@link AsyncInitializationWrapper} with the no-parameter constructor that takes the actual JVM * start time + * @deprecated As of release 2.0.0 this method is deprecated. Initializer is always async if running in on-demand. * @return A builder configured to use an async initializer */ public Builder asyncInit() { diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/springslowapp/LambdaHandler.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/springslowapp/LambdaHandler.java index 935954d0f..0607c6f17 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/springslowapp/LambdaHandler.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/springslowapp/LambdaHandler.java @@ -18,7 +18,6 @@ public LambdaHandler() throws ContainerInitializationException { long startTime = Instant.now().toEpochMilli(); handler = new SpringProxyHandlerBuilder() .defaultProxy() - .asyncInit() .configurationClasses(SlowAppConfig.class) .buildAndInitialize(); constructorTime = Instant.now().toEpochMilli() - startTime; diff --git a/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/slowapp/LambdaHandler.java b/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/slowapp/LambdaHandler.java index ec6993a7d..22f75e7a9 100644 --- a/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/slowapp/LambdaHandler.java +++ b/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/slowapp/LambdaHandler.java @@ -21,7 +21,6 @@ public LambdaHandler() { System.out.println("startCall: " + startTime); handler = new SpringBootProxyHandlerBuilder() .defaultProxy() - .asyncInit() .springBootApplication(SlowTestApplication.class) .buildAndInitialize(); constructorTime = Instant.now().toEpochMilli() - startTime; diff --git a/aws-serverless-springboot3-archetype/src/main/resources/archetype-resources/src/main/java/StreamLambdaHandler.java b/aws-serverless-springboot3-archetype/src/main/resources/archetype-resources/src/main/java/StreamLambdaHandler.java index dca9650d3..e022540c1 100644 --- a/aws-serverless-springboot3-archetype/src/main/resources/archetype-resources/src/main/java/StreamLambdaHandler.java +++ b/aws-serverless-springboot3-archetype/src/main/resources/archetype-resources/src/main/java/StreamLambdaHandler.java @@ -18,12 +18,6 @@ public class StreamLambdaHandler implements RequestStreamHandler { static { try { handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class); - // For applications that take longer than 10 seconds to start, use the async builder: - // handler = new SpringBootProxyHandlerBuilder() - // .defaultProxy() - // .asyncInit() - // .springBootApplication(Application.class) - // .buildAndInitialize(); } catch (ContainerInitializationException e) { // if we fail here. We re-throw the exception to force another cold start e.printStackTrace(); diff --git a/samples/springboot3/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot3/StreamLambdaHandler.java b/samples/springboot3/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot3/StreamLambdaHandler.java index 7f329357a..a65c6f1ec 100644 --- a/samples/springboot3/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot3/StreamLambdaHandler.java +++ b/samples/springboot3/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot3/StreamLambdaHandler.java @@ -25,13 +25,6 @@ public class StreamLambdaHandler implements RequestStreamHandler { try { handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class); - // For applications that take longer than 10 seconds to start, use the async builder: - // handler = new SpringBootProxyHandlerBuilder() - // .defaultProxy() - // .asyncInit() - // .springBootApplication(Application.class) - // .buildAndInitialize(); - // we use the onStartup method of the handler to register our custom filter handler.onStartup(servletContext -> { FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class);