1313from src .utils .my_logger import init_logger
1414
1515lock = asyncio .Lock ()
16- # TODO convert api_keys_lookup to a function that looksup api_keys from a redis cache
16+
1717api_keys_lookup = api_keys .get
1818cache_api_keys_func = cache_api_keys
1919
@@ -160,7 +160,7 @@ async def take_credit_method(api_key: str, path: str):
160160
161161 with next (sessions ) as session :
162162 # TO Update subscription Model
163- # TODO create a queue and put this in
163+ # TODO create a queue and put this in will make sense as demand for services increases
164164 await Subscriptions .update_subscription (subscription_data = subscription_dict , session = session )
165165
166166
@@ -188,7 +188,15 @@ async def return_kwargs(kwargs):
188188 return api_key , path
189189
190190 async def rate_limiter (api_key ):
191- """this method applies the actual rate_limiter"""
191+ """
192+ **rate_limiter**
193+ this only rate limits clients by api keys,
194+ there is also a regional rate limiter and a global rate limit both created so that the gateway
195+ does not end up taking too much traffic and is able to recover from DDOS attacks easily.
196+
197+ --> the rate_limiter has a side effect of also authorizing the client based on API Keys
198+
199+ this method applies the actual rate_limiter per client basis"""
192200 # Rate Limiting Section
193201 async with apikeys_lock :
194202 api_keys_model_dict : dict [str , str | int ] = api_keys_lookup (api_key )
@@ -200,7 +208,6 @@ async def rate_limiter(api_key):
200208 if now - last_request_timestamp > duration :
201209 api_keys_model_dict ['requests_count' ] = 0
202210 if api_keys_model_dict ['requests_count' ] >= limit :
203- # TODO consider returning a JSON String with data on the rate rate_limit and how long to wait
204211 time_left = last_request_timestamp + duration - now
205212 mess : str = f"EOD Stock API - Rate Limit Exceeded. Please wait { time_left :.0f} seconds before making " \
206213 f"another request, or upgrade your plan to better take advantage of extra resources " \
0 commit comments