Light, fluffy, and always free - The AWS Local Emulator alternative
fix: drain warm pool containers on server shutdown (#274)
Uğur Tafralıcaabf46b1c61
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java b/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.javaindex 46564a93bf..b5a180c068 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java@@ -41,6 +41,7 @@ public class WarmPool {private final ConcurrentHashMap<String, ArrayDeque<ContainerHandle>> pool = new ConcurrentHashMap<>();private final ScheduledExecutorService evictionScheduler = Executors.newSingleThreadScheduledExecutor(r -> { Thread t = new Thread(r, "warm-pool-evictor"); t.setDaemon(true); return t; });+ private Thread shutdownHook;@Injectpublic WarmPool(ContainerLauncher containerLauncher, EmulatorConfig config) {@@ -73,10 +74,20 @@ public class WarmPool {} else if (config.services().lambda().ephemeral()) {LOG.infov("Lambda containers running in ephemeral mode (destroyed after each invocation)");}++ shutdownHook = new Thread(this::drainAll, "warm-pool-shutdown-hook");+ Runtime.getRuntime().addShutdownHook(shutdownHook);}@PreDestroyvoid shutdown() {+ if (shutdownHook != null) {+ try {+ Runtime.getRuntime().removeShutdownHook(shutdownHook);+ } catch (IllegalStateException ignored) {+ // JVM already shutting down+ }+ }evictionScheduler.shutdownNow();drainAll();}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.javanew file mode 100644index 0000000000..bd74d930f4--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.java@@ -0,0 +1,59 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.extension.ExtendWith;+import org.mockito.Mock;+import org.mockito.junit.jupiter.MockitoExtension;++import java.lang.reflect.Field;++import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++@ExtendWith(MockitoExtension.class)+class WarmPoolTest {++ @Mock ContainerLauncher containerLauncher;+ @Mock EmulatorConfig config;++ private WarmPool buildPool() {+ EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);+ EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);+ when(config.services()).thenReturn(services);+ when(services.lambda()).thenReturn(lambda);+ when(lambda.ephemeral()).thenReturn(false);+ when(lambda.containerIdleTimeoutSeconds()).thenReturn(0);+ return new WarmPool(containerLauncher, config);+ }++ @Test+ void shutdownHookRegisteredAfterInit() throws Exception {+ WarmPool pool = buildPool();+ pool.init();++ Field hookField = WarmPool.class.getDeclaredField("shutdownHook");+ hookField.setAccessible(true);+ Thread hook = (Thread) hookField.get(pool);++ assertNotNull(hook);+ pool.shutdown();+ }++ @Test+ void shutdownHookDrainsEmptyPool() throws Exception {+ WarmPool pool = buildPool();+ pool.init();++ Field hookField = WarmPool.class.getDeclaredField("shutdownHook");+ hookField.setAccessible(true);+ Thread hook = (Thread) hookField.get(pool);++ // Running the hook on an empty pool must not throw+ hook.run();++ pool.shutdown();+ }+}