-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathFirstSuccessfulStrategy.java
More file actions
48 lines (44 loc) · 1.66 KB
/
FirstSuccessfulStrategy.java
File metadata and controls
48 lines (44 loc) · 1.66 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
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.List;
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.
*/
@Slf4j
@NoArgsConstructor
public class FirstSuccessfulStrategy implements Strategy {
@Override
public <T> ProviderEvaluation<T> evaluate(
List<FeatureProvider> providers,
String key,
T defaultValue,
EvaluationContext ctx,
Function<FeatureProvider, ProviderEvaluation<T>> providerFunction) {
for (FeatureProvider provider : providers) {
try {
ProviderEvaluation<T> res = providerFunction.apply(provider);
if (res.getErrorCode() == null) {
// First successful result (no error code)
return res;
}
} catch (Exception ignored) {
// swallow and continue; errors from individual providers
// are not fatal for this strategy
}
}
return ProviderEvaluation.<T>builder()
.errorMessage("No provider successfully responded")
.errorCode(ErrorCode.GENERAL)
.build();
}
}