邮件-调整限流策略
This commit is contained in:
@@ -14,8 +14,13 @@ import org.redisson.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
|
||||
import software.amazon.awssdk.awscore.internal.AwsErrorCode;
|
||||
import software.amazon.awssdk.services.sesv2.model.SendEmailResponse;
|
||||
import software.amazon.awssdk.services.sesv2.model.SesV2Exception;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@@ -53,10 +58,10 @@ public class EmailService extends BaseService {
|
||||
String ipKey = RedisKey.email_send_interval.getKey(ip);
|
||||
rateLimiter = redissonClient.getRateLimiter(ipKey);
|
||||
if (!rateLimiter.isExists()) {
|
||||
rateLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.DAYS);
|
||||
rateLimiter.trySetRate(RateType.OVERALL, 8, 1, RateIntervalUnit.DAYS);
|
||||
}
|
||||
|
||||
if (!rateLimiter.tryAcquire()){
|
||||
if (rateLimiter.availablePermits() <= 0L){
|
||||
log.error("[email rateLimiter] {} {} 被限流了", ip, type);
|
||||
throw new ServiceException(BusiStatus.EMAIL_CODE_IP_TO_OFTEN);
|
||||
}
|
||||
@@ -72,11 +77,17 @@ public class EmailService extends BaseService {
|
||||
|
||||
log.info("[email] to {} code {} responseCode {} responseMsg {}", emailAddress, code, responseCode, responseMsg);
|
||||
|
||||
} catch (Exception e){
|
||||
log.error("[email] error {}", e);
|
||||
if (rateLimiter != null) {
|
||||
rateLimiter.tryAcquire();
|
||||
rateLimiter.expire(Duration.of(1, ChronoUnit.DAYS));
|
||||
}
|
||||
|
||||
responseCode = String.valueOf(BusiStatus.SERVERERROR.getCode());
|
||||
responseMsg = e.getMessage();
|
||||
} catch (SesV2Exception e){
|
||||
log.error("[email] error {}", e.getMessage(), e);
|
||||
|
||||
AwsErrorDetails awsErrorDetails = e.awsErrorDetails();
|
||||
responseCode = awsErrorDetails.errorCode();
|
||||
responseMsg = awsErrorDetails.errorMessage();
|
||||
}
|
||||
|
||||
//新增短信记录
|
||||
@@ -90,10 +101,6 @@ public class EmailService extends BaseService {
|
||||
String codeKey = RedisKey.email_code.getKey(emailAddress);
|
||||
RBucket<String> codeBucket = redissonClient.getBucket(codeKey);
|
||||
codeBucket.set(code, EmailConstant.EXPIRE_MINUTE, TimeUnit.MINUTES);
|
||||
|
||||
if (null != rateLimiter){
|
||||
rateLimiter.expire(1, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean verifyEmailCode(String emailAddress, String code) {
|
||||
|
@@ -28,6 +28,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@@ -60,10 +62,10 @@ public class SmsService extends BaseService {
|
||||
String ipKey = RedisKey.sms_send_interval.getKey(ip);
|
||||
rateLimiter = redissonClient.getRateLimiter(ipKey);
|
||||
if (!rateLimiter.isExists()) {
|
||||
rateLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.DAYS);
|
||||
rateLimiter.trySetRate(RateType.OVERALL, 8, 1, RateIntervalUnit.DAYS);
|
||||
}
|
||||
|
||||
if (!rateLimiter.tryAcquire()){
|
||||
if (rateLimiter.availablePermits() <= 0L){
|
||||
log.error("[sms rateLimiter] {} {} 被限流了", ip, type);
|
||||
throw new ServiceException(BusiStatus.SMS_IP_TO_OFTEN);
|
||||
}
|
||||
@@ -91,6 +93,11 @@ public class SmsService extends BaseService {
|
||||
//响应编码校验
|
||||
smsStrategy.validResCode(resCode);
|
||||
|
||||
if (rateLimiter != null) {
|
||||
rateLimiter.tryAcquire();
|
||||
rateLimiter.expire(Duration.of(1, ChronoUnit.DAYS));
|
||||
}
|
||||
|
||||
} catch (RuntimeException | NumberParseException e) {
|
||||
log.error("sendSmsCode error. mobile:{}", mobile, e);
|
||||
throw new ServiceException(BusiStatus.SMS_SEND_ERROR);
|
||||
@@ -100,10 +107,6 @@ public class SmsService extends BaseService {
|
||||
jedisService.setex(getSmsIntervalKey(mobile, type), SmsConstant.SMS_SEND_INTERVAL_SECONDS, StrUtil.EMPTY);
|
||||
// 写入缓存
|
||||
jedisService.setex(getSmsKey(mobile), SmsConstant.SMS_EXPIRE_SECONDS, code);
|
||||
|
||||
if (null != rateLimiter){
|
||||
rateLimiter.expire(1, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean verifySmsCode(String mobile, String code) {
|
||||
|
Reference in New Issue
Block a user