fix: 解决分钟限制和每日限制存在的一个计数问题

问题1:日最大限制未达到,但分钟计数达到限制时,此时短信发送异常,但日限制计数已经+1
此问题可能会导致分钟限制触发,分钟内多次请求发送短信,导致日限额超过,但实际未收到短信
问题2:日志打印,引起误导,应该是手误复制后忘记改动
This commit is contained in:
kratos 2024-04-29 15:16:30 +08:00
parent a5f7ba37a4
commit 2d1c50337b

View File

@ -64,39 +64,57 @@ public class RestrictedProcessor implements CoreMethodProcessor, SmsDaoAware {
throw new SmsBlendException("The smsDao tool could not be found"); throw new SmsBlendException("The smsDao tool could not be found");
} }
SmsConfig config = BeanFactory.getSmsConfig(); SmsConfig config = BeanFactory.getSmsConfig();
// 如果未开始限制则不做处理
if (!config.getRestricted()){
return;
}
// 每日最大发送量 // 每日最大发送量
Integer accountMax = config.getAccountMax(); Integer accountMax = config.getAccountMax();
// 每分钟最大发送量 // 每分钟最大发送量
Integer minuteMax = config.getMinuteMax(); 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) { for (String phone : phones) {
// 分钟发送量缓存key
String minuteMaxKey = REDIS_KEY + phone;
// 天发送量缓存key
String accountMaxKey = minuteMaxKey.concat("max");
// 是否配置了每日限制 // 是否配置了每日限制
if (SmsUtils.isNotEmpty(accountMax)) { if (dailyMaxLimitExists) {
Integer i = (Integer) smsDao.get(REDIS_KEY + phone + "max"); Integer dailyCount = (Integer) smsDao.get(accountMaxKey);
if (SmsUtils.isEmpty(i)) { if (SmsUtils.isEmpty(dailyCount)) {
smsDao.set(REDIS_KEY + phone + "max", 1, accTimer / 1000); smsDao.set(accountMaxKey, 1, accTimer / 1000);
} else if (i >= accountMax) { } else if (dailyCount >= accountMax) {
log.info("The phone: {},number of short messages reached the maximum today", phone); 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); throw new SmsBlendException("The phone: {},number of short messages reached the maximum today", phone);
} else { } else {
smsDao.set(REDIS_KEY + phone + "max", i + 1, accTimer / 1000); smsDao.set(accountMaxKey, dailyCount + 1, accTimer / 1000);
} }
} }
// 是否配置了每分钟最大限制 // 是否配置了每分钟最大限制
if (SmsUtils.isNotEmpty(minuteMax)) { if (perMinuteLimitExists) {
Integer o = (Integer) smsDao.get(REDIS_KEY + phone); Integer minuteCount = (Integer) smsDao.get(REDIS_KEY + phone);
if (SmsUtils.isNotEmpty(o)) { if (SmsUtils.isNotEmpty(minuteCount)) {
if (o < minuteMax) { if (minuteCount < minuteMax) {
smsDao.set(REDIS_KEY + phone, o + 1, minTimer / 1000); smsDao.set(minuteMaxKey, minuteCount + 1, minTimer / 1000);
} else { } 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); throw new SmsBlendException("The phone: {} Text messages are sent too often", phone);
} }
} else { } else {
smsDao.set(REDIS_KEY + phone, 1, minTimer / 1000); smsDao.set(minuteMaxKey, 1, minTimer / 1000);
} }
} }
} }