perf: Sprint 19 P0/P1 性能优化落地
P0(高优先级): - P0-1: 确认数据库复合索引已存在(GORM tag),composite_index_test 验证通过 - P0-2: 连接池调优 MaxIdleConns 5→10, ConnMaxLifetime 30min→5min - P0-3: Redis 智能探测(ProbeRedis),无 Redis 自动降级到纯内存模式 P1(中优先级): - P1-1: GZIP 压缩中间件(compress/gzip 标准库,零新依赖) - P1-2: 权限缓存 TTL 30min→5min - P1-3: Argon2id 启动自适应校准(CalibrateArgon2id) 历史优化(含本次提交): - L1Cache O(n)→O(1) LRU 重构 - Auth 中间件 DB 查询合并 + 5s L1 缓存 - Logger 异步化(4096 缓冲通道) 验证: go build/vet/test 41/41 PASS, govulncheck 无漏洞
This commit is contained in:
@@ -46,6 +46,11 @@ func main() {
|
||||
log.Fatalf("auto migrate failed: %v", err)
|
||||
}
|
||||
|
||||
// P1-3:Argon2id 启动时自适应校准
|
||||
// 在当前机器上测量哈希耗时,超出 500ms 预算则自动降低参数,确保登录接口 P99 < 1000ms。
|
||||
// 此操作仅在启动阶段执行一次,耗时约 1-3s(正常情况下与默认参数一致则跳过)。
|
||||
auth.CalibrateArgon2id(500 * time.Millisecond)
|
||||
|
||||
// 初始化 JWT 管理器
|
||||
jwtManager, err := auth.NewJWTWithOptions(auth.JWTOptions{
|
||||
HS256Secret: cfg.JWT.Secret,
|
||||
@@ -57,9 +62,18 @@ func main() {
|
||||
}
|
||||
|
||||
// 初始化缓存
|
||||
// Redis 智能探测:有 Redis 则启用 L2 分布式缓存,无 Redis 则降级到纯 L1 内存缓存。
|
||||
// 两种模式下系统功能完全等价,区别仅在于多实例场景的缓存共享能力。
|
||||
// 如需禁用 Redis 探测(即使 Redis 可达也不启用),可将配置中 redis.host 留空。
|
||||
l1Cache := cache.NewL1Cache()
|
||||
redisAddr := fmt.Sprintf("%s:%d", cfg.Redis.Host, cfg.Redis.Port)
|
||||
redisEnabled := cfg.Redis.Host != "" && cache.ProbeRedis(redisAddr, cfg.Redis.Password, cfg.Redis.DB)
|
||||
if !redisEnabled {
|
||||
log.Printf("cache: running in memory-only mode (Redis unreachable or not configured)")
|
||||
}
|
||||
l2Cache := cache.NewRedisCacheWithConfig(cache.RedisCacheConfig{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Redis.Host, cfg.Redis.Port),
|
||||
Enabled: redisEnabled,
|
||||
Addr: redisAddr,
|
||||
Password: cfg.Redis.Password,
|
||||
DB: cfg.Redis.DB,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user