feat: 集成限流器到服务 #178

Merged
huanghaosheng merged 12 commits from develop into v2 2026-06-21 13:25:06 +08:00
5 changed files with 132 additions and 4 deletions
Showing only changes of commit 311330cea1 - Show all commits

2
backend/.gitignore vendored
View File

@@ -4,8 +4,6 @@ bin/
# 环境配置 # 环境配置
.env .env
config.dev.yaml
config.prod.yaml
# 临时文件 # 临时文件
tmp/ tmp/

View File

@@ -0,0 +1,55 @@
# CamTalk 开发环境配置
# 通过 APP_ENV=dev 加载此文件,覆盖 config.yaml 中的配置
server:
host: "0.0.0.0"
port: 8080
heartbeat_interval: 30
heartbeat_timeout: 60
allowed_origins: [] # 开发环境允许所有来源
session:
ttl: 30 # 开发环境会话较短,方便测试过期逻辑
max_history: 20
ai:
stt:
timeout: 10 # 开发环境超时较长,方便调试
http_client_timeout: 30
llm:
timeout: 60 # 开发环境 LLM 超时较长
http_client_timeout: 120
tts:
timeout: 10
http_client_timeout: 30
storage:
redis:
enabled: true # 开发环境启用 Redis测试三级存储
persistence:
enabled: true # 开发环境启用持久化
redis:
addr: "localhost:6379" # 本地 Redis
password: ""
db: 0
auth:
access_ttl: 120 # 开发环境 Access Token 2 小时,方便调试
refresh_ttl: 10080 # 7 天
ratelimit:
enabled: false # 开发环境关闭限流,方便测试
query:
capacity: 10
rate: 0.2
login:
capacity: 5
rate: 0.1
register:
capacity: 3
rate: 0.05
log:
level: debug # 开发环境 debug 日志
format: console # 控制台格式,易读

View File

@@ -0,0 +1,74 @@
# CamTalk 生产环境配置
# 通过 APP_ENV=prod 加载此文件,覆盖 config.yaml 中的配置
server:
host: "0.0.0.0"
port: 8080
read_timeout: 30
write_timeout: 30
shutdown_timeout: 15 # 生产环境优雅关闭时间稍长
heartbeat_interval: 30
heartbeat_timeout: 60
allowed_origins: # 生产环境严格 CORS 白名单
- "https://camtalk.example.com"
- "https://www.camtalk.example.com"
session:
ttl: 60 # 生产环境会话 1 小时
max_history: 20
ai:
stt:
provider: mimo # 生产环境推荐 MiMo性价比高
model: mimo-v2.5-asr
endpoint: "https://api.xiaomimimo.com/v1"
timeout: 5 # 生产环境严格超时控制
http_client_timeout: 30
llm:
provider: dashscope # 生产环境推荐通义千问,稳定性好
model: qwen3-vl-plus
endpoint: "https://dashscope.aliyuncs.com/compatible-mode/v1"
timeout: 30
http_client_timeout: 60
tts:
provider: mimo # 生产环境推荐 MiMo TTS
model: mimo-v2.5-tts
voice: mimo_default
speed: 1.0
endpoint: "https://token-plan-cn.xiaomimimo.com/v1"
timeout: 5
http_client_timeout: 30
output_format: mp3
sample_rate: 24000
storage:
redis:
enabled: true # 生产环境必须启用 Redis
persistence:
enabled: true # 生产环境必须启用持久化
driver: postgres
redis:
addr: "redis:6379" # Docker Compose 内部服务名
password: "" # 密码通过 CAMTALK_REDIS_PASSWORD 环境变量设置
db: 0
auth:
access_ttl: 120 # 生产环境 Access Token 2 小时
refresh_ttl: 10080 # Refresh Token 7 天
ratelimit:
enabled: true # 生产环境启用限流
query:
capacity: 10 # 允许突发 10 个请求
rate: 0.2 # 每 5 秒恢复 1 个令牌
login:
capacity: 5 # 防暴力破解
rate: 0.1 # 每 10 秒恢复 1 次
register:
capacity: 3 # 防批量注册
rate: 0.05 # 每 20 秒恢复 1 次
log:
level: info # 生产环境 info 级别
format: json # JSON 格式,便于日志收集和分析

View File

@@ -136,7 +136,7 @@ type BucketConfig struct {
} }
// Load 加载配置。优先级:环境变量 > config.{env}.yaml > config.yaml > 默认值。 // Load 加载配置。优先级:环境变量 > config.{env}.yaml > config.yaml > 默认值。
// workDir 为项目根目录或 backend 目录,用于定位 .env 和 config.yaml。 // workDir 为项目根目录或 backend 目录,用于定位 .env 和 config/config.yaml。
func Load(workDir string) (*Config, error) { func Load(workDir string) (*Config, error) {
// 1. 加载 .env 文件(敏感信息) // 1. 加载 .env 文件(敏感信息)
envFile := filepath.Join(workDir, ".env") envFile := filepath.Join(workDir, ".env")
@@ -145,7 +145,8 @@ func Load(workDir string) (*Config, error) {
v := viper.New() v := viper.New()
v.SetConfigName("config") v.SetConfigName("config")
v.SetConfigType("yaml") v.SetConfigType("yaml")
v.AddConfigPath(workDir) v.AddConfigPath(filepath.Join(workDir, "config")) // 配置文件在 config/ 目录下
v.AddConfigPath(workDir) // 兼容旧路径
// 2. 设置默认值(与 config.yaml 保持一致,仅作为兜底) // 2. 设置默认值(与 config.yaml 保持一致,仅作为兜底)
setDefaults(v) setDefaults(v)