mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 08:58:38 +08:00
fix: 解决分钟限制和每日限制存在的一个计数问题
问题1:日最大限制未达到,但分钟计数达到限制时,此时短信发送异常,但日限制计数已经+1 此问题可能会导致分钟限制触发,分钟内多次请求发送短信,导致日限额超过,但实际未收到短信 问题2:日志打印,引起误导,应该是手误复制后忘记改动
This commit is contained in:
parent
a5f7ba37a4
commit
2d1c50337b
@ -64,39 +64,57 @@ public class RestrictedProcessor implements CoreMethodProcessor, SmsDaoAware {
|
||||
throw new SmsBlendException("The smsDao tool could not be found");
|
||||
}
|
||||
SmsConfig config = BeanFactory.getSmsConfig();
|
||||
// 如果未开始限制则不做处理
|
||||
if (!config.getRestricted()){
|
||||
return;
|
||||
}
|
||||
// 每日最大发送量
|
||||
Integer accountMax = config.getAccountMax();
|
||||
// 每分钟最大发送量
|
||||
Integer minuteMax = config.getMinuteMax();
|
||||
// 配置了每日最大发送量
|
||||
boolean dailyMaxLimitExists = SmsUtils.isNotEmpty(accountMax);
|
||||
// 配置了每分钟最大发送量
|
||||
boolean perMinuteLimitExists = SmsUtils.isNotEmpty(minuteMax);
|
||||
// 如果未开启限制或未配置任何限制发送量,不做处理
|
||||
boolean isNoProcessing = !config.getRestricted() || (!dailyMaxLimitExists && !perMinuteLimitExists);
|
||||
if (isNoProcessing) {
|
||||
return;
|
||||
}
|
||||
for (String phone : phones) {
|
||||
// 分钟发送量缓存key
|
||||
String minuteMaxKey = REDIS_KEY + phone;
|
||||
// 天发送量缓存key
|
||||
String accountMaxKey = minuteMaxKey.concat("max");
|
||||
// 是否配置了每日限制
|
||||
if (SmsUtils.isNotEmpty(accountMax)) {
|
||||
Integer i = (Integer) smsDao.get(REDIS_KEY + phone + "max");
|
||||
if (SmsUtils.isEmpty(i)) {
|
||||
smsDao.set(REDIS_KEY + phone + "max", 1, accTimer / 1000);
|
||||
} else if (i >= accountMax) {
|
||||
if (dailyMaxLimitExists) {
|
||||
Integer dailyCount = (Integer) smsDao.get(accountMaxKey);
|
||||
if (SmsUtils.isEmpty(dailyCount)) {
|
||||
smsDao.set(accountMaxKey, 1, accTimer / 1000);
|
||||
} else if (dailyCount >= accountMax) {
|
||||
log.info("The phone: {},number of short messages reached the maximum today", phone);
|
||||
throw new SmsBlendException("The phone: {},number of short messages reached the maximum today", phone);
|
||||
} else {
|
||||
smsDao.set(REDIS_KEY + phone + "max", i + 1, accTimer / 1000);
|
||||
smsDao.set(accountMaxKey, dailyCount + 1, accTimer / 1000);
|
||||
}
|
||||
}
|
||||
// 是否配置了每分钟最大限制
|
||||
if (SmsUtils.isNotEmpty(minuteMax)) {
|
||||
Integer o = (Integer) smsDao.get(REDIS_KEY + phone);
|
||||
if (SmsUtils.isNotEmpty(o)) {
|
||||
if (o < minuteMax) {
|
||||
smsDao.set(REDIS_KEY + phone, o + 1, minTimer / 1000);
|
||||
if (perMinuteLimitExists) {
|
||||
Integer minuteCount = (Integer) smsDao.get(REDIS_KEY + phone);
|
||||
if (SmsUtils.isNotEmpty(minuteCount)) {
|
||||
if (minuteCount < minuteMax) {
|
||||
smsDao.set(minuteMaxKey, minuteCount + 1, minTimer / 1000);
|
||||
} else {
|
||||
log.info("The phone: {},number of short messages reached the maximum today", phone);
|
||||
//如果能走到这里且存在每日限制,说明每日限制已经计数,这里将之前的计数减一次
|
||||
if (dailyMaxLimitExists) {
|
||||
Integer dailyCount = (Integer) smsDao.get(accountMaxKey);
|
||||
if (dailyCount > 1) {
|
||||
smsDao.set(accountMaxKey, dailyCount - 1, accTimer / 1000);
|
||||
} else {
|
||||
smsDao.remove(accountMaxKey);
|
||||
}
|
||||
}
|
||||
log.info("The phone: {} Text messages are sent too often!", phone);
|
||||
throw new SmsBlendException("The phone: {} Text messages are sent too often!", phone);
|
||||
}
|
||||
} else {
|
||||
smsDao.set(REDIS_KEY + phone, 1, minTimer / 1000);
|
||||
smsDao.set(minuteMaxKey, 1, minTimer / 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user