io.smallrye.faulttolerance.internal.StrategyCache Maven / Gradle / Ivy
package io.smallrye.faulttolerance.internal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import io.smallrye.faulttolerance.SpecCompatibility;
import io.smallrye.faulttolerance.core.FaultToleranceStrategy;
import io.smallrye.faulttolerance.core.metrics.MetricsRecorder;
@Singleton
public class StrategyCache {
private final Map> strategies = new ConcurrentHashMap<>();
private final Map metricsRecorders = new ConcurrentHashMap<>();
private final Map fallbackMethods = new ConcurrentHashMap<>();
private final SpecCompatibility specCompatibility;
@Inject
public StrategyCache(SpecCompatibility specCompatibility) {
this.specCompatibility = specCompatibility;
}
@SuppressWarnings("unchecked")
public FaultToleranceStrategy getStrategy(InterceptionPoint point,
Supplier> producer) {
return (FaultToleranceStrategy) strategies.computeIfAbsent(point, ignored -> producer.get());
}
public MetricsRecorder getMetrics(InterceptionPoint point, Supplier producer) {
return metricsRecorders.computeIfAbsent(point, ignored -> producer.get());
}
public FallbackMethodCandidates getFallbackMethodCandidates(InterceptionPoint point, String fallbackMethodName) {
return fallbackMethods.computeIfAbsent(point, ignored -> FallbackMethodCandidates.create(
point, fallbackMethodName, specCompatibility.allowFallbackMethodExceptionParameter()));
}
}