Skip to content

Commit e8ab15c

Browse files
koukthomaspoignant
andauthored
fix: cache key in python provider should contain flag key (#3614)
* cache key in python provider should contain flag key * rename evaluation_context_hash to cache_key * python: update tests/test_websocket_cache_invalidation.py --------- Co-authored-by: Thomas Poignant <[email protected]>
1 parent 08cf093 commit e8ab15c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

openfeature/providers/python-provider/gofeatureflag_python_provider/provider.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ def __init__(self, **data):
7070
websocket.enableTrace(self.options.debug)
7171
self._ws = websocket.WebSocketApp(
7272
self._build_websocket_uri(),
73+
on_open=self.on_open,
7374
on_message=self._websocket_message_handler,
7475
)
7576

77+
def on_open(self, ws):
78+
if self._cache is not None:
79+
self._cache.clear()
80+
7681
def initialize(self, evaluation_context: EvaluationContext) -> None:
7782
"""
7883
initialize is called when the provider is initialized.
@@ -179,11 +184,11 @@ def generic_go_feature_flag_resolver(
179184
user=goff_evaluation_context,
180185
defaultValue=default_value,
181186
)
182-
evaluation_context_hash = goff_evaluation_context.hash()
187+
cache_key = f"{flag_key}:{goff_evaluation_context.hash()}"
183188
is_from_cache = False
184189

185-
if evaluation_context_hash in self._cache:
186-
response_body = self._cache[evaluation_context_hash]
190+
if cache_key in self._cache:
191+
response_body = self._cache[cache_key]
187192
is_from_cache = True
188193
else:
189194
headers = {"Content-Type": "application/json"}
@@ -240,7 +245,7 @@ def generic_go_feature_flag_resolver(
240245
)
241246

242247
if response_flag_evaluation.cacheable:
243-
self._cache[evaluation_context_hash] = response_body
248+
self._cache[cache_key] = response_body
244249

245250
if original_type == int:
246251
response_json = json.loads(response_body)

openfeature/providers/python-provider/tests/test_websocket_cache_invalidation.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77
import yaml
88
from openfeature import api
9-
from openfeature.flag_evaluation import FlagEvaluationDetails, Reason
9+
from openfeature.flag_evaluation import FlagEvaluationDetails, Reason, ErrorCode
1010

1111
from gofeatureflag_python_provider.options import GoFeatureFlagOptions
1212
from gofeatureflag_python_provider.provider import GoFeatureFlagProvider
@@ -20,7 +20,7 @@ def is_responsive(url):
2020
response = requests.get(url)
2121
if response.status_code == 200:
2222
return True
23-
except ConnectionError:
23+
except requests.exceptions.ConnectionError:
2424
return False
2525

2626

@@ -80,6 +80,15 @@ def test_test_websocket_cache_invalidation(goff):
8080
)
8181
want.reason = Reason.CACHED
8282
assert got == want
83+
84+
# test https://github.com/thomaspoignant/go-feature-flag/issues/3613
85+
got = client.get_boolean_details(
86+
flag_key="nonexistent-flag-key",
87+
default_value=False,
88+
evaluation_context=_default_evaluation_ctx,
89+
)
90+
assert got.error_code == ErrorCode.FLAG_NOT_FOUND
91+
8392
modify_flag_config()
8493
got = client.get_boolean_details(
8594
flag_key=flag_key,

0 commit comments

Comments
 (0)