new version

This commit is contained in:
Ivan Rezchikov
2026-07-11 19:04:41 +03:00
parent 3c64666299
commit b7334696e4
16 changed files with 313 additions and 269 deletions
@@ -25,6 +25,23 @@ var (
Max: 5,
Window: time.Hour,
}
TokenRateLimit = RateLimitConfig{
Max: 20,
Window: time.Minute,
BlockFor: 5 * time.Minute,
}
RevokeRateLimit = RateLimitConfig{
Max: 30,
Window: time.Minute,
}
RefreshRateLimit = RateLimitConfig{
Max: 30,
Window: time.Minute,
BlockFor: 5 * time.Minute,
}
)
type RateLimiter struct {
@@ -42,7 +59,7 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
if cfg.BlockFor > 0 {
blocked, err := r.redis.Exists(ctx, blockKey).Result()
if err != nil {
return true, 0, nil
return false, 0, fmt.Errorf("rate limiter: %w", err)
}
if blocked > 0 {
return false, 0, nil
@@ -53,7 +70,7 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
incrCmd := pipe.Incr(ctx, counterKey)
pipe.Expire(ctx, counterKey, cfg.Window)
if _, err := pipe.Exec(ctx); err != nil {
return true, 0, nil
return false, 0, fmt.Errorf("rate limiter: %w", err)
}
count := int(incrCmd.Val())