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:
fawney19
2026-03-15 14:22:59 +08:00
parent 920a383136
commit f92b0943b5
35 changed files with 2051 additions and 238 deletions

View File

@@ -1,6 +1,13 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { usersApi, type User, type CreateUserRequest, type UpdateUserRequest, type ApiKey } from '@/api/users'
import {
usersApi,
type User,
type CreateUserRequest,
type UpdateUserRequest,
type ApiKey,
type UpsertUserApiKeyRequest
} from '@/api/users'
import { parseApiError } from '@/utils/errorParser'
export const useUsersStore = defineStore('users', () => {
@@ -84,15 +91,28 @@ export const useUsersStore = defineStore('users', () => {
}
}
async function createApiKey(userId: string, name?: string): Promise<ApiKey> {
async function createApiKey(userId: string, data: UpsertUserApiKeyRequest): Promise<ApiKey> {
try {
return await usersApi.createApiKey(userId, name)
return await usersApi.createApiKey(userId, data)
} catch (err: unknown) {
error.value = parseApiError(err, '创建 API Key 失败')
throw err
}
}
async function updateApiKey(
userId: string,
keyId: string,
data: UpsertUserApiKeyRequest
): Promise<ApiKey> {
try {
return await usersApi.updateApiKey(userId, keyId, data)
} catch (err: unknown) {
error.value = parseApiError(err, '更新 API Key 失败')
throw err
}
}
async function deleteApiKey(userId: string, keyId: string) {
try {
await usersApi.deleteApiKey(userId, keyId)
@@ -121,6 +141,7 @@ export const useUsersStore = defineStore('users', () => {
deleteUser,
getUserApiKeys,
createApiKey,
updateApiKey,
deleteApiKey,
getFullApiKey
}