parent
f3c92a0c0f
commit
82174aa6c6
@ -0,0 +1,90 @@
|
||||
package com.mashibing.strategy.service.strategyfilter.impl;
|
||||
|
||||
import com.mashibing.common.constant.CacheConstant;
|
||||
import com.mashibing.common.enums.ExceptionEnums;
|
||||
import com.mashibing.common.exception.StrategyException;
|
||||
import com.mashibing.common.pojo.StandardSubmit;
|
||||
import com.mashibing.strategy.feignclient.CacheClient;
|
||||
import com.mashibing.strategy.service.strategyfilter.StrategyFilter;
|
||||
import com.mashibing.strategy.utils.StrategyCheckFailedUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.time.ZoneOffset.UTC;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: LimitHourStrategyFilter
|
||||
* @Description: TODO(这里用一句话描述这个类的作用)
|
||||
* @date 2025/6/10 21:14
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Service(value = "limitHour")
|
||||
public class LimitHourStrategyFilter implements StrategyFilter {
|
||||
|
||||
@Autowired
|
||||
CacheClient cacheClient;
|
||||
|
||||
@Autowired
|
||||
StrategyCheckFailedUtil strategyCheckFailedUtil;
|
||||
|
||||
private final long LIMIT_DURATION = 60 * 60 * 1000 - 1;
|
||||
|
||||
private final int LIMIT_COUNT = 3;
|
||||
|
||||
private static final String STRATEGY_NAME = "小时限流";
|
||||
|
||||
private static final int RETRY_MAX = 2;
|
||||
|
||||
@Override
|
||||
public void strategy(StandardSubmit submit) {
|
||||
if (submit.getState() != 0) {
|
||||
return;
|
||||
}
|
||||
log.info("【策略模块-小时限流校验】开始====================================");
|
||||
Long clientId = submit.getClientId();
|
||||
String mobile = submit.getMobile();
|
||||
long sendTimeMilli = submit.getSendTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
||||
String key = CacheConstant.LIMIT_HOUR + clientId + CacheConstant.COLON + mobile;
|
||||
boolean isSucceed = cacheClient.zAdd(key, String.valueOf(sendTimeMilli), sendTimeMilli);
|
||||
|
||||
//插入失败,开始重试
|
||||
int retryCount = 0;
|
||||
if (!isSucceed) {
|
||||
|
||||
while (retryCount < RETRY_MAX) {
|
||||
retryCount++;
|
||||
if (cacheClient.zAdd(key, String.valueOf(sendTimeMilli + retryCount), sendTimeMilli)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
//三次插入都失败,直接报错
|
||||
if (retryCount == RETRY_MAX) {
|
||||
log.error("【策略模块-小时限流校验】达到限流阈值,校验失败!!!");
|
||||
strategyCheckFailedUtil.smsSendLog(submit, STRATEGY_NAME);
|
||||
strategyCheckFailedUtil.smsPushReport(submit, STRATEGY_NAME);
|
||||
throw new StrategyException(ExceptionEnums.LIMIT_HOUR);
|
||||
}
|
||||
}
|
||||
|
||||
//插入成功或者重试成功,开始滑动时间窗口检验
|
||||
Set<Object> counts = cacheClient.zRangeByScore(key, sendTimeMilli - LIMIT_DURATION, sendTimeMilli);
|
||||
if (counts != null && counts.size() > LIMIT_COUNT) {
|
||||
log.error("【策略模块-小时限流校验】达到限流阈值,校验失败!!!");
|
||||
//插入成功但发送失败,需要删除
|
||||
cacheClient.zRemove(key, String.valueOf(sendTimeMilli + retryCount));
|
||||
strategyCheckFailedUtil.smsSendLog(submit, STRATEGY_NAME);
|
||||
strategyCheckFailedUtil.smsPushReport(submit, STRATEGY_NAME);
|
||||
throw new StrategyException(ExceptionEnums.LIMIT_HOUR);
|
||||
}
|
||||
|
||||
//插入成功,并且没有达到限流阈值
|
||||
log.info("【策略模块-小时限流校验】小时限流校验通过!!!");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue