|
| 1 | +from threading import Event |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from ldclient.config import Config |
| 5 | +from ldclient.impl.datasource.feature_requester import FeatureRequesterImpl |
| 6 | +from ldclient.impl.datasource.polling import PollingUpdateProcessor |
| 7 | +from ldclient.impl.datasource.status import ( |
| 8 | + DataSourceStatusProviderImpl, |
| 9 | + DataSourceUpdateSinkImpl |
| 10 | +) |
| 11 | +from ldclient.impl.datasource.streaming import StreamingUpdateProcessor |
| 12 | +from ldclient.impl.datastore.status import ( |
| 13 | + DataStoreStatusProviderImpl, |
| 14 | + DataStoreUpdateSinkImpl |
| 15 | +) |
| 16 | +from ldclient.impl.datasystem import DataAvailability |
| 17 | +from ldclient.impl.flag_tracker import FlagTrackerImpl |
| 18 | +from ldclient.impl.listeners import Listeners |
| 19 | +from ldclient.impl.stubs import NullUpdateProcessor |
| 20 | +from ldclient.interfaces import ( |
| 21 | + DataSourceState, |
| 22 | + DataSourceStatus, |
| 23 | + DataSourceStatusProvider, |
| 24 | + DataStoreStatusProvider, |
| 25 | + FeatureStore, |
| 26 | + FlagTracker, |
| 27 | + UpdateProcessor |
| 28 | +) |
| 29 | + |
| 30 | +# Delayed import inside __init__ to avoid circular dependency with ldclient.client |
| 31 | + |
| 32 | + |
| 33 | +class FDv1: |
| 34 | + """ |
| 35 | + FDv1 wires the existing v1 data source and store behavior behind the |
| 36 | + generic DataSystem surface. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, config: Config): |
| 40 | + self._config = config |
| 41 | + |
| 42 | + # Set up data store plumbing |
| 43 | + self._data_store_listeners = Listeners() |
| 44 | + self._data_store_update_sink = DataStoreUpdateSinkImpl( |
| 45 | + self._data_store_listeners |
| 46 | + ) |
| 47 | + # Import here to avoid circular import |
| 48 | + from ldclient.client import _FeatureStoreClientWrapper |
| 49 | + |
| 50 | + self._store_wrapper: FeatureStore = _FeatureStoreClientWrapper( |
| 51 | + self._config.feature_store, self._data_store_update_sink |
| 52 | + ) |
| 53 | + self._data_store_status_provider_impl = DataStoreStatusProviderImpl( |
| 54 | + self._store_wrapper, self._data_store_update_sink |
| 55 | + ) |
| 56 | + |
| 57 | + # Set up data source plumbing |
| 58 | + self._data_source_listeners = Listeners() |
| 59 | + self._flag_change_listeners = Listeners() |
| 60 | + self._flag_tracker_impl = FlagTrackerImpl( |
| 61 | + self._flag_change_listeners, |
| 62 | + # TODO: What is going on here? This doesn't seem right. |
| 63 | + # It requires calling the set_flag_value_eval_fn method. We should see if we can fix that. |
| 64 | + lambda key, context: None, # Replaced by client to use its evaluation method |
| 65 | + ) |
| 66 | + self._data_source_update_sink = DataSourceUpdateSinkImpl( |
| 67 | + self._store_wrapper, |
| 68 | + self._data_source_listeners, |
| 69 | + self._flag_change_listeners, |
| 70 | + ) |
| 71 | + self._data_source_status_provider_impl = DataSourceStatusProviderImpl( |
| 72 | + self._data_source_listeners, self._data_source_update_sink |
| 73 | + ) |
| 74 | + |
| 75 | + # Ensure v1 processors can find the sink via config for status updates |
| 76 | + self._config._data_source_update_sink = self._data_source_update_sink |
| 77 | + |
| 78 | + # Update processor created in start(), because it needs the ready Event |
| 79 | + self._update_processor: Optional[UpdateProcessor] = None |
| 80 | + |
| 81 | + # Track current data availability |
| 82 | + # QUESTION: Why cached? |
| 83 | + self._data_availability: DataAvailability = ( |
| 84 | + DataAvailability.CACHED |
| 85 | + if getattr(self._store_wrapper, "initialized", False) |
| 86 | + else DataAvailability.DEFAULTS |
| 87 | + ) |
| 88 | + |
| 89 | + # React to data source status updates to adjust availability |
| 90 | + def _on_status_change(status: DataSourceStatus): |
| 91 | + if status.state == DataSourceState.VALID: |
| 92 | + self._data_availability = DataAvailability.REFRESHED |
| 93 | + |
| 94 | + self._data_source_status_provider_impl.add_listener(_on_status_change) |
| 95 | + |
| 96 | + def start(self, set_on_ready: Event): |
| 97 | + """ |
| 98 | + Starts the v1 update processor and returns immediately. The provided |
| 99 | + Event is set by the processor upon first successful initialization or |
| 100 | + upon permanent failure. |
| 101 | + """ |
| 102 | + update_processor = self._make_update_processor( |
| 103 | + self._config, self._store_wrapper, set_on_ready |
| 104 | + ) |
| 105 | + self._update_processor = update_processor |
| 106 | + update_processor.start() |
| 107 | + |
| 108 | + def stop(self): |
| 109 | + if self._update_processor is not None: |
| 110 | + self._update_processor.stop() |
| 111 | + |
| 112 | + @property |
| 113 | + def store(self) -> FeatureStore: |
| 114 | + return self._store_wrapper |
| 115 | + |
| 116 | + def set_flag_value_eval_fn(self, eval_fn): |
| 117 | + """ |
| 118 | + Injects the flag value evaluation function used by the flag tracker to |
| 119 | + compute FlagValueChange events. The function signature should be |
| 120 | + (key: str, context: Context) -> Any. |
| 121 | + """ |
| 122 | + self._flag_tracker_impl = FlagTrackerImpl(self._flag_change_listeners, eval_fn) |
| 123 | + |
| 124 | + @property |
| 125 | + def data_source_status_provider(self) -> DataSourceStatusProvider: |
| 126 | + return self._data_source_status_provider_impl |
| 127 | + |
| 128 | + @property |
| 129 | + def data_store_status_provider(self) -> DataStoreStatusProvider: |
| 130 | + return self._data_store_status_provider_impl |
| 131 | + |
| 132 | + @property |
| 133 | + def flag_tracker(self) -> FlagTracker: |
| 134 | + return self._flag_tracker_impl |
| 135 | + |
| 136 | + @property |
| 137 | + def data_availability(self) -> DataAvailability: |
| 138 | + return self._data_availability |
| 139 | + |
| 140 | + @property |
| 141 | + def target_availability(self) -> DataAvailability: |
| 142 | + if self._config.offline: |
| 143 | + return DataAvailability.DEFAULTS |
| 144 | + # In LDD mode or normal connected modes, the ideal is to be refreshed |
| 145 | + return DataAvailability.REFRESHED |
| 146 | + |
| 147 | + def _make_update_processor(self, config: Config, store: FeatureStore, ready: Event): |
| 148 | + # Mirrors LDClient._make_update_processor but scoped for FDv1 |
| 149 | + if config.update_processor_class: |
| 150 | + return config.update_processor_class(config, store, ready) |
| 151 | + |
| 152 | + if config.offline or config.use_ldd: |
| 153 | + return NullUpdateProcessor(config, store, ready) |
| 154 | + |
| 155 | + if config.stream: |
| 156 | + # Diagnostic accumulator is handled in client; pass None here |
| 157 | + return StreamingUpdateProcessor(config, store, ready, None) |
| 158 | + |
| 159 | + # Polling mode |
| 160 | + feature_requester = ( |
| 161 | + config.feature_requester_class(config) |
| 162 | + if config.feature_requester_class is not None |
| 163 | + else FeatureRequesterImpl(config) |
| 164 | + ) |
| 165 | + return PollingUpdateProcessor(config, feature_requester, store, ready) |
0 commit comments