mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor(core): share lazy provider memoization (#7776)
Extract the repeated resolve-once provider getters into MemoizedSupplier. Preserve lazy runtime mapping, null and exception retry, concurrent first resolution, and calling-thread client-proxy behavior. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
dd777b7f62
commit
21b8e9346b
7 files changed
+134
-50
No files matched your search
@@ -57,10 +57,8 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
// Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist
|
||||
// (the AwsProtocolClaimFilter pattern). jakarta.inject.Provider is qualified inline because
|
||||
// this class also carries the JAX-RS @Provider annotation.
|
||||
private final jakarta.inject.Provider<EmulatorConfig> configProvider;
|
||||
private final jakarta.inject.Provider<ResolvedServiceCatalog> catalogProvider;
|
||||
private volatile EmulatorConfig config;
|
||||
private volatile ResolvedServiceCatalog catalog;
|
||||
private final MemoizedSupplier<EmulatorConfig> config;
|
||||
private final MemoizedSupplier<ResolvedServiceCatalog> catalog;
|
||||
|
||||
@Inject
|
||||
public AccountContextFilter(AccountResolver accountResolver,
|
||||
@@ -73,26 +71,16 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
this.regionResolver = regionResolver;
|
||||
this.requestContext = requestContext;
|
||||
this.sessionAccountLookup = sessionAccountLookup;
|
||||
this.configProvider = configProvider;
|
||||
this.catalogProvider = catalogProvider;
|
||||
this.config = new MemoizedSupplier<>(configProvider::get);
|
||||
this.catalog = new MemoizedSupplier<>(catalogProvider::get);
|
||||
}
|
||||
|
||||
private EmulatorConfig config() {
|
||||
EmulatorConfig resolved = config;
|
||||
if (resolved == null) {
|
||||
resolved = configProvider.get();
|
||||
config = resolved;
|
||||
}
|
||||
return resolved;
|
||||
return config.get();
|
||||
}
|
||||
|
||||
private ResolvedServiceCatalog catalog() {
|
||||
ResolvedServiceCatalog resolved = catalog;
|
||||
if (resolved == null) {
|
||||
resolved = catalogProvider.get();
|
||||
catalog = resolved;
|
||||
}
|
||||
return resolved;
|
||||
return catalog.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -46,24 +46,18 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
|
||||
private final ResolvedServiceCatalog catalog;
|
||||
// Lazily resolved: JAX-RS providers are instantiated before runtime config
|
||||
// mappings exist (same pattern as GlobalCorsFilter).
|
||||
private final jakarta.inject.Provider<EmulatorConfig> configProvider;
|
||||
private volatile EmulatorConfig config;
|
||||
private final MemoizedSupplier<EmulatorConfig> config;
|
||||
|
||||
@Inject
|
||||
public AwsProtocolClaimFilter(ProtocolClaimer claimer, ResolvedServiceCatalog catalog,
|
||||
jakarta.inject.Provider<EmulatorConfig> configProvider) {
|
||||
this.claimer = claimer;
|
||||
this.catalog = catalog;
|
||||
this.configProvider = configProvider;
|
||||
this.config = new MemoizedSupplier<>(configProvider::get);
|
||||
}
|
||||
|
||||
private EmulatorConfig config() {
|
||||
EmulatorConfig resolved = config;
|
||||
if (resolved == null) {
|
||||
resolved = configProvider.get();
|
||||
config = resolved;
|
||||
}
|
||||
return resolved;
|
||||
return config.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Lazily caches a non-null provider result. Failed or null resolutions are retried.
|
||||
* Concurrent first callers may resolve more than once, as with the filters' lazy getters.
|
||||
* For request-scoped beans, cache the client proxy, not a value read through that proxy.
|
||||
*/
|
||||
public final class MemoizedSupplier<T> implements Supplier<T> {
|
||||
|
||||
private final Supplier<? extends T> supplier;
|
||||
private volatile T value;
|
||||
|
||||
public MemoizedSupplier(Supplier<? extends T> supplier) {
|
||||
this.supplier = Objects.requireNonNull(supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
T resolved = value;
|
||||
if (resolved == null) {
|
||||
resolved = supplier.get();
|
||||
value = resolved;
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,8 @@ public class RegionResolver {
|
||||
@Inject
|
||||
Instance<RequestContext> requestContextInstance;
|
||||
|
||||
private volatile RequestContext requestContextProxy;
|
||||
private final MemoizedSupplier<RequestContext> requestContextProxy =
|
||||
new MemoizedSupplier<>(() -> requestContextInstance.get());
|
||||
|
||||
@Inject
|
||||
public RegionResolver(EmulatorConfig config) {
|
||||
@@ -261,12 +262,7 @@ public class RegionResolver {
|
||||
}
|
||||
|
||||
private RequestContext requestContext() {
|
||||
RequestContext proxy = requestContextProxy;
|
||||
if (proxy == null) {
|
||||
proxy = requestContextInstance.get();
|
||||
requestContextProxy = proxy;
|
||||
}
|
||||
return proxy;
|
||||
return requestContextProxy.get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -127,8 +127,7 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
|
||||
// Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist
|
||||
// (the AwsProtocolClaimFilter pattern).
|
||||
private final jakarta.inject.Provider<EmulatorConfig> configProvider;
|
||||
private volatile EmulatorConfig config;
|
||||
private final MemoizedSupplier<EmulatorConfig> config;
|
||||
private final IamService iamService;
|
||||
private final ResolvedServiceCatalog catalog;
|
||||
private final CurrentVertxRequest currentVertxRequest;
|
||||
@@ -140,19 +139,14 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
public SigV4HeaderSignatureFilter(jakarta.inject.Provider<EmulatorConfig> configProvider,
|
||||
IamService iamService, ResolvedServiceCatalog catalog,
|
||||
CurrentVertxRequest currentVertxRequest) {
|
||||
this.configProvider = configProvider;
|
||||
this.config = new MemoizedSupplier<>(configProvider::get);
|
||||
this.iamService = iamService;
|
||||
this.catalog = catalog;
|
||||
this.currentVertxRequest = currentVertxRequest;
|
||||
}
|
||||
|
||||
private EmulatorConfig config() {
|
||||
EmulatorConfig resolved = config;
|
||||
if (resolved == null) {
|
||||
resolved = configProvider.get();
|
||||
config = resolved;
|
||||
}
|
||||
return resolved;
|
||||
return config.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.core.storage;
|
||||
|
||||
import io.floci.aws.core.common.MemoizedSupplier;
|
||||
import io.floci.aws.core.common.RequestContext;
|
||||
import jakarta.enterprise.context.ContextNotActiveException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -42,7 +43,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
|
||||
private final StorageBackend<String, V> delegate;
|
||||
private final Instance<RequestContext> requestContextInstance;
|
||||
private volatile RequestContext requestContextProxy;
|
||||
private final MemoizedSupplier<RequestContext> requestContextProxy;
|
||||
private final String defaultAccountId;
|
||||
|
||||
// Accounts that own keys here, so a cross-account lookup does one get per account instead of a scan.
|
||||
@@ -57,6 +58,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
String defaultAccountId) {
|
||||
this.delegate = delegate;
|
||||
this.requestContextInstance = requestContextInstance;
|
||||
this.requestContextProxy = new MemoizedSupplier<>(() -> requestContextInstance.get());
|
||||
this.defaultAccountId = defaultAccountId;
|
||||
}
|
||||
|
||||
@@ -640,12 +642,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
}
|
||||
|
||||
private RequestContext requestContext() {
|
||||
RequestContext proxy = requestContextProxy;
|
||||
if (proxy == null) {
|
||||
proxy = requestContextInstance.get();
|
||||
requestContextProxy = proxy;
|
||||
}
|
||||
return proxy;
|
||||
return requestContextProxy.get();
|
||||
}
|
||||
|
||||
private String prefix() {
|
||||
|
||||
Reference in new issue
Block a user