Make PostHog async (#3786)

This commit is contained in:
Stanislav Novosad
2025-10-21 21:14:02 -06:00
committed by GitHub
parent 1b5fad9743
commit 54918a1b92
7 changed files with 31 additions and 25 deletions

View File

@@ -12,14 +12,16 @@ class BaseExperimentationProvider(ABC):
payload_map: dict[str, dict[str, str | None]] = {}
@abstractmethod
def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
async def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
"""Check if a specific feature is enabled."""
def is_feature_enabled_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
async def is_feature_enabled_cached(
self, feature_name: str, distinct_id: str, properties: dict | None = None
) -> bool:
if feature_name not in self.result_map:
self.result_map[feature_name] = {}
if distinct_id not in self.result_map[feature_name]:
feature_flag_value = self.is_feature_enabled(feature_name, distinct_id, properties)
feature_flag_value = await self.is_feature_enabled(feature_name, distinct_id, properties)
self.result_map[feature_name][distinct_id] = feature_flag_value
if feature_flag_value:
LOG.info("Feature flag is enabled", flag=feature_name, distinct_id=distinct_id)
@@ -27,30 +29,32 @@ class BaseExperimentationProvider(ABC):
return self.result_map[feature_name][distinct_id]
@abstractmethod
def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
async def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
"""Get the value of a feature."""
@abstractmethod
def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
async def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
"""Get the payload for a feature flag if it exists."""
def get_value_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
async def get_value_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
"""Get the value of a feature."""
if feature_name not in self.variant_map:
self.variant_map[feature_name] = {}
if distinct_id not in self.variant_map[feature_name]:
variant = self.get_value(feature_name, distinct_id, properties)
variant = await self.get_value(feature_name, distinct_id, properties)
self.variant_map[feature_name][distinct_id] = variant
if variant:
LOG.info("Feature is found", flag=feature_name, distinct_id=distinct_id, variant=variant)
return self.variant_map[feature_name][distinct_id]
def get_payload_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
async def get_payload_cached(
self, feature_name: str, distinct_id: str, properties: dict | None = None
) -> str | None:
"""Get the payload for a feature flag if it exists."""
if feature_name not in self.payload_map:
self.payload_map[feature_name] = {}
if distinct_id not in self.payload_map[feature_name]:
payload = self.get_payload(feature_name, distinct_id, properties)
payload = await self.get_payload(feature_name, distinct_id, properties)
self.payload_map[feature_name][distinct_id] = payload
if payload:
LOG.info("Feature payload is found", flag=feature_name, distinct_id=distinct_id)
@@ -58,11 +62,11 @@ class BaseExperimentationProvider(ABC):
class NoOpExperimentationProvider(BaseExperimentationProvider):
def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
async def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
return False
def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
async def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
return None
def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
async def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
return None