77from typing import Any , Callable , List , Optional , Tuple
88from uuid import uuid4
99
10- from ldclient . async_config import AsyncConfig
11- from ldclient .async_feature_store import AsyncInMemoryFeatureStore
10+ from ldclient import async_datasystem
11+ from ldclient .async_config import AsyncConfig , AsyncDataSystemConfig
1212from ldclient .context import Context
1313from ldclient .evaluation import EvaluationDetail , FeatureFlagsState
1414from ldclient .hook import (
5252from ldclient .versioned_data_kind import FEATURES , SEGMENTS
5353
5454
55+ def _is_daemon_mode (datasystem_config : AsyncDataSystemConfig ) -> bool :
56+ """
57+ Return true if the data system has a persistent store and no data sources. The SDK then reads
58+ flags only from the store, which another process such as the Relay Proxy keeps up to date.
59+ """
60+ return _has_no_data_sources (datasystem_config ) and datasystem_config .data_store is not None
61+
62+
63+ def _has_no_data_sources (datasystem_config : AsyncDataSystemConfig ) -> bool :
64+ return not datasystem_config .initializers and not datasystem_config .synchronizers
65+
66+
5567class AsyncLDClient :
5668 """Async LaunchDarkly SDK client.
5769
@@ -76,6 +88,7 @@ def __init__(self, config: AsyncConfig):
7688
7789 self ._config = config
7890 self ._config ._instance_id = str (uuid4 ())
91+ self ._datasystem_config = config .datasystem_config or async_datasystem .default ().build ()
7992 self ._lifecycle_lock = asyncio .Lock ()
8093
8194 self ._started = False
@@ -202,17 +215,17 @@ async def __start_up(self, start_wait: float):
202215 # Start the big-segment status poll now that a loop is running.
203216 self .__big_segment_store_manager .start ()
204217
205- # FDv2 builds its data sources from builders; wire the shared session into
206- # them before starting (FDv1 pulls the session itself via its provider).
207- datasystem_config = self ._config .datasystem_config
208- if datasystem_config is not None and not self ._config .offline :
209- self ._wire_data_source_sessions (datasystem_config )
218+ # The data system builds its data sources from builders. Give the shared
219+ # session to the builders before the data system starts.
220+ if not self ._config .offline :
221+ self ._wire_data_source_sessions (self ._datasystem_config )
210222
211223 if self ._config .offline :
212224 log .info ("Started LaunchDarkly Client in offline mode" )
213-
214- if self ._config .use_ldd :
215- log .info ("Started LaunchDarkly Client in LDD mode" )
225+ elif _is_daemon_mode (self ._datasystem_config ):
226+ log .info ("Started LaunchDarkly Client in daemon mode" )
227+ elif _has_no_data_sources (self ._datasystem_config ):
228+ log .warning ("The data system has no data sources and no persistent store. All evaluations will return default values." )
216229
217230 diagnostic_accumulator = self ._set_event_processor (self ._config )
218231
@@ -224,7 +237,7 @@ async def __start_up(self, start_wait: float):
224237 update_processor_ready = AsyncEvent ()
225238 self ._data_system .start (update_processor_ready )
226239
227- if not self ._config .offline and not self ._config . use_ldd :
240+ if not self ._config .offline and not _is_daemon_mode ( self ._datasystem_config ) :
228241 if start_wait > 60 :
229242 log .warning (f"Client was configured to block for up to { start_wait } seconds when initializing. We recommend blocking no longer than 60." )
230243
@@ -239,7 +252,7 @@ async def __start_up(self, start_wait: float):
239252
240253 def _get_session (self ):
241254 """Return the shared aiohttp session, creating it on first use inside the
242- event loop. Nothing creates it in offline/LDD mode, because no network
255+ event loop. Nothing creates it in offline or daemon mode, because no network
243256 component asks for it. Uses the same factory as the async transport so
244257 SSL/cert setup and proxy handling stay consistent (proxies are resolved
245258 per request, so the session itself has trust_env=False)."""
@@ -248,23 +261,9 @@ def _get_session(self):
248261 return self ._session
249262
250263 def _make_data_system (self ) -> AsyncDataSystem :
251- datasystem_config = self ._config .datasystem_config
252- if datasystem_config is None :
253- from ldclient .impl .datasystem .async_fdv1 import AsyncFDv1
254-
255- return AsyncFDv1 (self ._config , self ._select_feature_store (), self ._get_session )
256-
257264 from ldclient .impl .datasystem .async_fdv2 import AsyncFDv2
258265
259- return AsyncFDv2 (self ._config , datasystem_config )
260-
261- def _select_feature_store (self ) -> AsyncFeatureStore :
262- """Choose the async feature store for the v1 data system based on the
263- configured store."""
264- feature_store = self ._config .feature_store
265- if feature_store is None :
266- return AsyncInMemoryFeatureStore ()
267- return feature_store
266+ return AsyncFDv2 (self ._config , self ._datasystem_config )
268267
269268 def _wire_data_source_sessions (self , data_system_config ) -> None :
270269 """Provide the client's aiohttp session to any async data source
@@ -394,7 +393,7 @@ async def is_initialized(self) -> bool:
394393
395394 This is a coroutine because determining readiness may query a persistent store.
396395 """
397- if self .is_offline () or self . _config . use_ldd :
396+ if self .is_offline ():
398397 return True
399398
400399 return (await self ._data_system .data_availability ()).at_least (DataAvailability .CACHED )
@@ -499,7 +498,7 @@ async def _evaluate_internal(self, key: str, context: Context, default: Any, eve
499498 return EvaluationDetail (default , None , error_reason ('CLIENT_NOT_READY' )), None
500499
501500 availability = await self ._data_system .data_availability ()
502- if availability != DataAvailability . REFRESHED :
501+ if not availability . at_least ( self . _data_system . target_availability ) :
503502 if availability == DataAvailability .CACHED :
504503 if not self ._eval_cached_data_warned :
505504 self ._eval_cached_data_warned = True
@@ -573,7 +572,7 @@ async def all_flags_state(self, context: Context, **kwargs) -> FeatureFlagsState
573572 return FeatureFlagsState (False )
574573
575574 availability = await self ._data_system .data_availability ()
576- if availability != DataAvailability . REFRESHED :
575+ if not availability . at_least ( self . _data_system . target_availability ) :
577576 if availability == DataAvailability .CACHED :
578577 if not self ._all_flags_cached_data_warned :
579578 self ._all_flags_cached_data_warned = True
0 commit comments