fixed blocking important issues
This commit is contained in:
+1
-14
@@ -5,8 +5,6 @@ import (
|
||||
"time"
|
||||
|
||||
"tm/pkg/redis"
|
||||
|
||||
redisv9 "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Allow returns whether the request is within limit for the given key.
|
||||
@@ -15,21 +13,10 @@ func Allow(ctx context.Context, client redis.Client, key string, limit int64, wi
|
||||
return true, nil
|
||||
}
|
||||
|
||||
count, err := client.Incr(ctx, key)
|
||||
count, err := client.IncrWithExpire(ctx, key, window)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
if err := client.Expire(ctx, key, window); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
return count <= limit, nil
|
||||
}
|
||||
|
||||
// IsNotFound reports whether err indicates a missing Redis key.
|
||||
func IsNotFound(err error) bool {
|
||||
return err == redisv9.Nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user