-
Notifications
You must be signed in to change notification settings - Fork 50
feat: Cache hooks per Flag Evaluation Type #1734
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
Open
chrfwow
wants to merge
10
commits into
open-feature:main
Choose a base branch
from
chrfwow:cached-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
df49024
add bench
chrfwow ea3990d
fix bench
chrfwow 1e7ee96
Merge branch 'refs/heads/main' into cache-baseline
chrfwow 9d49b91
merge master
chrfwow 628eda6
improve benchmark
chrfwow 2e9af46
improve benchmark
chrfwow b9fc776
add str hook for ctx
chrfwow 8a0770e
cache hooks
chrfwow 64a81fb
Merge branch 'refs/heads/main' into cached-hooks
chrfwow b5a443f
cache hooks
chrfwow 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,12 +8,13 @@ | |
| import dev.openfeature.sdk.internal.ObjectUtils; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Consumer; | ||
|
|
@@ -47,7 +48,7 @@ public class OpenFeatureClient implements Client { | |
| @Getter | ||
| private final String version; | ||
|
|
||
| private final ConcurrentLinkedQueue<Hook> clientHooks; | ||
| private final ConcurrentHashMap<FlagValueType, ConcurrentLinkedQueue<Hook>> clientHooks; | ||
| private final AtomicReference<EvaluationContext> evaluationContext = new AtomicReference<>(); | ||
|
|
||
| private final HookSupport hookSupport; | ||
|
|
@@ -69,7 +70,11 @@ public OpenFeatureClient(OpenFeatureAPI openFeatureAPI, String domain, String ve | |
| this.domain = domain; | ||
| this.version = version; | ||
| this.hookSupport = new HookSupport(); | ||
| this.clientHooks = new ConcurrentLinkedQueue<>(); | ||
| var values = FlagValueType.values(); | ||
| this.clientHooks = new ConcurrentHashMap<>(values.length); | ||
| for (FlagValueType value : values) { | ||
| this.clientHooks.put(value, new ConcurrentLinkedQueue<>()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -125,7 +130,16 @@ public void track(String trackingEventName, EvaluationContext context, TrackingE | |
| */ | ||
| @Override | ||
| public OpenFeatureClient addHooks(Hook... hooks) { | ||
| this.clientHooks.addAll(Arrays.asList(hooks)); | ||
| var types = FlagValueType.values(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] seems like this block of code repeats itself, should this maybe be a static helper function? |
||
| for (int i = 0; i < hooks.length; i++) { | ||
| var current = hooks[i]; | ||
| for (int j = 0; j < types.length; j++) { | ||
| var type = types[j]; | ||
| if (current.supportsFlagValueType(type)) { | ||
| this.clientHooks.get(type).add(current); | ||
| } | ||
| } | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -134,7 +148,11 @@ public OpenFeatureClient addHooks(Hook... hooks) { | |
| */ | ||
| @Override | ||
| public List<Hook> getHooks() { | ||
| return new ArrayList<>(this.clientHooks); | ||
| var allHooks = new HashSet<Hook>(); | ||
| for (var queue : this.clientHooks.values()) { | ||
| allHooks.addAll(queue); | ||
| } | ||
| return new ArrayList<>(allHooks); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -174,9 +192,12 @@ private <T> FlagEvaluationDetails<T> evaluateFlag( | |
| final var state = stateManager.getState(); | ||
|
|
||
| // Hooks are initialized as early as possible to enable the execution of error stages | ||
| var mergedHooks = ObjectUtils.merge( | ||
| provider.getProviderHooks(), flagOptions.getHooks(), clientHooks, openfeatureApi.getMutableHooks()); | ||
| hookSupport.setHooks(hookSupportData, mergedHooks, type); | ||
| hookSupport.setHooks( | ||
| hookSupportData, | ||
| provider.getProviderHooks(type), | ||
| flagOptions.getHooks(type), | ||
| clientHooks.get(type), | ||
| openfeatureApi.getHooks(type)); | ||
|
|
||
| var sharedHookContext = | ||
| new SharedHookContext(key, type, this.getMetadata(), provider.getMetadata(), defaultValue); | ||
|
|
||
This file contains hidden or 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.
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.
I am not sure, the order of hooks is important, i know that you are now using other methods, to retrieve the needed hooks, but can this implementation now be a problem, because we are not keeping the order of the hooks. Now we merge multiple hook source after each other.