mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
fix(security/limiter): 修复默认限流器名称生成器错误
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package top.continew.starter.cache.redisson.util;
|
package top.continew.starter.cache.redisson.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import org.redisson.api.*;
|
import org.redisson.api.*;
|
||||||
import top.continew.starter.core.constant.StringConstants;
|
import top.continew.starter.core.constant.StringConstants;
|
||||||
@@ -206,25 +207,6 @@ public class RedisUtils {
|
|||||||
return rateLimiter.tryAcquire(1);
|
return rateLimiter.tryAcquire(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 限流
|
|
||||||
*
|
|
||||||
* @param key 限流key
|
|
||||||
* @param rateType 限流类型
|
|
||||||
* @param rate 速率
|
|
||||||
* @param rateInterval 速率间隔
|
|
||||||
* @return -1 表示失败
|
|
||||||
*/
|
|
||||||
public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval, RateIntervalUnit unit) {
|
|
||||||
RRateLimiter rateLimiter = CLIENT.getRateLimiter(key);
|
|
||||||
rateLimiter.trySetRate(rateType, rate, rateInterval, unit);
|
|
||||||
if (rateLimiter.tryAcquire()) {
|
|
||||||
return rateLimiter.availablePermits();
|
|
||||||
} else {
|
|
||||||
return -1L;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化键,将各子键用 : 拼接起来
|
* 格式化键,将各子键用 : 拼接起来
|
||||||
*
|
*
|
||||||
@@ -232,6 +214,6 @@ public class RedisUtils {
|
|||||||
* @return 键
|
* @return 键
|
||||||
*/
|
*/
|
||||||
public static String formatKey(String... subKeys) {
|
public static String formatKey(String... subKeys) {
|
||||||
return String.join(StringConstants.COLON, subKeys);
|
return String.join(StringConstants.COLON, ArrayUtil.removeBlank(subKeys));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -45,9 +45,7 @@ public class DefaultRateLimiterNameGenerator implements RateLimiterNameGenerator
|
|||||||
this.getDescriptor(nameSb, clazz);
|
this.getDescriptor(nameSb, clazz);
|
||||||
}
|
}
|
||||||
nameSb.append(StringConstants.ROUND_BRACKET_END);
|
nameSb.append(StringConstants.ROUND_BRACKET_END);
|
||||||
String str = nameSb.toString();
|
return nameSb.toString();
|
||||||
nameMap.put(method, str);
|
|
||||||
return str;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +69,7 @@ public class DefaultRateLimiterNameGenerator implements RateLimiterNameGenerator
|
|||||||
String name = clazz.getName();
|
String name = clazz.getName();
|
||||||
name = ClassUtil.getShortClassName(name);
|
name = ClassUtil.getShortClassName(name);
|
||||||
sb.append(name);
|
sb.append(name);
|
||||||
sb.append(StringConstants.COLON);
|
sb.append(StringConstants.SEMICOLON);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user