-
Notifications
You must be signed in to change notification settings - Fork 47
feat: Add async hook, plugin, and flag tracker #463
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9869242
feat: Add async hook, plugin, and flag tracker
jsonbailey 564abb6
refactor: Take the baseline flag value in AsyncFlagValueChangeListene…
jsonbailey 28d61bb
perf: Filter flag key in __call__ before scheduling the async re-eval
jsonbailey fb1dc02
refactor: Rename baseline constructor param to initial_value
jsonbailey 606860d
fix: Give the async flag tracker its own AsyncFlagTracker interface
jsonbailey 733132b
docs: Mark AsyncReadOnlyStore experimental
jsonbailey 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from typing import Any, Callable | ||
|
|
||
| from ldclient.context import Context | ||
| from ldclient.impl.aio.concurrency import AsyncCallbackScheduler, AsyncLock | ||
| from ldclient.impl.listeners import Listeners | ||
| from ldclient.interfaces import AsyncFlagTracker, FlagChange, FlagValueChange | ||
|
|
||
|
|
||
| class AsyncFlagValueChangeListener: | ||
| """Calls the user's listener when a specific flag's evaluated value changes for a specific context.""" | ||
|
|
||
| def __init__(self, key: str, context: Context, listener: Callable[[FlagValueChange], None], eval_fn: Callable, scheduler: AsyncCallbackScheduler, initial_value: Any): | ||
| self.__key = key | ||
| self.__context = context | ||
| self.__listener = listener | ||
| self.__eval_fn = eval_fn | ||
| self.__scheduler = scheduler | ||
|
|
||
| self.__lock = AsyncLock() | ||
| self.__value = initial_value | ||
|
|
||
| @classmethod | ||
| async def create(cls, key: str, context: Context, listener: Callable[[FlagValueChange], None], eval_fn: Callable, scheduler: AsyncCallbackScheduler) -> 'AsyncFlagValueChangeListener': | ||
| """Evaluates the flag once to capture the baseline value, then returns the listener.""" | ||
| initial_value = await eval_fn(key, context) | ||
| return cls(key, context, listener, eval_fn, scheduler, initial_value) | ||
|
|
||
| def __call__(self, flag_change: FlagChange): | ||
| if flag_change.key != self.__key: | ||
| return | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| self.__scheduler.call(self._on_flag_change) | ||
|
|
||
| async def _on_flag_change(self): | ||
| async with self.__lock: | ||
| new_value = await self.__eval_fn(self.__key, self.__context) | ||
| old_value, self.__value = self.__value, new_value | ||
|
|
||
| if new_value == old_value: | ||
| return | ||
|
|
||
| self.__listener(FlagValueChange(self.__key, old_value, new_value)) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class AsyncFlagTrackerImpl(AsyncFlagTracker): | ||
| def __init__(self, listeners: Listeners, eval_fn: Callable): | ||
| self.__listeners = listeners | ||
| self.__eval_fn = eval_fn | ||
| self.__scheduler = AsyncCallbackScheduler() | ||
|
|
||
| def add_listener(self, listener: Callable[[FlagChange], None]): | ||
| self.__listeners.add(listener) | ||
|
|
||
| def remove_listener(self, listener: Callable[[FlagChange], None]): | ||
| self.__listeners.remove(listener) | ||
|
|
||
| async def add_flag_value_change_listener(self, key: str, context: Context, fn: Callable[[FlagValueChange], None]) -> Callable[[FlagChange], None]: | ||
| listener = await AsyncFlagValueChangeListener.create(key, context, fn, self.__eval_fn, self.__scheduler) | ||
| self.add_listener(listener) | ||
|
|
||
|
jsonbailey marked this conversation as resolved.
|
||
| return listener | ||
|
cursor[bot] marked this conversation as resolved.
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.