Skip to content

Commit

Permalink
fix: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileSay committed Mar 19, 2024
1 parent 83c0f0b commit a99dbbf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 42 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/coco/boot/interceptor/ChatInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import java.io.IOException;
import java.time.Duration;

import static com.coco.boot.constant.SysConstant.GHU_ALIVE_KEY;
import static com.coco.boot.constant.SysConstant.SYS_USER_ID;
import static com.coco.boot.constant.SysConstant.USING_USER;
import static com.coco.boot.constant.SysConstant.*;

/**
* @author Fengdong.Duan
Expand All @@ -31,17 +29,17 @@ public class ChatInterceptor implements HandlerInterceptor {
private final CoCoConfig coCoConfig;

public ChatInterceptor() {
this.redissonClient= SpringUtil.getBean(RedissonClient.class);
this.coCoConfig= SpringUtil.getBean(CoCoConfig.class);
this.redissonClient = SpringUtil.getBean(RedissonClient.class);
this.coCoConfig = SpringUtil.getBean(CoCoConfig.class);
}


/**
* 拦截请求,检查请求头中的Authorization字段是否为有效的token,并判断token是否可用
*
* @param request HTTP请求
* @param request HTTP请求
* @param response HTTP响应
* @param handler 处理器
* @param handler 处理器
* @return 是否继续执行处理器
* @throws Exception 异常信息
*/
Expand Down Expand Up @@ -85,8 +83,8 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp
* 设置HTTP响应的状态码和消息体
*
* @param response HTTP响应
* @param status 状态码
* @param msg 消息体
* @param status 状态码
* @param msg 消息体
* @throws IOException IO异常
*/
private void setResponse(HttpServletResponse response, int status, String msg) throws IOException {
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/com/coco/boot/service/impl/CoCoPilotServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,32 +314,6 @@ private ResponseEntity<String> getBaseProxyResponse(Object requestBody, String p
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("{\"message\": \"Too Many Requests\"}");
}

private void setCoolkey(String ghu, ResponseEntity response) {
String retryAfter = response.getHeaders().getFirst(HEADER_RETRY);
// 默认 600秒
long time = 120;
if (StringUtil.isNotBlank(retryAfter)) {
try {
time = Long.parseLong(retryAfter);
} catch (NumberFormatException e) {}
}

if (time > 1000) {
redissonClient.getSet(GHU_NO_ALIVE_KEY, StringCodec.INSTANCE).addAsync(ghu);
} else {
RMapCache<String, Integer> collingMap = redissonClient.getMapCache(GHU_COOLING_KEY);
if (!collingMap.isExists()) {
collingMap.addListener((EntryExpiredListener<String, Integer>) event -> {
// expired key
redissonClient.getSet(GHU_ALIVE_KEY, StringCodec.INSTANCE).add(event.getKey());
});
}
collingMap.put(ghu, 1, time+5, TimeUnit.SECONDS);
}

return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("{\"message\": \"Too Many Requests\"}");
}


/**
* 限流随机现有GHU
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/coco/boot/task/CoCoTask.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.coco.boot.task;

import com.coco.boot.config.CoCoConfig;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RKeys;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -35,8 +37,8 @@ public class CoCoTask {
* 重置账号
* 凌晨2点执行
*/
// @Scheduled(cron = "0 0 2 * * ?") 启动后,test()添加数据, 此定时任务 10秒后执行
@Scheduled(cron = "0/10 * * * * ?")
@Scheduled(cron = "0 0 2 * * ?")
// @Scheduled(cron = "0/10 * * * * ?")启动后,test()添加数据, 此定时任务 10秒后执行
private void resetSomeThins() {
RKeys keys = redissonClient.getKeys();
//token 次数
Expand All @@ -52,8 +54,8 @@ private void resetSomeThins() {
* 检查一下key吧
* 凌晨1点执行
*/
// @Scheduled(cron = "0 0 1 * * ?") 启动后,test()添加数据, 此定时任务 10秒后执行
@Scheduled(cron = "0/10 * * * * ?")
@Scheduled(cron = "0 0 1 * * ?")
// @Scheduled(cron = "0/10 * * * * ?")启动后,test()添加数据, 此定时任务 10秒后执行
private void checkSomeKey() {
RSet<String> noAlive = redissonClient.getSet(GHU_NO_ALIVE_KEY, StringCodec.INSTANCE);
RSet<String> alive = redissonClient.getSet(GHU_ALIVE_KEY, StringCodec.INSTANCE);
Expand Down Expand Up @@ -93,7 +95,7 @@ private void checkSomeKey() {
/**
* 添加测试数据
*/
@PostConstruct
// @PostConstruct 开启注解测试
private void test() {
// TODO 测试通过后 删除此代码
RSet<String> noAlive = redissonClient.getSet(GHU_NO_ALIVE_KEY, StringCodec.INSTANCE);
Expand Down

0 comments on commit a99dbbf

Please sign in to comment.