growthbook 1.4.3__py2.py3-none-any.whl → 1.4.5__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- growthbook/__init__.py +1 -1
- growthbook/common_types.py +1 -0
- growthbook/growthbook.py +89 -8
- growthbook/growthbook_client.py +28 -2
- {growthbook-1.4.3.dist-info → growthbook-1.4.5.dist-info}/METADATA +1 -1
- growthbook-1.4.5.dist-info/RECORD +15 -0
- growthbook-1.4.3.dist-info/RECORD +0 -15
- {growthbook-1.4.3.dist-info → growthbook-1.4.5.dist-info}/WHEEL +0 -0
- {growthbook-1.4.3.dist-info → growthbook-1.4.5.dist-info}/licenses/LICENSE +0 -0
- {growthbook-1.4.3.dist-info → growthbook-1.4.5.dist-info}/top_level.txt +0 -0
growthbook/__init__.py
CHANGED
growthbook/common_types.py
CHANGED
|
@@ -426,6 +426,7 @@ class Options:
|
|
|
426
426
|
sticky_bucket_service: Optional[AbstractStickyBucketService] = None
|
|
427
427
|
sticky_bucket_identifier_attributes: Optional[List[str]] = None
|
|
428
428
|
on_experiment_viewed: Optional[Callable[[Experiment, Result, Optional[UserContext]], None]] = None
|
|
429
|
+
on_feature_usage: Optional[Callable[[str, 'FeatureResult'], None]] = None
|
|
429
430
|
tracking_plugins: Optional[List[Any]] = None
|
|
430
431
|
|
|
431
432
|
|
growthbook/growthbook.py
CHANGED
|
@@ -100,7 +100,8 @@ class InMemoryFeatureCache(AbstractFeatureCache):
|
|
|
100
100
|
def set(self, key: str, value: Dict, ttl: int) -> None:
|
|
101
101
|
if key in self.cache:
|
|
102
102
|
self.cache[key].update(value)
|
|
103
|
-
|
|
103
|
+
else:
|
|
104
|
+
self.cache[key] = CacheEntry(value, ttl)
|
|
104
105
|
|
|
105
106
|
def clear(self) -> None:
|
|
106
107
|
self.cache.clear()
|
|
@@ -327,6 +328,11 @@ class FeatureRepository(object):
|
|
|
327
328
|
self.http: Optional[PoolManager] = None
|
|
328
329
|
self.sse_client: Optional[SSEClient] = None
|
|
329
330
|
self._feature_update_callbacks: List[Callable[[Dict], None]] = []
|
|
331
|
+
|
|
332
|
+
# Background refresh support
|
|
333
|
+
self._refresh_thread: Optional[threading.Thread] = None
|
|
334
|
+
self._refresh_stop_event = threading.Event()
|
|
335
|
+
self._refresh_lock = threading.Lock()
|
|
330
336
|
|
|
331
337
|
def set_cache(self, cache: AbstractFeatureCache) -> None:
|
|
332
338
|
self.cache = cache
|
|
@@ -375,6 +381,7 @@ class FeatureRepository(object):
|
|
|
375
381
|
return res
|
|
376
382
|
return cached
|
|
377
383
|
|
|
384
|
+
|
|
378
385
|
async def load_features_async(
|
|
379
386
|
self, api_host: str, client_key: str, decryption_key: str = "", ttl: int = 600
|
|
380
387
|
) -> Optional[Dict]:
|
|
@@ -493,6 +500,52 @@ class FeatureRepository(object):
|
|
|
493
500
|
if self.sse_client:
|
|
494
501
|
self.sse_client.disconnect(timeout=timeout)
|
|
495
502
|
self.sse_client = None
|
|
503
|
+
|
|
504
|
+
def start_background_refresh(self, api_host: str, client_key: str, decryption_key: str, ttl: int = 600, refresh_interval: int = 300) -> None:
|
|
505
|
+
"""Start periodic background refresh task"""
|
|
506
|
+
with self._refresh_lock:
|
|
507
|
+
if self._refresh_thread is not None:
|
|
508
|
+
return # Already running
|
|
509
|
+
|
|
510
|
+
self._refresh_stop_event.clear()
|
|
511
|
+
self._refresh_thread = threading.Thread(
|
|
512
|
+
target=self._background_refresh_worker,
|
|
513
|
+
args=(api_host, client_key, decryption_key, ttl, refresh_interval),
|
|
514
|
+
daemon=True
|
|
515
|
+
)
|
|
516
|
+
self._refresh_thread.start()
|
|
517
|
+
logger.debug("Started background refresh task")
|
|
518
|
+
|
|
519
|
+
def _background_refresh_worker(self, api_host: str, client_key: str, decryption_key: str, ttl: int, refresh_interval: int) -> None:
|
|
520
|
+
"""Worker method for periodic background refresh"""
|
|
521
|
+
while not self._refresh_stop_event.is_set():
|
|
522
|
+
try:
|
|
523
|
+
# Wait for the refresh interval or stop event
|
|
524
|
+
if self._refresh_stop_event.wait(refresh_interval):
|
|
525
|
+
break # Stop event was set
|
|
526
|
+
|
|
527
|
+
logger.debug("Background refresh for Features - started")
|
|
528
|
+
res = self._fetch_features(api_host, client_key, decryption_key)
|
|
529
|
+
if res is not None:
|
|
530
|
+
cache_key = api_host + "::" + client_key
|
|
531
|
+
self.cache.set(cache_key, res, ttl)
|
|
532
|
+
logger.debug("Background refresh completed")
|
|
533
|
+
# Notify callbacks about fresh features
|
|
534
|
+
self._notify_feature_update_callbacks(res)
|
|
535
|
+
else:
|
|
536
|
+
logger.warning("Background refresh failed")
|
|
537
|
+
except Exception as e:
|
|
538
|
+
logger.warning(f"Background refresh error: {e}")
|
|
539
|
+
|
|
540
|
+
def stop_background_refresh(self) -> None:
|
|
541
|
+
"""Stop background refresh task"""
|
|
542
|
+
self._refresh_stop_event.set()
|
|
543
|
+
|
|
544
|
+
with self._refresh_lock:
|
|
545
|
+
if self._refresh_thread is not None:
|
|
546
|
+
self._refresh_thread.join(timeout=1.0) # Wait up to 1 second
|
|
547
|
+
self._refresh_thread = None
|
|
548
|
+
logger.debug("Stopped background refresh task")
|
|
496
549
|
|
|
497
550
|
@staticmethod
|
|
498
551
|
def _get_features_url(api_host: str, client_key: str) -> str:
|
|
@@ -512,6 +565,7 @@ class GrowthBook(object):
|
|
|
512
565
|
features: dict = {},
|
|
513
566
|
qa_mode: bool = False,
|
|
514
567
|
on_experiment_viewed=None,
|
|
568
|
+
on_feature_usage=None,
|
|
515
569
|
api_host: str = "",
|
|
516
570
|
client_key: str = "",
|
|
517
571
|
decryption_key: str = "",
|
|
@@ -522,6 +576,8 @@ class GrowthBook(object):
|
|
|
522
576
|
savedGroups: dict = {},
|
|
523
577
|
streaming: bool = False,
|
|
524
578
|
streaming_connection_timeout: int = 30,
|
|
579
|
+
stale_while_revalidate: bool = False,
|
|
580
|
+
stale_ttl: int = 300, # 5 minutes default
|
|
525
581
|
plugins: List = None,
|
|
526
582
|
# Deprecated args
|
|
527
583
|
trackingCallback=None,
|
|
@@ -548,9 +604,12 @@ class GrowthBook(object):
|
|
|
548
604
|
|
|
549
605
|
self._qaMode = qa_mode or qaMode
|
|
550
606
|
self._trackingCallback = on_experiment_viewed or trackingCallback
|
|
607
|
+
self._featureUsageCallback = on_feature_usage
|
|
551
608
|
|
|
552
609
|
self._streaming = streaming
|
|
553
610
|
self._streaming_timeout = streaming_connection_timeout
|
|
611
|
+
self._stale_while_revalidate = stale_while_revalidate
|
|
612
|
+
self._stale_ttl = stale_ttl
|
|
554
613
|
|
|
555
614
|
# Deprecated args
|
|
556
615
|
self._user = user
|
|
@@ -603,6 +662,13 @@ class GrowthBook(object):
|
|
|
603
662
|
if self._streaming:
|
|
604
663
|
self.load_features()
|
|
605
664
|
self.startAutoRefresh()
|
|
665
|
+
elif self._stale_while_revalidate and self._client_key:
|
|
666
|
+
# Start background refresh task for stale-while-revalidate
|
|
667
|
+
self.load_features() # Initial load
|
|
668
|
+
feature_repo.start_background_refresh(
|
|
669
|
+
self._api_host, self._client_key, self._decryption_key,
|
|
670
|
+
self._cache_ttl, self._stale_ttl
|
|
671
|
+
)
|
|
606
672
|
|
|
607
673
|
def _on_feature_update(self, features_data: Dict) -> None:
|
|
608
674
|
"""Callback to handle automatic feature updates from FeatureRepository"""
|
|
@@ -739,6 +805,13 @@ class GrowthBook(object):
|
|
|
739
805
|
except Exception as e:
|
|
740
806
|
logger.warning(f"Error stopping auto refresh during destroy: {e}")
|
|
741
807
|
|
|
808
|
+
try:
|
|
809
|
+
# Stop background refresh operations
|
|
810
|
+
if self._stale_while_revalidate and self._client_key:
|
|
811
|
+
feature_repo.stop_background_refresh()
|
|
812
|
+
except Exception as e:
|
|
813
|
+
logger.warning(f"Error stopping background refresh during destroy: {e}")
|
|
814
|
+
|
|
742
815
|
try:
|
|
743
816
|
# Clean up feature update callback
|
|
744
817
|
if self._client_key:
|
|
@@ -752,6 +825,7 @@ class GrowthBook(object):
|
|
|
752
825
|
self._tracked.clear()
|
|
753
826
|
self._assigned.clear()
|
|
754
827
|
self._trackingCallback = None
|
|
828
|
+
self._featureUsageCallback = None
|
|
755
829
|
self._forcedVariations.clear()
|
|
756
830
|
self._overrides.clear()
|
|
757
831
|
self._groups.clear()
|
|
@@ -790,8 +864,8 @@ class GrowthBook(object):
|
|
|
790
864
|
def _ensure_fresh_features(self) -> None:
|
|
791
865
|
"""Lazy refresh: Check cache expiry and refresh if needed, but only if client_key is provided"""
|
|
792
866
|
|
|
793
|
-
if self._streaming or not self._client_key:
|
|
794
|
-
return # Skip cache checks - SSE
|
|
867
|
+
if self._streaming or self._stale_while_revalidate or not self._client_key:
|
|
868
|
+
return # Skip cache checks - SSE or background refresh handles freshness
|
|
795
869
|
|
|
796
870
|
try:
|
|
797
871
|
self.load_features()
|
|
@@ -815,11 +889,18 @@ class GrowthBook(object):
|
|
|
815
889
|
)
|
|
816
890
|
|
|
817
891
|
def eval_feature(self, key: str) -> FeatureResult:
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
892
|
+
result = core_eval_feature(key=key,
|
|
893
|
+
evalContext=self._get_eval_context(),
|
|
894
|
+
callback_subscription=self._fireSubscriptions,
|
|
895
|
+
tracking_cb=self._track
|
|
896
|
+
)
|
|
897
|
+
# Call feature usage callback if provided
|
|
898
|
+
if self._featureUsageCallback:
|
|
899
|
+
try:
|
|
900
|
+
self._featureUsageCallback(key, result)
|
|
901
|
+
except Exception:
|
|
902
|
+
pass
|
|
903
|
+
return result
|
|
823
904
|
|
|
824
905
|
# @deprecated, use get_all_results
|
|
825
906
|
def getAllResults(self):
|
growthbook/growthbook_client.py
CHANGED
|
@@ -500,24 +500,50 @@ class GrowthBookClient:
|
|
|
500
500
|
async with self._context_lock:
|
|
501
501
|
context = await self.create_evaluation_context(user_context)
|
|
502
502
|
result = core_eval_feature(key=key, evalContext=context, tracking_cb=self._track)
|
|
503
|
+
# Call feature usage callback if provided
|
|
504
|
+
if self.options.on_feature_usage:
|
|
505
|
+
try:
|
|
506
|
+
self.options.on_feature_usage(key, result)
|
|
507
|
+
except Exception:
|
|
508
|
+
logger.exception("Error in feature usage callback")
|
|
503
509
|
return result
|
|
504
510
|
|
|
505
511
|
async def is_on(self, key: str, user_context: UserContext) -> bool:
|
|
506
512
|
"""Check if a feature is enabled with proper async context management"""
|
|
507
513
|
async with self._context_lock:
|
|
508
514
|
context = await self.create_evaluation_context(user_context)
|
|
509
|
-
|
|
515
|
+
result = core_eval_feature(key=key, evalContext=context, tracking_cb=self._track)
|
|
516
|
+
# Call feature usage callback if provided
|
|
517
|
+
if self.options.on_feature_usage:
|
|
518
|
+
try:
|
|
519
|
+
self.options.on_feature_usage(key, result)
|
|
520
|
+
except Exception:
|
|
521
|
+
logger.exception("Error in feature usage callback")
|
|
522
|
+
return result.on
|
|
510
523
|
|
|
511
524
|
async def is_off(self, key: str, user_context: UserContext) -> bool:
|
|
512
525
|
"""Check if a feature is set to off with proper async context management"""
|
|
513
526
|
async with self._context_lock:
|
|
514
527
|
context = await self.create_evaluation_context(user_context)
|
|
515
|
-
|
|
528
|
+
result = core_eval_feature(key=key, evalContext=context, tracking_cb=self._track)
|
|
529
|
+
# Call feature usage callback if provided
|
|
530
|
+
if self.options.on_feature_usage:
|
|
531
|
+
try:
|
|
532
|
+
self.options.on_feature_usage(key, result)
|
|
533
|
+
except Exception:
|
|
534
|
+
logger.exception("Error in feature usage callback")
|
|
535
|
+
return result.off
|
|
516
536
|
|
|
517
537
|
async def get_feature_value(self, key: str, fallback: Any, user_context: UserContext) -> Any:
|
|
518
538
|
async with self._context_lock:
|
|
519
539
|
context = await self.create_evaluation_context(user_context)
|
|
520
540
|
result = core_eval_feature(key=key, evalContext=context, tracking_cb=self._track)
|
|
541
|
+
# Call feature usage callback if provided
|
|
542
|
+
if self.options.on_feature_usage:
|
|
543
|
+
try:
|
|
544
|
+
self.options.on_feature_usage(key, result)
|
|
545
|
+
except Exception:
|
|
546
|
+
logger.exception("Error in feature usage callback")
|
|
521
547
|
return result.value if result.value is not None else fallback
|
|
522
548
|
|
|
523
549
|
async def run(self, experiment: Experiment, user_context: UserContext) -> Result:
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
growthbook/__init__.py,sha256=JLdue4592q3iH1ck1qG9Fbv-p8i07SJo-tUJhGCf60s,444
|
|
2
|
+
growthbook/common_types.py,sha256=KYA9rmWRMde2JnsUjygsiaJ1q-KZakDdzPAtUOnrKyY,14959
|
|
3
|
+
growthbook/core.py,sha256=n9nwna26iZTY48LIvQqu5N_RrE35X0wlRBhq0-Qdb-s,35241
|
|
4
|
+
growthbook/growthbook.py,sha256=xjuX-q8oPsLqP0kWBZqy7LpoTkrgjMZCOWjtpUsQR1o,39793
|
|
5
|
+
growthbook/growthbook_client.py,sha256=kfdc2NGhdmsXxaVIm0CBlNCMkwicfPpbDZ10nZSPd7w,24576
|
|
6
|
+
growthbook/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
growthbook/plugins/__init__.py,sha256=y2eAV1sA041XWcftBVTDH0t-ggy9r2C5oKRYRF6XR6s,602
|
|
8
|
+
growthbook/plugins/base.py,sha256=PWBXUBj62hi25Y5Eif9WmEWagWdkwGXHi2dMtn44bo8,3637
|
|
9
|
+
growthbook/plugins/growthbook_tracking.py,sha256=FvPFOuKF_xKjmTX8x_hzMlHrrL-68Y2ZPw1Hfl2_ilQ,11333
|
|
10
|
+
growthbook/plugins/request_context.py,sha256=O5FJDrjJR5u0rx3ENGO9cOsKMHd9e0l0Nvdb1PHfmm8,12951
|
|
11
|
+
growthbook-1.4.5.dist-info/licenses/LICENSE,sha256=D-TcBckB0dTPUlNJ8jBiTIJIj1ekHLB1CY7HJtJKhMY,1069
|
|
12
|
+
growthbook-1.4.5.dist-info/METADATA,sha256=8KBWZ908qYYku0GqZCeZ1KNS5YLPxs7FXZVl6wesLWM,22074
|
|
13
|
+
growthbook-1.4.5.dist-info/WHEEL,sha256=JNWh1Fm1UdwIQV075glCn4MVuCRs0sotJIq-J6rbxCU,109
|
|
14
|
+
growthbook-1.4.5.dist-info/top_level.txt,sha256=dzfRQFGYejCIUstRSrrRVTMlxf7pBqASTI5S8gGRlXw,11
|
|
15
|
+
growthbook-1.4.5.dist-info/RECORD,,
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
growthbook/__init__.py,sha256=Js_KujzX2ehccclPD8fRM8duCGQdwiOyyV5uNdWCKf8,444
|
|
2
|
-
growthbook/common_types.py,sha256=OUGkqoUuYetWz1cyA1eWz5DM3awYw_ExcNAjFqJuGAc,14881
|
|
3
|
-
growthbook/core.py,sha256=n9nwna26iZTY48LIvQqu5N_RrE35X0wlRBhq0-Qdb-s,35241
|
|
4
|
-
growthbook/growthbook.py,sha256=EK7nkS0Rbkc0aDORKh_0ie6cc7hXlJUui5ve-GkWmHk,35996
|
|
5
|
-
growthbook/growthbook_client.py,sha256=1bDIuJoxlKUR_bKe_gD6V7JlUPt53uGgix9DhgSkPPc,23360
|
|
6
|
-
growthbook/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
-
growthbook/plugins/__init__.py,sha256=y2eAV1sA041XWcftBVTDH0t-ggy9r2C5oKRYRF6XR6s,602
|
|
8
|
-
growthbook/plugins/base.py,sha256=PWBXUBj62hi25Y5Eif9WmEWagWdkwGXHi2dMtn44bo8,3637
|
|
9
|
-
growthbook/plugins/growthbook_tracking.py,sha256=FvPFOuKF_xKjmTX8x_hzMlHrrL-68Y2ZPw1Hfl2_ilQ,11333
|
|
10
|
-
growthbook/plugins/request_context.py,sha256=O5FJDrjJR5u0rx3ENGO9cOsKMHd9e0l0Nvdb1PHfmm8,12951
|
|
11
|
-
growthbook-1.4.3.dist-info/licenses/LICENSE,sha256=D-TcBckB0dTPUlNJ8jBiTIJIj1ekHLB1CY7HJtJKhMY,1069
|
|
12
|
-
growthbook-1.4.3.dist-info/METADATA,sha256=l5wX61QF36lMGmPSxZsr_CKmHOdqOU9PKAbQPA3gN50,22074
|
|
13
|
-
growthbook-1.4.3.dist-info/WHEEL,sha256=JNWh1Fm1UdwIQV075glCn4MVuCRs0sotJIq-J6rbxCU,109
|
|
14
|
-
growthbook-1.4.3.dist-info/top_level.txt,sha256=dzfRQFGYejCIUstRSrrRVTMlxf7pBqASTI5S8gGRlXw,11
|
|
15
|
-
growthbook-1.4.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|