-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add initialize and shutdown behavior #456
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5125e03
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 1c3ea04
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 56b4f02
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 11fc987
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz f490efe
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 2581635
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz c4631e3
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz b324e10
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz ce84058
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 8ca85c8
chore(deps): update github/codeql-action digest to 9d2dd7c (#457)
renovate[bot] a3b6b1f
Merge branch 'main' into java-sdk-449
lopitz 4d50752
Merge branch 'main' into java-sdk-449
lopitz a313750
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 9475cac
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz d0fa55d
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz 75491eb
Merge branch 'main' into java-sdk-449
lopitz 076d970
Merge branch 'main' into java-sdk-449
lopitz a650655
java-sdk-449 Implement Initialize/Shutdown on provider registration
lopitz c09b814
Merge branch 'main' into java-sdk-449
lopitz 4437b89
- addresses review comments
lopitz 24b7e98
Merge branch 'main' into java-sdk-449
toddbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
156 changes: 156 additions & 0 deletions
156
src/main/java/dev/openfeature/sdk/ProviderRepository.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,156 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
|
||
@Slf4j | ||
class ProviderRepository { | ||
|
||
private final Map<String, FeatureProvider> providers = new ConcurrentHashMap<>(); | ||
private final ExecutorService taskExecutor = Executors.newCachedThreadPool(); | ||
private final Map<String, FeatureProvider> initializingNamedProviders = new ConcurrentHashMap<>(); | ||
private final AtomicReference<FeatureProvider> defaultProvider = new AtomicReference<>(new NoOpProvider()); | ||
private FeatureProvider initializingDefaultProvider; | ||
|
||
/** | ||
* Return the default provider. | ||
*/ | ||
public FeatureProvider getProvider() { | ||
return defaultProvider.get(); | ||
} | ||
|
||
/** | ||
* Fetch a provider for a named client. If not found, return the default. | ||
* | ||
* @param name The client name to look for. | ||
* @return A named {@link FeatureProvider} | ||
*/ | ||
public FeatureProvider getProvider(String name) { | ||
return Optional.ofNullable(name).map(this.providers::get).orElse(this.defaultProvider.get()); | ||
} | ||
|
||
/** | ||
* Set the default provider. | ||
*/ | ||
public void setProvider(FeatureProvider provider) { | ||
if (provider == null) { | ||
throw new IllegalArgumentException("Provider cannot be null"); | ||
} | ||
initializeProvider(provider); | ||
} | ||
|
||
/** | ||
* Add a provider for a named client. | ||
* | ||
* @param clientName The name of the client. | ||
* @param provider The provider to set. | ||
*/ | ||
public void setProvider(String clientName, FeatureProvider provider) { | ||
if (provider == null) { | ||
throw new IllegalArgumentException("Provider cannot be null"); | ||
} | ||
if (clientName == null) { | ||
throw new IllegalArgumentException("clientName cannot be null"); | ||
} | ||
initializeProvider(clientName, provider); | ||
} | ||
|
||
private void initializeProvider(FeatureProvider provider) { | ||
initializingDefaultProvider = provider; | ||
initializeProvider(provider, this::updateDefaultProviderAfterInitialization); | ||
} | ||
|
||
private void initializeProvider(String clientName, FeatureProvider provider) { | ||
initializingNamedProviders.put(clientName, provider); | ||
initializeProvider(provider, newProvider -> updateProviderAfterInit(clientName, newProvider)); | ||
} | ||
|
||
private void initializeProvider(FeatureProvider provider, Consumer<FeatureProvider> afterInitialization) { | ||
taskExecutor.submit(() -> { | ||
try { | ||
if (!isProviderRegistered(provider)) { | ||
provider.initialize(); | ||
} | ||
afterInitialization.accept(provider); | ||
} catch (Exception e) { | ||
log.error("Exception when initializing feature provider {}", provider.getClass().getName(), e); | ||
} | ||
}); | ||
} | ||
|
||
private void updateProviderAfterInit(String clientName, FeatureProvider newProvider) { | ||
Optional | ||
.ofNullable(initializingNamedProviders.get(clientName)) | ||
.filter(initializingProvider -> initializingProvider.equals(newProvider)) | ||
.ifPresent(provider -> updateNamedProviderAfterInitialization(clientName, provider)); | ||
} | ||
|
||
private void updateDefaultProviderAfterInitialization(FeatureProvider initializedProvider) { | ||
Optional | ||
.ofNullable(this.initializingDefaultProvider) | ||
.filter(initializingProvider -> initializingProvider.equals(initializedProvider)) | ||
.ifPresent(this::replaceDefaultProvider); | ||
} | ||
|
||
private void replaceDefaultProvider(FeatureProvider provider) { | ||
FeatureProvider oldProvider = this.defaultProvider.getAndSet(provider); | ||
if (isOldProviderNotBoundByName(oldProvider)) { | ||
shutdownProvider(oldProvider); | ||
} | ||
} | ||
|
||
private boolean isOldProviderNotBoundByName(FeatureProvider oldProvider) { | ||
return !this.providers.containsValue(oldProvider); | ||
} | ||
|
||
private void updateNamedProviderAfterInitialization(String clientName, FeatureProvider initializedProvider) { | ||
Optional | ||
.ofNullable(this.initializingNamedProviders.get(clientName)) | ||
Kavindu-Dodan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.filter(initializingProvider -> initializingProvider.equals(initializedProvider)) | ||
.ifPresent(provider -> replaceNamedProviderAndShutdownOldOne(clientName, provider)); | ||
} | ||
|
||
private void replaceNamedProviderAndShutdownOldOne(String clientName, FeatureProvider provider) { | ||
FeatureProvider oldProvider = this.providers.put(clientName, provider); | ||
this.initializingNamedProviders.remove(clientName, provider); | ||
if (!isProviderRegistered(oldProvider)) { | ||
shutdownProvider(oldProvider); | ||
} | ||
} | ||
|
||
private boolean isProviderRegistered(FeatureProvider oldProvider) { | ||
return this.providers.containsValue(oldProvider) || this.defaultProvider.get().equals(oldProvider); | ||
} | ||
|
||
private void shutdownProvider(FeatureProvider provider) { | ||
taskExecutor.submit(() -> { | ||
try { | ||
provider.shutdown(); | ||
} catch (Exception e) { | ||
log.error("Exception when shutting down feature provider {}", provider.getClass().getName(), e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Shutdowns this repository which includes shutting down all FeatureProviders that are registered, | ||
* including the default feature provider. | ||
*/ | ||
public void shutdown() { | ||
Stream | ||
.concat(Stream.of(this.defaultProvider.get()), this.providers.values().stream()) | ||
.distinct() | ||
.forEach(this::shutdownProvider); | ||
setProvider(new NoOpProvider()); | ||
this.providers.clear(); | ||
taskExecutor.shutdown(); | ||
} | ||
} |
12 changes: 6 additions & 6 deletions
12
src/test/java/dev/openfeature/sdk/ClientProviderMappingTest.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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine with me since it's package private.