perf(apigateway): skip the key copy for a host that is no custom domain (#7727)

Every request of every service looked its Host up among the custom
domains. The lookup copied all stored domain keys and cut a substring per
key, and almost no host is a custom domain. 100 domains added about 19 KB
to each request.

A walk over the stored keys now answers a miss first. It copies nothing.
A hit still takes the old path, so the owner, region and conflict rules
stay the same.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-07 09:08:28 -04:00
1 parent 6974d8c096
commit 9a88ebf830
2 files changed
+25 -6

No files matched your search

@@ -3284,8 +3284,13 @@ public class ApiGatewayService implements ResourceProvider {
* @return the domain's routing scope if found, or null if no domain matches
*/
public CustomDomainRoute findDomainByName(String domainName) {
String keySuffix = "::" + domainName;
// Every request of every service gets here, and scanAllAccountEntries copies every key.
if (domainStore.scanRaw(rawKey -> rawKey.endsWith(keySuffix)).isEmpty()) {
return null;
}
List<AccountAwareStorageBackend.AccountEntry<CustomDomain>> matches =
domainStore.scanAllAccountEntries(key -> key.endsWith("::" + domainName));
domainStore.scanAllAccountEntries(key -> key.endsWith(keySuffix));
if (matches.size() > 1) {
throw new AwsException("ConflictException",
"Custom domain '" + domainName + "' is ambiguous across accounts or regions", 409);
@@ -22,6 +22,9 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class ApiGatewayCustomDomainOwnerServiceTest {
@@ -41,7 +44,7 @@ class ApiGatewayCustomDomainOwnerServiceTest {
StorageFactory storageFactory = mock(StorageFactory.class);
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation ->
stores.computeIfAbsent(invocation.getArgument(1, String.class),
ignored -> AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT)));
ignored -> spy(AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT))));
service = new ApiGatewayService(storageFactory,
mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), mock(TlsCertificateManager.class),
new RegionResolver(REGION, DEFAULT_ACCOUNT));
@@ -112,13 +115,24 @@ class ApiGatewayCustomDomainOwnerServiceTest {
assertNull(service.findDomainByRegionalHostname(DOMAIN));
}
@SuppressWarnings("unchecked")
@Test
void anUnknownHostDoesNotCopyTheStoredKeys() {
putDomain(OWNER, REGION);
assertNull(service.findDomainByName("other.example.test"));
verify(domains(), never()).scanAllAccountEntries(any());
}
private void putDomain(String account, String region) {
AccountAwareStorageBackend<CustomDomain> domains =
(AccountAwareStorageBackend<CustomDomain>) stores.get("apigateway-domains.json");
CustomDomain domain = new CustomDomain();
domain.setDomainName(DOMAIN);
domains.putForAccount(account, region + "::" + DOMAIN, domain);
domains().putForAccount(account, region + "::" + DOMAIN, domain);
}
@SuppressWarnings("unchecked")
private AccountAwareStorageBackend<CustomDomain> domains() {
return (AccountAwareStorageBackend<CustomDomain>) stores.get("apigateway-domains.json");
}
@SuppressWarnings("unchecked")