Skip to content

Commit 5d5f882

Browse files
Merge pull request #35 from hellohaptik/develop
develop -> master
2 parents 9ddf550 + 5447a9d commit 5d5f882

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

FeatureToggle/__init__.py

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from UnleashClient import constants as consts
88
from UnleashClient import UnleashClient
99
from UnleashClient.utils import LOGGER
10+
from FeatureToggle.utils import timed_lru_cache
1011

1112

1213
def split_and_strip(parameters: str):
@@ -50,6 +51,7 @@ def initialize(url: str,
5051
FeatureToggles.__redis_db = redis_db
5152
FeatureToggles.__enable_toggle_service = enable_toggle_service
5253
FeatureToggles.__cache = FeatureToggles.__get_cache()
54+
LOGGER.info(f'Initializing Feature toggles')
5355
else:
5456
raise Exception("Client has been already initialized")
5557

@@ -88,9 +90,9 @@ def update_cache(data: Dict[str, Any]) -> None:
8890
)
8991
except Exception as err:
9092
raise Exception(
91-
f'Exception occured while updating the redis cache: {str(err)}'
93+
f'Exception occurred while updating the redis cache: {str(err)}'
9294
)
93-
LOGGER.info(f'Cache Updatation is Done')
95+
LOGGER.info(f'[Feature Toggles] Cache Updated')
9496

9597
@staticmethod
9698
def __get_unleash_client():
@@ -145,14 +147,8 @@ def is_enabled_for_domain(feature_name: str,
145147
Returns:
146148
(bool): True if Feature is enabled else False
147149
"""
148-
feature_name = FeatureToggles.__get_full_feature_name(feature_name)
149-
150-
context = {}
151-
if domain_name:
152-
context['domain_names'] = domain_name
153-
154-
return FeatureToggles.__get_unleash_client().is_enabled(feature_name,
155-
context)
150+
feature_toggles = FeatureToggles.fetch_feature_toggles()
151+
return domain_name in feature_toggles.get(feature_name, {}).get('domain_names', [])
156152

157153
@staticmethod
158154
def is_enabled_for_partner(feature_name: str,
@@ -165,14 +161,8 @@ def is_enabled_for_partner(feature_name: str,
165161
Returns:
166162
(bool): True if Feature is enabled else False
167163
"""
168-
feature_name = FeatureToggles.__get_full_feature_name(feature_name)
169-
170-
context = {}
171-
if partner_name:
172-
context['partner_names'] = partner_name
173-
174-
return FeatureToggles.__get_unleash_client().is_enabled(feature_name,
175-
context)
164+
feature_toggles = FeatureToggles.fetch_feature_toggles()
165+
return partner_name in feature_toggles.get(feature_name, {}).get('partner_names', [])
176166

177167
@staticmethod
178168
def is_enabled_for_business(feature_name: str,
@@ -185,14 +175,8 @@ def is_enabled_for_business(feature_name: str,
185175
Returns:
186176
(bool): True if Feature is enabled else False
187177
"""
188-
feature_name = FeatureToggles.__get_full_feature_name(feature_name)
189-
190-
context = {}
191-
if business_via_name:
192-
context['business_via_names'] = business_via_name
193-
194-
return FeatureToggles.__get_unleash_client().is_enabled(feature_name,
195-
context)
178+
feature_toggles = FeatureToggles.fetch_feature_toggles()
179+
return business_via_name in feature_toggles.get(feature_name, {}).get('business_via_names', [])
196180

197181
@staticmethod
198182
def is_enabled_for_expert(feature_name: str,
@@ -205,14 +189,8 @@ def is_enabled_for_expert(feature_name: str,
205189
Returns:
206190
(bool): True if Feature is enabled else False
207191
"""
208-
feature_name = FeatureToggles.__get_full_feature_name(feature_name)
209-
210-
context = {}
211-
if expert_email:
212-
context['expert_emails'] = expert_email
213-
214-
return FeatureToggles.__get_unleash_client().is_enabled(feature_name,
215-
context)
192+
feature_toggles = FeatureToggles.fetch_feature_toggles()
193+
return expert_email in feature_toggles.get(feature_name, {}).get('expert_emails', [])
216194

217195
@staticmethod
218196
def is_enabled_for_team(feature_name: str,
@@ -225,19 +203,11 @@ def is_enabled_for_team(feature_name: str,
225203
Returns:
226204
(bool): True if feature is enabled else False
227205
"""
228-
feature_name = FeatureToggles.__get_full_feature_name(feature_name)
229-
230-
context = {}
231-
if team_id:
232-
context['team_ids'] = team_id
233-
234-
return (
235-
FeatureToggles
236-
.__get_unleash_client()
237-
.is_enabled(feature_name, context)
238-
)
206+
feature_toggles = FeatureToggles.fetch_feature_toggles()
207+
return team_id in feature_toggles.get(feature_name, {}).get('team_ids', [])
239208

240209
@staticmethod
210+
@timed_lru_cache(seconds=(60*60), maxsize=2048)
241211
def fetch_feature_toggles():
242212
"""
243213
Returns(Dict):
@@ -251,7 +221,7 @@ def fetch_feature_toggles():
251221
}
252222
"""
253223
# TODO: Remove the cas and environment name from the feature toggles while returning the response
254-
224+
LOGGER.info(f'Loading Feature Toggles from Redis')
255225
if FeatureToggles.__cache is None:
256226
raise Exception(
257227
'To update cache Feature Toggles class needs to be initialised'
@@ -311,5 +281,4 @@ def fetch_feature_toggles():
311281
except Exception as err:
312282
# Handle this exception from where this util gets called
313283
raise Exception(f'An error occurred while parsing the response: {str(err)}')
314-
315284
return response

FeatureToggle/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from UnleashClient import LOGGER
2+
from datetime import datetime, timedelta
3+
from functools import lru_cache, wraps
4+
5+
6+
def timed_lru_cache(seconds: int, maxsize: int = 128):
7+
LOGGER.info(f'timed_lru_cache was called')
8+
9+
def wrapper_cache(func):
10+
func = lru_cache(maxsize=maxsize)(func)
11+
func.lifetime = timedelta(seconds=seconds)
12+
func.expiration = datetime.utcnow() + func.lifetime
13+
14+
@wraps(func)
15+
def wrapped_func(*args, **kwargs):
16+
if datetime.utcnow() >= func.expiration:
17+
func.cache_clear()
18+
func.expiration = datetime.utcnow() + func.lifetime
19+
return func(*args, **kwargs)
20+
21+
return wrapped_func
22+
23+
return wrapper_cache

0 commit comments

Comments
 (0)