feat: 实现日志追踪链路 #185
@@ -805,8 +805,9 @@ func (r *CachedUserRepository) DeleteRefreshToken(ctx, tokenHash) error {
|
|||||||
```
|
```
|
||||||
|
|
||||||
**降级策略**:
|
**降级策略**:
|
||||||
- Redis 操作失败时记录日志,但不阻断主流程
|
- Redis 操作失败时使用 `trace.FromContext(ctx)` 记录 Warn 日志(带 trace_id),但不阻断主流程
|
||||||
- DB 是唯一真实数据源,Redis 仅用于加速
|
- DB 是唯一真实数据源,Redis 仅用于加速
|
||||||
|
- 详见 `docs/13-日志追踪.md` — 存储层日志实现
|
||||||
|
|
||||||
### 6. Gin 中间件实现
|
### 6. Gin 中间件实现
|
||||||
|
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ if cfg.RateLimit.Enabled {
|
|||||||
logger.Log.Info("rate limiter initialized with Redis backend")
|
logger.Log.Info("rate limiter initialized with Redis backend")
|
||||||
} else {
|
} else {
|
||||||
// 单实例:使用内存令牌桶
|
// 单实例:使用内存令牌桶
|
||||||
limiter = ratelimit.NewLimiter(cfg.RateLimit)
|
limiter = ratelimit.NewMemoryLimiter(cfg.RateLimit)
|
||||||
logger.Log.Info("rate limiter initialized with in-memory backend")
|
logger.Log.Info("rate limiter initialized with in-memory backend")
|
||||||
}
|
}
|
||||||
defer limiter.Stop()
|
defer limiter.Stop()
|
||||||
@@ -590,9 +590,9 @@ case "query":
|
|||||||
// 限流检查
|
// 限流检查
|
||||||
if limiter != nil {
|
if limiter != nil {
|
||||||
key := fmt.Sprintf("%s:query", userID)
|
key := fmt.Sprintf("%s:query", userID)
|
||||||
allowed, retryAfter := limiter.Allow(context.Background(), key)
|
allowed, retryAfter := limiter.Allow(ctx, key)
|
||||||
if !allowed {
|
if !allowed {
|
||||||
logger.Log.Warnw("rate limited", "user_id", userID, "retry_after", retryAfter)
|
// 限流触发时自动记录 Warn 日志(在 limiter 内部使用 trace.FromContext)
|
||||||
errors.SendWSError(client, errors.CodeRateLimited, msg.RequestID,
|
errors.SendWSError(client, errors.CodeRateLimited, msg.RequestID,
|
||||||
fmt.Errorf("rate limited, retry after %s", retryAfter.Round(time.Second)))
|
fmt.Errorf("rate limited, retry after %s", retryAfter.Round(time.Second)))
|
||||||
continue
|
continue
|
||||||
@@ -605,7 +605,7 @@ case "query":
|
|||||||
**设计要点**:
|
**设计要点**:
|
||||||
- key 格式:`userID:query`(用户级限流)
|
- key 格式:`userID:query`(用户级限流)
|
||||||
- 拒绝时发送 `RATE_LIMITED` 错误到客户端
|
- 拒绝时发送 `RATE_LIMITED` 错误到客户端
|
||||||
- 记录警告日志(便于监控告警)
|
- 限流触发时 `RedisLimiter.Allow` 内部自动记录 Warn 日志(带 trace_id,详见 `docs/13-日志追踪.md`)
|
||||||
- 不阻塞其他消息类型(`ping`/`config`/`interrupt` 不限流)
|
- 不阻塞其他消息类型(`ping`/`config`/`interrupt` 不限流)
|
||||||
|
|
||||||
### 配置加载与依赖注入
|
### 配置加载与依赖注入
|
||||||
|
|||||||
Reference in New Issue
Block a user