mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
feat(rate-limit): 实现分层 RPM 限速,支持系统默认/用户/独立Key三级配置
- 新增用户级 rate_limit 字段,支持系统默认/用户自定义/不限制三种模式 - 独立 Key 的 rate_limit 语义调整:null=跟随系统默认,0=不限制,>0=自定义 - 实现 UserRpmLimiter 基于 Redis sliding window 的 RPM 限速引擎 - Pipeline 请求流程集成用户级 RPM 检查 - 管理后台和用户面板新增 RPM 限速配置与实时状态查看 - 系统设置新增全局默认 RPM 配置项 - 迁移脚本回填现有 API Key 的 rate_limit 默认值 - 新增用户/Key RPM 状态监控 API 和前端展示 Closes #231 Co-authored-by: LewisPen <LewisPen@nyadoo.com>
This commit is contained in:
@@ -692,6 +692,11 @@ class UpdateUserRequest(BaseModel):
|
||||
allowed_providers: list[str] | None = Field(None, description="允许使用的提供商 ID 列表")
|
||||
allowed_api_formats: list[str] | None = Field(None, description="允许使用的 API 格式列表")
|
||||
allowed_models: list[str] | None = Field(None, description="允许使用的模型名称列表")
|
||||
rate_limit: int | None = Field(
|
||||
None,
|
||||
ge=0,
|
||||
description="每分钟请求限制;null 表示继承系统默认,0 表示不限制",
|
||||
)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
|
||||
@@ -252,6 +252,11 @@ class CreateUserRequest(BaseModel):
|
||||
allowed_models: list[str] | None = Field(
|
||||
default=None, description="允许使用的模型名称列表,null表示无限制"
|
||||
)
|
||||
rate_limit: int | None = Field(
|
||||
default=None,
|
||||
ge=0,
|
||||
description="每分钟请求限制;null 表示继承系统默认,0 表示不限制",
|
||||
)
|
||||
|
||||
@field_validator("initial_gift_usd", mode="before")
|
||||
@classmethod
|
||||
@@ -335,6 +340,11 @@ class UpdateUserRequest(BaseModel):
|
||||
allowed_providers: list[str] | None = None # 允许使用的提供商 ID 列表
|
||||
allowed_api_formats: list[str] | None = None # 允许使用的 API 格式列表
|
||||
allowed_models: list[str] | None = None # 允许使用的模型名称列表
|
||||
rate_limit: int | None = Field(
|
||||
default=None,
|
||||
ge=0,
|
||||
description="每分钟请求限制;null 表示继承系统默认,0 表示不限制",
|
||||
)
|
||||
is_active: bool | None = None
|
||||
|
||||
@field_validator("allowed_api_formats")
|
||||
@@ -351,7 +361,11 @@ class CreateApiKeyRequest(BaseModel):
|
||||
allowed_providers: list[str] | None = None # 允许使用的提供商 ID 列表
|
||||
allowed_api_formats: list[str] | None = None # 允许使用的 API 格式列表
|
||||
allowed_models: list[str] | None = None # 允许使用的模型名称列表
|
||||
rate_limit: int | None = None # None = 无限制
|
||||
rate_limit: int | None = Field(
|
||||
None,
|
||||
ge=0,
|
||||
description="每分钟请求限制;独立Key: null=继承系统默认,0=不限制;普通Key: 0=不限制",
|
||||
)
|
||||
expire_days: int | None = None # None = 永不过期,数字 = 多少天后过期
|
||||
expires_at: str | None = None # ISO 日期字符串,如 "2025-12-31",优先于 expire_days
|
||||
initial_balance_usd: float | None = Field(
|
||||
@@ -382,6 +396,7 @@ class UserResponse(BaseModel):
|
||||
allowed_providers: list[str] | None = None # 允许使用的提供商 ID 列表
|
||||
allowed_api_formats: list[str] | None = None # 允许使用的 API 格式列表
|
||||
allowed_models: list[str] | None = None # 允许使用的模型名称列表
|
||||
rate_limit: int | None = None
|
||||
unlimited: bool = False
|
||||
is_active: bool
|
||||
created_at: datetime
|
||||
@@ -402,7 +417,7 @@ class ApiKeyResponse(BaseModel):
|
||||
total_cost_usd: float
|
||||
allowed_providers: list[str] | None
|
||||
allowed_models: list[str] | None
|
||||
rate_limit: int
|
||||
rate_limit: int | None
|
||||
is_active: bool
|
||||
expires_at: datetime | None = None
|
||||
is_standalone: bool = False
|
||||
@@ -772,6 +787,18 @@ class CreateMyApiKeyRequest(BaseModel):
|
||||
"""创建我的API密钥请求"""
|
||||
|
||||
name: str
|
||||
rate_limit: int = Field(0, ge=0, description="该 Key 的每分钟请求限制,0 表示不限制")
|
||||
|
||||
|
||||
class UpdateMyApiKeyRequest(BaseModel):
|
||||
"""更新我的 API 密钥请求"""
|
||||
|
||||
name: str | None = None
|
||||
rate_limit: int | None = Field(
|
||||
None,
|
||||
ge=0,
|
||||
description="该 Key 的每分钟请求限制;0 表示不限制,null 表示不修改",
|
||||
)
|
||||
|
||||
|
||||
class ProviderConfig(BaseModel):
|
||||
|
||||
@@ -110,6 +110,9 @@ class User(Base):
|
||||
allowed_providers = Column(JSON, nullable=True) # 允许使用的提供商 ID 列表
|
||||
allowed_api_formats = Column(JSON, nullable=True) # 允许使用的 API 格式列表
|
||||
allowed_models = Column(JSON, nullable=True) # 允许使用的模型名称列表
|
||||
rate_limit = Column(
|
||||
Integer, nullable=True, default=None
|
||||
) # 每分钟请求限制,NULL=继承系统默认,0=不限制,N=N RPM
|
||||
|
||||
# Key 能力配置
|
||||
model_capability_settings = Column(JSON, nullable=True) # 用户针对特定模型的能力配置
|
||||
@@ -209,7 +212,9 @@ class ApiKey(Base):
|
||||
allowed_providers = Column(JSON, nullable=True) # 允许使用的提供商 ID 列表
|
||||
allowed_api_formats = Column(JSON, nullable=True) # 允许使用的 API 格式列表
|
||||
allowed_models = Column(JSON, nullable=True) # 允许使用的模型名称列表
|
||||
rate_limit = Column(Integer, default=None, nullable=True) # 每分钟请求限制,None = 无限制
|
||||
rate_limit = Column(
|
||||
Integer, default=None, nullable=True
|
||||
) # 每分钟请求限制;独立Key: NULL=继承系统默认,普通Key: 0=不限制
|
||||
concurrent_limit = Column(Integer, default=5, nullable=True) # 并发请求限制
|
||||
|
||||
# Key 能力配置
|
||||
|
||||
Reference in New Issue
Block a user