Skip to content

Commit cc17288

Browse files
committed
refactor(ai-gateway): _TIER_RANK 单源化消除重复定义,nginx 限流双层设计加注释说明
1 parent 5593777 commit cc17288

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎server/ai-gateway/config/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
ALIYUN_ACCESS_KEY_SECRET,
2525
AI_PROVIDERS,
2626
MODEL_ROUTING,
27+
_TIER_RANK, # 单源 tier 优先级(rate_limit 等消费方从此导入,避免重复定义)
2728
get_provider_for_feature,
2829
)
2930
from config.fallback import ( # noqa: F401
@@ -46,6 +47,7 @@
4647
"ALIYUN_ACCESS_KEY_SECRET",
4748
"AI_PROVIDERS",
4849
"MODEL_ROUTING",
50+
"_TIER_RANK",
4951
"get_provider_for_feature",
5052
"PROVIDER_FALLBACK_CHAIN",
5153
"_resolve_model_name",

‎server/ai-gateway/middleware/rate_limit.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from starlette.middleware.base import BaseHTTPMiddleware
1919

2020
from cache.redis_cache import get_cache
21-
from config import RATE_LIMITS, TIMEOUT_CONFIG
21+
from config import RATE_LIMITS, TIMEOUT_CONFIG, _TIER_RANK
2222

2323
logger = logging.getLogger(__name__)
2424

@@ -42,8 +42,8 @@
4242

4343
DEFAULT_TIER = "free"
4444

45-
# Tier 优先级(模块级常量,避免每次调用重新创建字典)
46-
_TIER_RANK = {"free": 0, "observer": 1, "active": 2, "pro": 3, "core": 4, "lifetime": 5}
45+
# Tier 优先级:单源定义于 config.providers(2026-08 R5 消除重复定义),
46+
# 此处从 config 导入,与 providers.get_effective_tier 共享同一 rank 表。
4747

4848

4949
def get_tier_limits(beta_tier: str | None = None, paid_tier: str | None = None) -> dict:

‎server/nginx/nginx.conf‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ http {
2525
gzip_min_length 1000;
2626

2727
# Rate limiting
28+
# 双层限流说明(2026-08 审计复核):nginx 层(本 zone)按 IP 防洪泛,
29+
# 网关内 Redis 滑动窗口按账号 tier 配额(15-120 次/天)。两层互补:
30+
# nginx 30r/m 只拦截单 IP 突发刷量(正常用户每日调用远低于此),
31+
# 不会误伤;账号级每日配额由网关精确执行(nginx 不感知 tier)。
2832
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/m;
2933
limit_req_zone $binary_remote_addr zone=ai:10m rate=30r/m;
3034
# 安装包下载并发限制(按 IP),避免大文件下载挤占 API 带宽

0 commit comments

Comments
 (0)