forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstSuccessfulStrategy.java
More file actions
59 lines (54 loc) · 2.27 KB
/
FirstSuccessfulStrategy.java
File metadata and controls
59 lines (54 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dev.openfeature.sdk.multiprovider;
import dev.openfeature.sdk.ErrorCode;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.FeatureProvider;
import dev.openfeature.sdk.ProviderEvaluation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* First Successful Strategy.
*
* <p>Similar to "First Match", except that errors from evaluated providers do not halt execution.
* Instead, it returns the first successful result from a provider. If no provider successfully
* responds, it returns a {@code GENERAL} error result that includes per-provider error details
* describing why each provider failed.
*/
@Slf4j
@NoArgsConstructor
public class FirstSuccessfulStrategy implements Strategy {
@Override
public <T> ProviderEvaluation<T> evaluate(
Map<String, FeatureProvider> providers,
String key,
T defaultValue,
EvaluationContext ctx,
Function<FeatureProvider, ProviderEvaluation<T>> providerFunction) {
List<ProviderError> collectedErrors = new ArrayList<>();
for (Map.Entry<String, FeatureProvider> entry : providers.entrySet()) {
String providerName = entry.getKey();
FeatureProvider provider = entry.getValue();
try {
ProviderEvaluation<T> res = providerFunction.apply(provider);
if (res.getErrorCode() == null) {
// First successful result (no error code)
return res;
}
// Record error-coded result
collectedErrors.add(ProviderError.fromResult(providerName, res.getErrorCode(), res.getErrorMessage()));
} catch (Exception e) {
// Record thrown exception
collectedErrors.add(ProviderError.fromException(providerName, e));
}
}
return MultiProviderEvaluation.<T>builder()
.errorMessage(
ProviderError.buildAggregateMessage("No provider successfully responded", collectedErrors))
.errorCode(ErrorCode.GENERAL)
.providerErrors(collectedErrors)
.build();
}
}