Skip to content

Commit 854cace

Browse files
authored
Merge pull request #659 from richarddd/deprecate-async-init
Deprecate asyncInit & account for other initialization types
2 parents cea033d + 4905e7a commit 854cace

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java

+14
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@
3838
* seconds has already been used up.
3939
*/
4040
public class AsyncInitializationWrapper extends InitializationWrapper {
41+
4142
private static final int DEFAULT_INIT_GRACE_TIME_MS = 150;
4243
private static final String INIT_GRACE_TIME_ENVIRONMENT_VARIABLE_NAME = "AWS_SERVERLESS_JAVA_CONTAINER_INIT_GRACE_TIME";
44+
private static final String INITIALIZATION_TYPE_ENVIRONMENT_VARIABLE_NAME = "AWS_LAMBDA_INITIALIZATION_TYPE";
45+
private static final String INITIALIZATION_TYPE_ON_DEMAND = "on-demand";
46+
private static final String INITIALIZATION_TYPE = System.getenv().getOrDefault(INITIALIZATION_TYPE_ENVIRONMENT_VARIABLE_NAME,INITIALIZATION_TYPE_ON_DEMAND);
47+
private static final boolean ASYNC_INIT_DISABLED = !INITIALIZATION_TYPE.equals(INITIALIZATION_TYPE_ON_DEMAND);
4348
private static final int INIT_GRACE_TIME_MS = Integer.parseInt(System.getenv().getOrDefault(
4449
INIT_GRACE_TIME_ENVIRONMENT_VARIABLE_NAME, Integer.toString(DEFAULT_INIT_GRACE_TIME_MS)));
4550
private static final int LAMBDA_MAX_INIT_TIME_MS = 10_000;
@@ -48,6 +53,7 @@ public class AsyncInitializationWrapper extends InitializationWrapper {
4853
private final long actualStartTime;
4954
private Logger log = LoggerFactory.getLogger(AsyncInitializationWrapper.class);
5055

56+
5157
/**
5258
* Creates a new instance of the async initializer.
5359
* @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() {
6773

6874
@Override
6975
public void start(LambdaContainerHandler handler) throws ContainerInitializationException {
76+
if(ASYNC_INIT_DISABLED){
77+
log.info("Async init disabled due to \"{}\" initialization", INITIALIZATION_TYPE);
78+
super.start(handler);
79+
return;
80+
}
7081
initializationLatch = new CountDownLatch(1);
7182
AsyncInitializer initializer = new AsyncInitializer(initializationLatch, handler);
7283
Thread initThread = new Thread(initializer);
@@ -96,6 +107,9 @@ public long getActualStartTimeMs() {
96107

97108
@Override
98109
public CountDownLatch getInitializationLatch() {
110+
if(ASYNC_INIT_DISABLED){
111+
return super.getInitializationLatch();
112+
}
99113
return initializationLatch;
100114
}
101115

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected void validate() throws ContainerInitializationException {
9292
* @return A populated builder
9393
*/
9494
public Builder defaultProxy() {
95-
initializationWrapper(new InitializationWrapper())
95+
initializationWrapper(new AsyncInitializationWrapper())
9696
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsProxyHttpServletRequestReader())
9797
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter())
9898
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsProxySecurityContextWriter())
@@ -108,7 +108,7 @@ public Builder defaultProxy() {
108108
* @return A populated builder
109109
*/
110110
public Builder defaultHttpApiV2Proxy() {
111-
initializationWrapper(new InitializationWrapper())
111+
initializationWrapper(new AsyncInitializationWrapper())
112112
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsHttpApiV2HttpServletRequestReader())
113113
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter(true))
114114
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsHttpApiV2SecurityContextWriter())
@@ -165,7 +165,7 @@ public Builder responseTypeClass(Class<ResponseType> responseType) {
165165
/**
166166
* Uses an async initializer with the given start time to calculate the 10 seconds timeout.
167167
*
168-
* @deprecated As of release 1.5 this method is deprecated in favor of the parameters-less one {@link ServletLambdaContainerHandlerBuilder#asyncInit()}.
168+
* @deprecated As of release 2.0.0 this method is deprecated. Initializer is always async if running in on-demand.
169169
* @param actualStartTime An epoch in milliseconds that should be used to calculate the 10 seconds timeout since the start of the application
170170
* @return A builder configured to use the async initializer
171171
*/
@@ -178,6 +178,7 @@ public Builder asyncInit(long actualStartTime) {
178178
/**
179179
* Uses a new {@link AsyncInitializationWrapper} with the no-parameter constructor that takes the actual JVM
180180
* start time
181+
* @deprecated As of release 2.0.0 this method is deprecated. Initializer is always async if running in on-demand.
181182
* @return A builder configured to use an async initializer
182183
*/
183184
public Builder asyncInit() {

aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/springslowapp/LambdaHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public LambdaHandler() throws ContainerInitializationException {
1818
long startTime = Instant.now().toEpochMilli();
1919
handler = new SpringProxyHandlerBuilder<AwsProxyRequest>()
2020
.defaultProxy()
21-
.asyncInit()
2221
.configurationClasses(SlowAppConfig.class)
2322
.buildAndInitialize();
2423
constructorTime = Instant.now().toEpochMilli() - startTime;

aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/slowapp/LambdaHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public LambdaHandler() {
2121
System.out.println("startCall: " + startTime);
2222
handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
2323
.defaultProxy()
24-
.asyncInit()
2524
.springBootApplication(SlowTestApplication.class)
2625
.buildAndInitialize();
2726
constructorTime = Instant.now().toEpochMilli() - startTime;

aws-serverless-springboot3-archetype/src/main/resources/archetype-resources/src/main/java/StreamLambdaHandler.java

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ public class StreamLambdaHandler implements RequestStreamHandler {
1818
static {
1919
try {
2020
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
21-
// For applications that take longer than 10 seconds to start, use the async builder:
22-
// handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
23-
// .defaultProxy()
24-
// .asyncInit()
25-
// .springBootApplication(Application.class)
26-
// .buildAndInitialize();
2721
} catch (ContainerInitializationException e) {
2822
// if we fail here. We re-throw the exception to force another cold start
2923
e.printStackTrace();

samples/springboot3/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot3/StreamLambdaHandler.java

-7
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ public class StreamLambdaHandler implements RequestStreamHandler {
2525
try {
2626
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
2727

28-
// For applications that take longer than 10 seconds to start, use the async builder:
29-
// handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
30-
// .defaultProxy()
31-
// .asyncInit()
32-
// .springBootApplication(Application.class)
33-
// .buildAndInitialize();
34-
3528
// we use the onStartup method of the handler to register our custom filter
3629
handler.onStartup(servletContext -> {
3730
FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class);

0 commit comments

Comments
 (0)