修复Redis限流中increment概率性重新创建一个无过期时间的新键值对(刚好过期)此时会导致用户永久被封禁(永远返回403或是401)

This commit is contained in:
柏码の讲师 2024-02-22 23:24:16 +08:00
parent 9eff280059
commit 8636be9a48

View File

@ -72,8 +72,12 @@ public class FlowUtils {
* @return 是否通过限流检查
*/
private boolean internalCheck(String key, int frequency, int period, LimitAction action){
if (Boolean.TRUE.equals(template.hasKey(key))) {
Long value = Optional.ofNullable(template.opsForValue().increment(key)).orElse(0L);
String count = template.opsForValue().get(key);
if (count != null) {
long value = Optional.ofNullable(template.opsForValue().increment(key)).orElse(0L);
int c = Integer.parseInt(count);
if(value != c + 1)
template.expire(key, period, TimeUnit.SECONDS);
return action.run(value > frequency);
} else {
template.opsForValue().set(key, "1", period, TimeUnit.SECONDS);