-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for data store status monitoring (#252)
The client instance will now provide access to a `data_store_status_provider`. This provider allows developers to retrieve the data store status of the SDK on demand, or asynchronously by registering listeners.
- Loading branch information
Showing
19 changed files
with
621 additions
and
20 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
Empty file.
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,56 @@ | ||
from __future__ import annotations | ||
from typing import Callable, TYPE_CHECKING | ||
from copy import copy | ||
|
||
from ldclient.interfaces import DataStoreStatusProvider, DataStoreStatus, DataStoreUpdateSink | ||
from ldclient.impl.listeners import Listeners | ||
from ldclient.impl.rwlock import ReadWriteLock | ||
|
||
if TYPE_CHECKING: | ||
from ldclient.client import _FeatureStoreClientWrapper | ||
|
||
|
||
class DataStoreUpdateSinkImpl(DataStoreUpdateSink): | ||
def __init__(self, listeners: Listeners): | ||
self.__listeners = listeners | ||
|
||
self.__lock = ReadWriteLock() | ||
self.__status = DataStoreStatus(True, False) | ||
|
||
@property | ||
def listeners(self) -> Listeners: | ||
return self.__listeners | ||
|
||
def status(self) -> DataStoreStatus: | ||
self.__lock.rlock() | ||
status = copy(self.__status) | ||
self.__lock.runlock() | ||
|
||
return status | ||
|
||
def update_status(self, status: DataStoreStatus): | ||
self.__lock.lock() | ||
old_value, self.__status = self.__status, status | ||
self.__lock.unlock() | ||
|
||
if old_value != status: | ||
self.__listeners.notify(status) | ||
|
||
|
||
class DataStoreStatusProviderImpl(DataStoreStatusProvider): | ||
def __init__(self, store: _FeatureStoreClientWrapper, update_sink: DataStoreUpdateSinkImpl): | ||
self.__store = store | ||
self.__update_sink = update_sink | ||
|
||
@property | ||
def status(self) -> DataStoreStatus: | ||
return self.__update_sink.status() | ||
|
||
def is_monitoring_enabled(self) -> bool: | ||
return self.__store.is_monitoring_enabled() | ||
|
||
def add_listener(self, listener: Callable[[DataStoreStatus], None]): | ||
self.__update_sink.listeners.add(listener) | ||
|
||
def remove_listener(self, listener: Callable[[DataStoreStatus], None]): | ||
self.__update_sink.listeners.remove(listener) |
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
Oops, something went wrong.