This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Add read lock to get_flag_keys method #35
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,33 +1,23 @@ | ||
| from typing import Dict, Optional, TypeVar, Generic | ||
| from cachetools import LRUCache | ||
|
|
||
| from eppo_client.read_write_lock import ReadWriteLock | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class ConfigurationStore(Generic[T]): | ||
| def __init__(self, max_size: int): | ||
| self.__cache: LRUCache = LRUCache(maxsize=max_size) | ||
| def __init__(self): | ||
| self.__cache: Dict[str, T] = {} | ||
| self.__lock = ReadWriteLock() | ||
|
|
||
| def get_configuration(self, key: str) -> Optional[T]: | ||
| try: | ||
| self.__lock.acquire_read() | ||
| return self.__cache[key] | ||
| except KeyError: | ||
| return None # key does not exist | ||
| finally: | ||
| self.__lock.release_read() | ||
| with self.__lock.reader(): | ||
schmit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return self.__cache.get(key, None) | ||
|
|
||
| def set_configurations(self, configs: Dict[str, T]): | ||
| try: | ||
| self.__lock.acquire_write() | ||
| self.__cache.clear() | ||
| for key, config in configs.items(): | ||
| self.__cache[key] = config | ||
| finally: | ||
| self.__lock.release_write() | ||
| with self.__lock.writer(): | ||
| self.__cache = configs | ||
|
|
||
| def get_keys(self): | ||
| return list(self.__cache.keys()) | ||
| with self.__lock.reader(): | ||
| return list(self.__cache.keys()) | ||
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import threading | ||
| from contextlib import contextmanager | ||
|
|
||
| # Copied from: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch06s04.html | ||
| # Adapted from: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch06s04.html | ||
|
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. Show me the LLM vectors
Author
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. I tried that first, it went down a much more complicated path so I just added the contextmanager myself :) |
||
|
|
||
|
|
||
| class ReadWriteLock: | ||
|
|
@@ -14,21 +15,15 @@ def __init__(self): | |
| def acquire_read(self): | ||
| """Acquire a read lock. Blocks only if a thread has | ||
| acquired the write lock.""" | ||
| self._read_ready.acquire() | ||
| try: | ||
| with self._read_ready: | ||
| self._readers += 1 | ||
| finally: | ||
| self._read_ready.release() | ||
|
|
||
| def release_read(self): | ||
| """Release a read lock.""" | ||
| self._read_ready.acquire() | ||
| try: | ||
| with self._read_ready: | ||
| self._readers -= 1 | ||
| if not self._readers: | ||
| self._read_ready.notify_all() | ||
| finally: | ||
| self._read_ready.release() | ||
|
|
||
| def acquire_write(self): | ||
| """Acquire a write lock. Blocks until there are no | ||
|
|
@@ -40,3 +35,19 @@ def acquire_write(self): | |
| def release_write(self): | ||
| """Release a write lock.""" | ||
| self._read_ready.release() | ||
|
|
||
| @contextmanager | ||
| def reader(self): | ||
| try: | ||
| self.acquire_read() | ||
| yield | ||
| finally: | ||
| self.release_read() | ||
|
|
||
| @contextmanager | ||
| def writer(self): | ||
| try: | ||
| self.acquire_write() | ||
| yield | ||
| finally: | ||
| self.release_write() | ||
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "3.0.1" | ||
| __version__ = "3.0.2" |
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
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.
How is this cache used? If it's for assignments I think we do want some sort of eviction
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.
This cache is used to store the flag configuration. There is no cache for assignments for the Python SDK at the moment.