mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(lambda): initialize ESM pollers at startup and fix worker-pool exhaustion in RuntimeApiServer (#543)
This commit is contained in:
1 parent
9f231afc3b
commit
c8057785eb
7 files changed
+52
-31
No files matched your search
@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
|
||||
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
|
||||
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
|
||||
import io.github.hectorvent.floci.services.lambda.DynamoDbStreamsEventSourcePoller;
|
||||
import io.github.hectorvent.floci.services.lambda.KinesisEventSourcePoller;
|
||||
import io.github.hectorvent.floci.services.lambda.SqsEventSourcePoller;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
|
||||
import io.quarkus.runtime.Quarkus;
|
||||
@@ -36,6 +39,9 @@ public class EmulatorLifecycle {
|
||||
private final RdsContainerManager rdsContainerManager;
|
||||
private final RdsProxyManager rdsProxyManager;
|
||||
private final InitializationHooksRunner initializationHooksRunner;
|
||||
private final SqsEventSourcePoller sqsPoller;
|
||||
private final KinesisEventSourcePoller kinesisPoller;
|
||||
private final DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller;
|
||||
|
||||
@Inject
|
||||
public EmulatorLifecycle(StorageFactory storageFactory, ServiceRegistry serviceRegistry,
|
||||
@@ -44,7 +50,10 @@ public class EmulatorLifecycle {
|
||||
ElastiCacheProxyManager elastiCacheProxyManager,
|
||||
RdsContainerManager rdsContainerManager,
|
||||
RdsProxyManager rdsProxyManager,
|
||||
InitializationHooksRunner initializationHooksRunner) {
|
||||
InitializationHooksRunner initializationHooksRunner,
|
||||
SqsEventSourcePoller sqsPoller,
|
||||
KinesisEventSourcePoller kinesisPoller,
|
||||
DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller) {
|
||||
this.storageFactory = storageFactory;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.config = config;
|
||||
@@ -53,6 +62,9 @@ public class EmulatorLifecycle {
|
||||
this.rdsContainerManager = rdsContainerManager;
|
||||
this.rdsProxyManager = rdsProxyManager;
|
||||
this.initializationHooksRunner = initializationHooksRunner;
|
||||
this.sqsPoller = sqsPoller;
|
||||
this.kinesisPoller = kinesisPoller;
|
||||
this.dynamodbStreamsPoller = dynamodbStreamsPoller;
|
||||
}
|
||||
|
||||
void onStart(@Observes StartupEvent ignored) {
|
||||
@@ -63,6 +75,10 @@ public class EmulatorLifecycle {
|
||||
serviceRegistry.logEnabledServices();
|
||||
storageFactory.loadAll();
|
||||
|
||||
sqsPoller.startPersistedPollers();
|
||||
kinesisPoller.startPersistedPollers();
|
||||
dynamodbStreamsPoller.startPersistedPollers();
|
||||
|
||||
if (!initializationHooksRunner.hasHooks(InitializationHook.START)) {
|
||||
LOG.info("=== AWS Local Emulator Ready ===");
|
||||
}
|
||||
|
||||
+1
-3
@@ -12,7 +12,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.vertx.core.Vertx;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -59,8 +58,7 @@ public class DynamoDbStreamsEventSourcePoller {
|
||||
this.pollIntervalMs = config.services().lambda().pollIntervalMs();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
public void startPersistedPollers() {
|
||||
for (EventSourceMapping esm : esmStore.list()) {
|
||||
if (esm.isEnabled() && esm.getEventSourceArn().contains(":dynamodb:")) {
|
||||
startPolling(esm);
|
||||
|
||||
+1
-3
@@ -13,7 +13,6 @@ import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.vertx.core.Vertx;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -61,8 +60,7 @@ public class KinesisEventSourcePoller {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
public void startPersistedPollers() {
|
||||
List<EventSourceMapping> esms = esmStore.list();
|
||||
for (EventSourceMapping esm : esms) {
|
||||
if (esm.isEnabled() && esm.getEventSourceArn().contains(":kinesis:")) {
|
||||
|
||||
@@ -12,7 +12,6 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.vertx.core.Vertx;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -69,8 +68,7 @@ public class SqsEventSourcePoller {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
public void startPersistedPollers() {
|
||||
List<EventSourceMapping> esms = esmStore.list();
|
||||
for (EventSourceMapping esm : esms) {
|
||||
if (esm.isEnabled() && esm.getEventSourceArn().contains(":sqs:")) {
|
||||
|
||||
+13
-9
@@ -63,16 +63,20 @@ public class RuntimeApiServer {
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create());
|
||||
|
||||
// GET /runtime/invocation/next — long-poll for next invocation
|
||||
// GET /runtime/invocation/next — poll for next invocation, one request per worker thread.
|
||||
// Returns 204 when nothing arrives within 30 s so the runtime re-polls.
|
||||
// This matches the real AWS Runtime API contract and caps each handler to one
|
||||
// Vert.x worker thread for at most 30 seconds, preventing worker-pool exhaustion
|
||||
// when multiple warm containers are polling concurrently.
|
||||
router.get(NEXT_PATH).blockingHandler(ctx -> {
|
||||
try {
|
||||
PendingInvocation invocation = null;
|
||||
while (invocation == null && !stopped) {
|
||||
invocation = pendingQueue.poll(30, TimeUnit.SECONDS);
|
||||
if (invocation == SHUTDOWN_SENTINEL) {
|
||||
invocation = null;
|
||||
break;
|
||||
}
|
||||
if (stopped) {
|
||||
ctx.response().setStatusCode(204).end();
|
||||
return;
|
||||
}
|
||||
PendingInvocation invocation = pendingQueue.poll(30, TimeUnit.SECONDS);
|
||||
if (invocation == SHUTDOWN_SENTINEL) {
|
||||
invocation = null;
|
||||
}
|
||||
// If stop() ran after poll() returned a real invocation (narrow race before
|
||||
// inFlight.put), complete it here so the caller doesn't hang.
|
||||
@@ -97,7 +101,7 @@ public class RuntimeApiServer {
|
||||
: "{}");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
ctx.response().setStatusCode(500).end();
|
||||
ctx.response().setStatusCode(204).end();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user