fix(appsync): bound the Lambda authorizer result cache (#4345)

* fix(appsync): bound the Lambda authorizer result cache

* fix: keep the authorizer cache cap under concurrent inserts
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-24 20:55:27 -04:00
1 parent 09dcbabe2b
commit 6ed4726f52
4 files changed
+147 -5

No files matched your search

@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker;
import io.github.hectorvent.floci.services.appsync.graphql.SchemaRegistry;
import io.github.hectorvent.floci.services.appsync.graphql.auth.LambdaAuthorizerCache;
import io.github.hectorvent.floci.services.appsync.model.*;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
@@ -57,6 +58,7 @@ public class AppSyncService {
private final ObjectMapper objectMapper;
private final Clock clock;
private final String baseUrl;
private final LambdaAuthorizerCache lambdaAuthorizerCache;
@Inject
public AppSyncService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver,
@@ -64,7 +66,8 @@ public class AppSyncService {
Instance<RequestContext> requestContextInstance, ObjectMapper objectMapper,
AccountAwareStorageBackend<SchemaCreationStatus> schemaStatusStore,
AccountAwareStorageBackend<String> schemaStore,
Clock clock) {
Clock clock,
LambdaAuthorizerCache lambdaAuthorizerCache) {
this.apiStore = storageFactory.create("appsync", "appsync-apis.json", new TypeReference<>() {});
this.schemaStore = schemaStore;
this.schemaStatusStore = schemaStatusStore;
@@ -84,6 +87,7 @@ public class AppSyncService {
this.objectMapper = objectMapper;
this.clock = clock;
this.baseUrl = trimTrailingSlash(config.effectiveBaseUrl());
this.lambdaAuthorizerCache = lambdaAuthorizerCache;
}
// ──────────────────────────── GraphQL API ────────────────────────────
@@ -228,6 +232,7 @@ public class AppSyncService {
deleteApiKeysForApi(apiId);
deleteChannelNamespacesForApi(apiId);
deleteDomainAssociationsForApi(apiId);
lambdaAuthorizerCache.evictApi(apiId);
LOG.infov("Deleted GraphQL API {0}", apiId);
}
@@ -4,6 +4,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import java.time.Clock;
import java.time.Instant;
import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@@ -12,6 +13,13 @@ public class LambdaAuthorizerCache {
static final int MAX_CACHEABLE_BYTES = 1_048_576;
/**
* Caps the number of distinct (API, token) entries held at once. Callers presenting many
* distinct authorization tokens would otherwise grow this cache without bound; once the cap
* is reached, the entry closest to expiry is evicted to make room for the new one.
*/
static final int MAX_ENTRIES = 1_000;
private final ConcurrentHashMap<String, Entry> cache = new ConcurrentHashMap<>();
private final Clock clock;
@@ -35,18 +43,43 @@ public class LambdaAuthorizerCache {
return Optional.of(entry.result);
}
public void put(String apiId, String token, LambdaAuthorizerResult result, int ttlSeconds) {
// Synchronized so the sweep, capacity check, eviction and insert happen as one step;
// otherwise concurrent puts all see room (or evict the same entry) and overshoot the cap.
public synchronized void put(String apiId, String token, LambdaAuthorizerResult result, int ttlSeconds) {
if (result == null || ttlSeconds <= 0 || result.responseSizeBytes() >= MAX_CACHEABLE_BYTES) {
return;
}
Instant expiresAt = clock.instant().plusSeconds(ttlSeconds);
cache.put(key(apiId, token), new Entry(result, expiresAt));
Instant now = clock.instant();
String key = key(apiId, token);
cache.entrySet().removeIf(entry -> !entry.getValue().expiresAt.isAfter(now));
if (!cache.containsKey(key) && cache.size() >= MAX_ENTRIES) {
evictClosestToExpiry();
}
cache.put(key, new Entry(result, now.plusSeconds(ttlSeconds)));
}
/**
* Drops every cached entry for an API, e.g. when the API itself is deleted.
*/
public void evictApi(String apiId) {
String prefix = apiId + "\0";
cache.keySet().removeIf(k -> k.startsWith(prefix));
}
private void evictClosestToExpiry() {
cache.entrySet().stream()
.min(Comparator.comparing(entry -> entry.getValue().expiresAt))
.ifPresent(entry -> cache.remove(entry.getKey(), entry.getValue()));
}
static String key(String apiId, String token) {
return apiId + "\0" + token;
}
int size() {
return cache.size();
}
private record Entry(LambdaAuthorizerResult result, Instant expiresAt) {
}
}