13
13
from src .utils .my_logger import init_logger
14
14
15
15
lock = asyncio .Lock ()
16
- # TODO convert api_keys_lookup to a function that looksup api_keys from a redis cache
16
+
17
17
api_keys_lookup = api_keys .get
18
18
cache_api_keys_func = cache_api_keys
19
19
@@ -160,7 +160,7 @@ async def take_credit_method(api_key: str, path: str):
160
160
161
161
with next (sessions ) as session :
162
162
# 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
164
164
await Subscriptions .update_subscription (subscription_data = subscription_dict , session = session )
165
165
166
166
@@ -188,7 +188,15 @@ async def return_kwargs(kwargs):
188
188
return api_key , path
189
189
190
190
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"""
192
200
# Rate Limiting Section
193
201
async with apikeys_lock :
194
202
api_keys_model_dict : dict [str , str | int ] = api_keys_lookup (api_key )
@@ -200,7 +208,6 @@ async def rate_limiter(api_key):
200
208
if now - last_request_timestamp > duration :
201
209
api_keys_model_dict ['requests_count' ] = 0
202
210
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
204
211
time_left = last_request_timestamp + duration - now
205
212
mess : str = f"EOD Stock API - Rate Limit Exceeded. Please wait { time_left :.0f} seconds before making " \
206
213
f"another request, or upgrade your plan to better take advantage of extra resources " \
0 commit comments