feat: 实现日志追踪链路 #185

Merged
huanghaosheng merged 30 commits from feature/log-track into develop 2026-06-21 23:23:23 +08:00
Showing only changes of commit edc66625ba - Show all commits

View File

@@ -7,6 +7,7 @@ import (
"time"
"github.com/hhs/camtalk/internal/config"
"github.com/hhs/camtalk/internal/trace"
"github.com/redis/go-redis/v9"
)
@@ -72,6 +73,7 @@ func NewRedisLimiter(client *redis.Client, cfg config.RateLimitConfig) *RedisLim
// Allow 实现 Limiter 接口。
func (l *RedisLimiter) Allow(ctx context.Context, key string) (bool, time.Duration) {
log := trace.FromContext(ctx)
cfg := l.getBucketConfig(key)
now := float64(time.Now().UnixNano()) / 1e9 // 秒,浮点
@@ -81,6 +83,7 @@ func (l *RedisLimiter) Allow(ctx context.Context, key string) (bool, time.Durati
cfg.Capacity, cfg.Rate, now, ttl).Result()
if err != nil {
log.Errorw("rate limit check failed", "key", key, "error", err)
// Redis 错误时降级允许请求fail-open 策略)
return true, 0
}
@@ -100,6 +103,7 @@ func (l *RedisLimiter) Allow(ctx context.Context, key string) (bool, time.Durati
}
retryAfter := time.Duration(retryAfterSec*1000) * time.Millisecond
log.Warnw("rate limit triggered", "key", key, "retry_after_sec", retryAfterSec)
return false, retryAfter
}