Skip to content

Commit

Permalink
Add timeout to cached instances
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4a616e committed Aug 13, 2020
1 parent 3227594 commit f9618dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,10 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
</project>
4 changes: 3 additions & 1 deletion src/main/java/de/jangassen/lambda/LambdaProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
import java.util.concurrent.Executors;

public class LambdaProxy {
public static final int PORT = 3000;
public static final Duration INSTANCE_TIMEOUT = Duration.ofMinutes(5);

public static final String LAMBDA_PROXY = "lambdaProxy";
public static final String TEMPLATE_YAML = "template.yaml";
Expand Down Expand Up @@ -58,7 +60,7 @@ private LambdaProxyServlet getLambdaServlet(Path rootPath, Path templateFile) th
SamApiDescription apiDescription = new SamApiDescription(samTemplate, projectPath);
LambdaClassLoaderFactory classLoaderFactory = new LambdaClassLoaderFactory(new SamArtifactResolver(rootPath));
DefaultLambdaMethodInvoker lambdaMethodInvoker = new DefaultLambdaMethodInvoker();
MethodInvocationContextCache methodInvocationContextCache = new MethodInvocationContextCache(classLoaderFactory);
MethodInvocationContextCache methodInvocationContextCache = new MethodInvocationContextCache(classLoaderFactory, INSTANCE_TIMEOUT);

return new LambdaProxyServlet(lambdaMethodInvoker, methodInvocationContextCache, getCorsSettings(samTemplate), apiDescription.getApiMethods());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package de.jangassen.lambda.loader;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import de.jangassen.lambda.api.ApiResource;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import java.time.Duration;

public class MethodInvocationContextCache implements MethodInvocationContextProvider {
private final ClassLoaderFactory classLoaderFactory;
private final LoadingCache<ApiResource, MethodInvocationContext> cache;

private final Map<ApiResource, MethodInvocationContext> cache = new ConcurrentHashMap<>();

public MethodInvocationContextCache(ClassLoaderFactory classLoaderFactory) {
this.classLoaderFactory = classLoaderFactory;
public MethodInvocationContextCache(ClassLoaderFactory classLoaderFactory, Duration timeout) {
cache = CacheBuilder.newBuilder()
.expireAfterAccess(timeout)
.build(new CacheLoader<ApiResource, MethodInvocationContext>() {
@Override
public MethodInvocationContext load(@Nonnull ApiResource apiResource) {
return MethodInvocationContextBuilder
.start(classLoaderFactory)
.withApiResource(apiResource)
.build();
}
});
}

@Override
public MethodInvocationContext getMethodInvocationContext(ApiResource apiResource) {
return cache.computeIfAbsent(apiResource, key -> MethodInvocationContextBuilder.start(classLoaderFactory).withApiResource(key).build());
return cache.getUnchecked(apiResource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultLambdaMethodInvokerTest {
@Test
public void testSimpleInvocationInSameClassLoader() throws IOException, ReflectiveOperationException {
MethodInvocationContextCache methodInvocationContextCache = new MethodInvocationContextCache(h -> DefaultLambdaMethodInvokerTest.class.getClassLoader());
MethodInvocationContextCache methodInvocationContextCache = new MethodInvocationContextCache(h -> DefaultLambdaMethodInvokerTest.class.getClassLoader(), Duration.ofSeconds(10));
DefaultLambdaMethodInvoker defaultLambdaMethodInvoker = new DefaultLambdaMethodInvoker();

Request req = createTestRequest();
Expand Down

0 comments on commit f9618dd

Please sign in to comment.