This commit is contained in:
parent
cf8934c605
commit
add5f6df63
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from dogpile.cache import make_region
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
|
@ -25,10 +26,33 @@ async def request_data(gql, headers=None):
|
||||||
return data
|
return data
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Handling and logging exceptions during authentication check
|
# Handling and logging exceptions during authentication check
|
||||||
logger.error(f'[services.auth] request_data error: {e}')
|
logger.error(f'request_data error: {e}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# Создание региона кэша с TTL 30 секунд
|
||||||
|
region = make_region().configure('dogpile.cache.memory', expiration_time=30)
|
||||||
|
|
||||||
|
# Функция-ключ для кэширования
|
||||||
|
def auth_cache_key(req):
|
||||||
|
token = req.headers.get('Authorization')
|
||||||
|
return f"auth_token:{token}"
|
||||||
|
|
||||||
|
# Декоратор для кэширования запроса проверки токена
|
||||||
|
def cache_auth_request(f):
|
||||||
|
@wraps(f)
|
||||||
|
async def decorated_function(*args, **kwargs):
|
||||||
|
req = args[0]
|
||||||
|
cache_key = auth_cache_key(req)
|
||||||
|
result = region.get(cache_key)
|
||||||
|
if result is None:
|
||||||
|
result = await f(*args, **kwargs)
|
||||||
|
region.set(cache_key, result)
|
||||||
|
return result
|
||||||
|
return decorated_function
|
||||||
|
|
||||||
|
# Измененная функция проверки аутентификации с кэшированием
|
||||||
|
@cache_auth_request
|
||||||
async def check_auth(req):
|
async def check_auth(req):
|
||||||
token = req.headers.get('Authorization')
|
token = req.headers.get('Authorization')
|
||||||
user_id = ''
|
user_id = ''
|
||||||
|
@ -45,9 +69,7 @@ async def check_auth(req):
|
||||||
}
|
}
|
||||||
|
|
||||||
gql = {
|
gql = {
|
||||||
'query': f'query {operation}($params: ValidateJWTTokenInput!) {{'
|
'query': f'query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}',
|
||||||
+ f'{query_name}(params: $params) {{ is_valid claims }} '
|
|
||||||
+ '}',
|
|
||||||
'variables': variables,
|
'variables': variables,
|
||||||
'operationName': operation,
|
'operationName': operation,
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ def before_cursor_execute(conn, cursor, statement, parameters, context, executem
|
||||||
@event.listens_for(Engine, 'after_cursor_execute')
|
@event.listens_for(Engine, 'after_cursor_execute')
|
||||||
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
||||||
total = time.time() - conn.info['query_start_time'].pop(-1)
|
total = time.time() - conn.info['query_start_time'].pop(-1)
|
||||||
logger.debug(f' Finished in {total*1000} ms ')
|
logger.debug(f' Finished in {total*1000} s ')
|
||||||
|
|
||||||
|
|
||||||
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
|
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user