mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善日志
This commit is contained in:
@@ -65,8 +65,16 @@ public class RateLimitFilter extends OncePerRequestFilter {
|
||||
|
||||
|
||||
RateLimiterService.RateLimitAcquireResult acquireSessionRL = rateLimiterService.acquireSessionRL(sessionId);
|
||||
|
||||
|
||||
if (acquireSessionRL != RateLimiterService.RateLimitAcquireResult.SUCCESS ) {
|
||||
sysLogService.saveSysLog(request, "限流模块", "会话限流", acquireSessionRL.name());
|
||||
|
||||
if (acquireSessionRL == RateLimiterService.RateLimitAcquireResult.ERROR){
|
||||
writeResponse(new BizException("invalidToken","请重新登录"), response);
|
||||
return;
|
||||
}
|
||||
|
||||
writeResponse(new BizException("当前会话请求被限流,请稍后再试"), response);
|
||||
log.warn(
|
||||
"会话限流: sessionId {} user {} request {} type {}",
|
||||
@@ -80,6 +88,10 @@ public class RateLimitFilter extends OncePerRequestFilter {
|
||||
RateLimiterService.RateLimitAcquireResult acquireUserRL = rateLimiterService.acquireUserRL(Ctx.currentUser().getId());
|
||||
if (acquireUserRL != RateLimiterService.RateLimitAcquireResult.SUCCESS ) {
|
||||
sysLogService.saveSysLog(request, "限流模块", "用户限流", acquireSessionRL.name());
|
||||
if (acquireUserRL == RateLimiterService.RateLimitAcquireResult.ERROR){
|
||||
writeResponse(new BizException("invalidToken","请重新登录"), response);
|
||||
return;
|
||||
}
|
||||
writeResponse(new BizException("当前会话请求被限流,请稍后再试"), response);
|
||||
log.warn(
|
||||
"用户限流: sessionId {} user {} request {} type {}",
|
||||
@@ -100,7 +112,10 @@ public class RateLimitFilter extends OncePerRequestFilter {
|
||||
|
||||
if (acquire != RateLimiterService.RateLimitAcquireResult.SUCCESS) {
|
||||
sysLogService.saveSysLog(request, "限流模块", "IP限流", acquire.name());
|
||||
|
||||
if (acquire == RateLimiterService.RateLimitAcquireResult.ERROR){
|
||||
writeResponse(new BizException("invalidToken","请重新登录"), response);
|
||||
return;
|
||||
}
|
||||
writeResponse(new BizException("请求被限流,请稍后再试"), response);
|
||||
log.warn("ip {} request {} type {} is rate limited", ip, request.getRequestURI(), acquire.name());
|
||||
return;
|
||||
|
||||
@@ -248,7 +248,7 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
|
||||
rateLimiterService.destroyRL(sessionId, loginUser.getUser().getId());
|
||||
this.repository.deleteById(sessionId);
|
||||
} catch (Exception e) {
|
||||
|
||||
log.warn("删除会话失败 {}",sessionId, e);
|
||||
}
|
||||
|
||||
this.clearCache(sessionId);
|
||||
|
||||
@@ -70,6 +70,8 @@ public class RateLimiterService {
|
||||
throw new RuntimeException("ip is empty");
|
||||
}
|
||||
|
||||
try{
|
||||
|
||||
RRateLimiter rateLimiter =
|
||||
redissonClient.getRateLimiter(RATE_LIMIT_GLOBAL_ANONYMOUS_MIN_PREFIX + ip);
|
||||
|
||||
@@ -92,6 +94,10 @@ public class RateLimiterService {
|
||||
}
|
||||
|
||||
return RateLimitAcquireResult.SUCCESS;
|
||||
}catch (Exception e){
|
||||
log.warn("获取IP限流失败", e);
|
||||
return RateLimitAcquireResult.ERROR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -104,6 +110,8 @@ public class RateLimiterService {
|
||||
RRateLimiter rateLimiter =
|
||||
redissonClient.getRateLimiter(RATE_LIMIT_GLOBAL_SESSION_MIN_PREFIX + session);
|
||||
|
||||
try{
|
||||
|
||||
if (!rateLimiter.tryAcquire(1)) {
|
||||
return RateLimitAcquireResult.MIN;
|
||||
}
|
||||
@@ -123,6 +131,10 @@ public class RateLimiterService {
|
||||
}
|
||||
return RateLimitAcquireResult.SUCCESS;
|
||||
|
||||
}catch (Exception e){
|
||||
log.warn("获取会话限流失败", e);
|
||||
return RateLimitAcquireResult.ERROR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -132,6 +144,9 @@ public class RateLimiterService {
|
||||
throw new RuntimeException("userId is empty");
|
||||
}
|
||||
|
||||
try{
|
||||
|
||||
|
||||
RRateLimiter rateLimiter =
|
||||
redissonClient.getRateLimiter(RATE_LIMIT_GLOBAL_USER_MIN_PREFIX + userId);
|
||||
|
||||
@@ -154,6 +169,10 @@ public class RateLimiterService {
|
||||
}
|
||||
|
||||
return RateLimitAcquireResult.SUCCESS;
|
||||
}catch (Exception e){
|
||||
log.warn("获取用户限流失败", e);
|
||||
return RateLimitAcquireResult.ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
public void initRL(String sessionId, String userId) {
|
||||
@@ -242,6 +261,8 @@ public class RateLimiterService {
|
||||
throw new RuntimeException("userId is empty");
|
||||
}
|
||||
|
||||
log.info("删除会话限流 session: {} user: {}", sessionId, userId);
|
||||
|
||||
try {
|
||||
|
||||
RRateLimiter sessionMinRL =
|
||||
@@ -300,6 +321,7 @@ public class RateLimiterService {
|
||||
|
||||
public enum RateLimitAcquireResult {
|
||||
SUCCESS,
|
||||
ERROR,
|
||||
MIN, HOUR, DAY,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user