将ProjectConfig加入Architecture

This commit is contained in:
2025-11-07 23:47:33 +08:00
parent c49f55808e
commit 9899387697
8 changed files with 56 additions and 41 deletions

View File

@@ -4,9 +4,11 @@ from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
config = ProjectConfig()
MAX_CONCURRENT_REQUESTS = config.FindItem("max_concurrent_requests", 100)
logger: ProjectConfig = Architecture.Get(ProjectConfig)
MAX_CONCURRENT_REQUESTS = logger.FindItem("max_concurrent_requests", 100)
logger.SaveProperties()
class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
"""并发限制中间件 - 防止内存爆炸"""
@@ -15,7 +17,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
super().__init__(app)
self.semaphore = asyncio.Semaphore(max_concurrent)
self.max_concurrent = max_concurrent
config.Log("Info", f"并发限制中间件已启用,最大并发数:{max_concurrent}")
logger.Log("Info", f"并发限制中间件已启用,最大并发数:{max_concurrent}")
async def dispatch(self, request: Request, call_next) -> Response:
"""处理请求"""
@@ -24,7 +26,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
response = await call_next(request)
return response
except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}")
logger.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}")
return Response(
content='{"error": "Internal Server Error"}',
status_code=500,