Skip to content

Commit

Permalink
Merge pull request #659 from richarddd/deprecate-async-init
Browse files Browse the repository at this point in the history
Deprecate asyncInit & account for other initialization types
  • Loading branch information
deki authored Oct 25, 2023
2 parents cea033d + 4905e7a commit 854cace
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -96,6 +107,9 @@ public long getActualStartTimeMs() {

@Override
public CountDownLatch getInitializationLatch() {
if(ASYNC_INIT_DISABLED){
return super.getInitializationLatch();
}
return initializationLatch;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void validate() throws ContainerInitializationException {
* @return A populated builder
*/
public Builder defaultProxy() {
initializationWrapper(new InitializationWrapper())
initializationWrapper(new AsyncInitializationWrapper())
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsProxyHttpServletRequestReader())
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter())
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsProxySecurityContextWriter())
Expand All @@ -108,7 +108,7 @@ public Builder defaultProxy() {
* @return A populated builder
*/
public Builder defaultHttpApiV2Proxy() {
initializationWrapper(new InitializationWrapper())
initializationWrapper(new AsyncInitializationWrapper())
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsHttpApiV2HttpServletRequestReader())
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter(true))
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsHttpApiV2SecurityContextWriter())
Expand Down Expand Up @@ -165,7 +165,7 @@ public Builder responseTypeClass(Class<ResponseType> 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
*/
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public LambdaHandler() throws ContainerInitializationException {
long startTime = Instant.now().toEpochMilli();
handler = new SpringProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.asyncInit()
.configurationClasses(SlowAppConfig.class)
.buildAndInitialize();
constructorTime = Instant.now().toEpochMilli() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public LambdaHandler() {
System.out.println("startCall: " + startTime);
handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.asyncInit()
.springBootApplication(SlowTestApplication.class)
.buildAndInitialize();
constructorTime = Instant.now().toEpochMilli() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AwsProxyRequest>()
// .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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AwsProxyRequest>()
// .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);
Expand Down

0 comments on commit 854cace

Please sign in to comment.