perf(cloudfront): skip the host lookup when no distribution exists (#7467)

* perf(cloudfront): skip the host lookup when no distribution exists

CloudFrontDistributionFilter scanned the distribution store on every
request of every service. CloudFrontService.hasDistributions() lets the
filter return at once while the store is empty, which is what findByHost
answered then anyway.

* perf(cloudfront): keep the distribution check free once one exists

AccountAwareStorageBackend.isEmpty() copies every key once the store holds
an entry, so a distribution in any account made each request pay that copy
before the same host scan as before. The filter only needs to know whether
any distribution exists, which the backing store answers without a copy.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-03 22:09:18 -04:00
1 parent 62bfd5e5ce
commit d7a3461fbc
4 files changed
+33 -2

No files matched your search

@@ -177,6 +177,11 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
return delegate.scan(k -> true);
}
/** Whether no account holds a value. */
public boolean isEmptyInAllAccounts() {
return delegate.isEmpty();
}
/** Removes matching values using their actual stored keys, including unprefixed legacy keys. */
public int deleteAllAccountsMatching(Predicate<V> valueFilter) {
int removed = 0;
@@ -55,7 +55,12 @@ public class CloudFrontDistributionFilter implements ContainerRequestFilter {
return;
}
Distribution dist = service.get().findByHost(host);
CloudFrontService cloudFront = service.get();
if (!cloudFront.hasDistributions()) {
return;
}
Distribution dist = cloudFront.findByHost(host);
if (dist == null) {
return;
}
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.acm.AcmCertificateReferenceProvider.CertificateReference;
@@ -119,7 +120,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
private static final Map<String, CachePolicy> MANAGED_CACHE_POLICIES = managedCachePolicies();
private static final String MANAGED_POLICY_ETAG = "E23ZP02F085DFQ";
private final StorageBackend<String, Distribution> distStore;
private final AccountAwareStorageBackend<Distribution> distStore;
private final StorageBackend<String, List<Invalidation>> invalidationStore;
private final StorageBackend<String, CachePolicy> cachePolicyStore;
private final StorageBackend<String, OriginRequestPolicy> orpStore;
@@ -560,6 +561,10 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
distStore.put(targetDistributionId, dist);
}
public boolean hasDistributions() {
return !distStore.isEmptyInAllAccounts();
}
/**
* Finds the distribution whose data-plane requests should be served for the given {@code Host}
* header. A distribution matches when the host equals its assigned CloudFront domain name
@@ -52,6 +52,22 @@ class AccountAwareStorageBackendTest {
assertTrue(second.isEmpty());
}
@Test
void isEmptyInAllAccountsSeesEveryAccount() {
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
AccountAwareStorageBackend<String> first =
new AccountAwareStorageBackend<>(raw, null, "111111111111");
AccountAwareStorageBackend<String> second =
new AccountAwareStorageBackend<>(raw, null, "222222222222");
assertTrue(second.isEmptyInAllAccounts());
first.put("resource", "value");
assertTrue(second.isEmpty());
assertFalse(second.isEmptyInAllAccounts());
}
@Test
void putAllPrefixesEveryEntryWithTheRequestScope() {
InMemoryStorage<String, String> raw = new InMemoryStorage<>();