From 2d1c50337bfc3f83cf3c7a28860a5f9676c3831c Mon Sep 17 00:00:00 2001 From: kratos Date: Mon, 29 Apr 2024 15:16:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=88=86=E9=92=9F?= =?UTF-8?q?=E9=99=90=E5=88=B6=E5=92=8C=E6=AF=8F=E6=97=A5=E9=99=90=E5=88=B6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E4=B8=80=E4=B8=AA=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题1:日最大限制未达到,但分钟计数达到限制时,此时短信发送异常,但日限制计数已经+1 此问题可能会导致分钟限制触发,分钟内多次请求发送短信,导致日限额超过,但实际未收到短信 问题2:日志打印,引起误导,应该是手误复制后忘记改动 --- .../proxy/processor/RestrictedProcessor.java | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java b/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java index d7eb50e2..21f187ca 100644 --- a/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java +++ b/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java @@ -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); } } }