new version
This commit is contained in:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user