Skip to content

Commit

Permalink
refactor: extract InitializableLambdaContainerHandler interface to be…
Browse files Browse the repository at this point in the history
… used by SpringDelegatingLambdaContainerHandler later on
  • Loading branch information
deki committed Jan 30, 2024
1 parent c26036f commit 58b296f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package com.amazonaws.serverless.proxy;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler;
import com.amazonaws.services.lambda.runtime.Context;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
Expand All @@ -26,10 +26,10 @@

/**
* An async implementation of the <code>InitializationWrapper</code> interface. This initializer calls the
* {@link LambdaContainerHandler#initialize()} in a separate thread. Then uses a latch to wait for the maximum Lambda
* {@link InitializableLambdaContainerHandler#initialize()} in a separate thread. Then uses a latch to wait for the maximum Lambda
* initialization time of 10 seconds, if the <code>initialize</code> method takes longer than 10 seconds to return, the
* {@link #start(LambdaContainerHandler)} returns control to the caller and lets the initialization thread continue in
* the background. The {@link LambdaContainerHandler#proxy(Object, Context)} automatically waits for the latch of the
* {@link #start(InitializableLambdaContainerHandler)} returns control to the caller and lets the initialization thread continue in
* the background. The {@link com.amazonaws.serverless.proxy.internal.LambdaContainerHandler#proxy(Object, Context)} automatically waits for the latch of the
* initializer to be released.
*
* The constructor of this class expects an epoch long. This is meant to be as close as possible to the time the Lambda
Expand Down Expand Up @@ -72,7 +72,7 @@ public AsyncInitializationWrapper() {
}

@Override
public void start(LambdaContainerHandler handler) throws ContainerInitializationException {
public void start(InitializableLambdaContainerHandler handler) throws ContainerInitializationException {
if(ASYNC_INIT_DISABLED){
log.info("Async init disabled due to \"{}\" initialization", INITIALIZATION_TYPE);
super.start(handler);
Expand Down Expand Up @@ -107,18 +107,18 @@ public long getActualStartTimeMs() {

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

private static class AsyncInitializer implements Runnable {
private LambdaContainerHandler handler;
private final InitializableLambdaContainerHandler handler;
private CountDownLatch initLatch;
private Logger log = LoggerFactory.getLogger(AsyncInitializationWrapper.class);

AsyncInitializer(CountDownLatch latch, LambdaContainerHandler h) {
AsyncInitializer(CountDownLatch latch, InitializableLambdaContainerHandler h) {
initLatch = latch;
handler = h;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
package com.amazonaws.serverless.proxy;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler;

import java.util.concurrent.CountDownLatch;

/**
* This class is in charge of initializing a {@link LambdaContainerHandler}.
* In most cases, this means calling the {@link LambdaContainerHandler#initialize()} method. Some implementations may
* This class is in charge of initializing a {@link InitializableLambdaContainerHandler}.
* In most cases, this means calling the {@link InitializableLambdaContainerHandler#initialize()} method. Some implementations may
* require additional initialization steps, in this case implementations should provide their own
* <code>InitializationWrapper</code>. This library includes an async implementation of this class
* {@link AsyncInitializationWrapper} for frameworks that are likely to take longer than 10 seconds to start.
Expand All @@ -31,7 +31,7 @@ public class InitializationWrapper {
* @param handler The container handler to be initializer
* @throws ContainerInitializationException If anything goes wrong during container initialization.
*/
public void start(LambdaContainerHandler handler) throws ContainerInitializationException {
public void start(InitializableLambdaContainerHandler handler) throws ContainerInitializationException {
handler.initialize();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.internal;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;

/**
* Interface to define initialization/ cold-start related methods.
* See also the documentation for
* <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/execution-environments.html">
* AWS Lambda Execution Environments</a>.
*/
public interface InitializableLambdaContainerHandler {

/**
* This method is called on the first (cold) invocation
*
* @throws ContainerInitializationException in case initialization fails
*/
void initialize() throws ContainerInitializationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
* @param <ContainerRequestType> The request type for the wrapped Java container
* @param <ContainerResponseType> The response or response writer type for the wrapped Java container
*/
public abstract class LambdaContainerHandler<RequestType, ResponseType, ContainerRequestType, ContainerResponseType> {
public abstract class LambdaContainerHandler<RequestType, ResponseType, ContainerRequestType, ContainerResponseType>
implements InitializableLambdaContainerHandler {

//-------------------------------------------------------------
// Constants
Expand Down

0 comments on commit 58b296f

Please sign in to comment.