Commit 4286f16974fc7938bcdd370975c1210008463ed6
1 parent
0fb8f683
fix: RateIntervalUnit 已过期
Showing
1 changed file
with
8 additions
and
4 deletions
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/ratelimiter/core/redis/RateLimiterRedisDAO.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.ratelimiter.core.redis; |
| 3 | 3 | import lombok.AllArgsConstructor; |
| 4 | 4 | import org.redisson.api.*; |
| 5 | 5 | |
| 6 | +import java.time.Duration; | |
| 6 | 7 | import java.util.Objects; |
| 7 | 8 | import java.util.concurrent.TimeUnit; |
| 8 | 9 | |
| ... | ... | @@ -40,11 +41,13 @@ public class RateLimiterRedisDAO { |
| 40 | 41 | String redisKey = formatKey(key); |
| 41 | 42 | RRateLimiter rateLimiter = redissonClient.getRateLimiter(redisKey); |
| 42 | 43 | long rateInterval = timeUnit.toSeconds(time); |
| 44 | + Duration duration = Duration.ofSeconds(rateInterval); | |
| 43 | 45 | // 1. 如果不存在,设置 rate 速率 |
| 44 | 46 | RateLimiterConfig config = rateLimiter.getConfig(); |
| 45 | 47 | if (config == null) { |
| 46 | - rateLimiter.trySetRate(RateType.OVERALL, count, rateInterval, RateIntervalUnit.SECONDS); | |
| 47 | - rateLimiter.expire(rateInterval, TimeUnit.SECONDS); // 原因参见 https://t.zsxq.com/lcR0W | |
| 48 | + rateLimiter.trySetRate(RateType.OVERALL, count, duration); | |
| 49 | + // 原因参见 https://t.zsxq.com/lcR0W | |
| 50 | + rateLimiter.expire(duration); | |
| 48 | 51 | return rateLimiter; |
| 49 | 52 | } |
| 50 | 53 | // 2. 如果存在,并且配置相同,则直接返回 |
| ... | ... | @@ -54,8 +57,9 @@ public class RateLimiterRedisDAO { |
| 54 | 57 | return rateLimiter; |
| 55 | 58 | } |
| 56 | 59 | // 3. 如果存在,并且配置不同,则进行新建 |
| 57 | - rateLimiter.setRate(RateType.OVERALL, count, rateInterval, RateIntervalUnit.SECONDS); | |
| 58 | - rateLimiter.expire(rateInterval, TimeUnit.SECONDS); // 原因参见 https://t.zsxq.com/lcR0W | |
| 60 | + rateLimiter.setRate(RateType.OVERALL, count, duration); | |
| 61 | + // 原因参见 https://t.zsxq.com/lcR0W | |
| 62 | + rateLimiter.expire(duration); | |
| 59 | 63 | return rateLimiter; |
| 60 | 64 | } |
| 61 | 65 | ... | ... |