-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make provider interface "stateless"; SDK maintains provider sta…
…te (#1096) * Make provider interface "stateless", SDK maintains provider state Signed-off-by: christian.lutnik <[email protected]> Signed-off-by: Todd Baert <[email protected]> Co-authored-by: christian.lutnik <[email protected]> Co-authored-by: Todd Baert <[email protected]>
- Loading branch information
1 parent
dd8ba81
commit 1b1e527
Showing
28 changed files
with
1,070 additions
and
580 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dev.openfeature.sdk; | ||
|
||
@FunctionalInterface | ||
interface EventProviderListener { | ||
void onEmit(ProviderEvent event, ProviderEventDetails details); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/dev/openfeature/sdk/FeatureProviderStateManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import dev.openfeature.sdk.exceptions.OpenFeatureError; | ||
import lombok.Getter; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
class FeatureProviderStateManager implements EventProviderListener { | ||
private final FeatureProvider delegate; | ||
private final AtomicBoolean isInitialized = new AtomicBoolean(); | ||
@Getter | ||
private ProviderState state = ProviderState.NOT_READY; | ||
|
||
public FeatureProviderStateManager(FeatureProvider delegate) { | ||
this.delegate = delegate; | ||
if (delegate instanceof EventProvider) { | ||
((EventProvider) delegate).setEventProviderListener(this); | ||
} | ||
} | ||
|
||
public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
if (isInitialized.getAndSet(true)) { | ||
return; | ||
} | ||
try { | ||
delegate.initialize(evaluationContext); | ||
state = ProviderState.READY; | ||
} catch (OpenFeatureError openFeatureError) { | ||
if (ErrorCode.PROVIDER_FATAL.equals(openFeatureError.getErrorCode())) { | ||
state = ProviderState.FATAL; | ||
} else { | ||
state = ProviderState.ERROR; | ||
} | ||
isInitialized.set(false); | ||
throw openFeatureError; | ||
} catch (Exception e) { | ||
state = ProviderState.ERROR; | ||
isInitialized.set(false); | ||
throw e; | ||
} | ||
} | ||
|
||
public void shutdown() { | ||
delegate.shutdown(); | ||
state = ProviderState.NOT_READY; | ||
isInitialized.set(false); | ||
} | ||
|
||
@Override | ||
public void onEmit(ProviderEvent event, ProviderEventDetails details) { | ||
if (ProviderEvent.PROVIDER_ERROR.equals(event)) { | ||
if (details != null && details.getErrorCode() == ErrorCode.PROVIDER_FATAL) { | ||
state = ProviderState.FATAL; | ||
} else { | ||
state = ProviderState.ERROR; | ||
} | ||
} else if (ProviderEvent.PROVIDER_STALE.equals(event)) { | ||
state = ProviderState.STALE; | ||
} else if (ProviderEvent.PROVIDER_READY.equals(event)) { | ||
state = ProviderState.READY; | ||
} | ||
} | ||
|
||
FeatureProvider getProvider() { | ||
return delegate; | ||
} | ||
|
||
public boolean hasSameProvider(FeatureProvider featureProvider) { | ||
return this.delegate.equals(featureProvider); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.