Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sayHello committed Mar 19, 2024
1 parent 2f8eecb commit dc6567d
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/main/java/com/coco/boot/service/impl/CoCoPilotServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public R<String> uploadGhu(String data) {
map.put(ghu, "存活");
ghus.add(ghu);
} else if (response.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
Integer retry = Integer.valueOf(response.getHeaders().get(HEADER_RETRY).get(0));
setCoolkey(ghu,response);
log.info("upload 存活校验限流: {}, 返回: {}", ghu, response.getBody());
} else {
map.put(ghu, "失效");
Expand Down Expand Up @@ -250,28 +250,7 @@ private ResponseEntity<String> getBaseProxyResponse(Object requestBody, String p
} else {
ghuAliveKey.remove(ghu);
if (response.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
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);
}
setCoolkey(ghu,response);
} else {
redissonClient.getSet(GHU_NO_ALIVE_KEY, StringCodec.INSTANCE).addAsync(ghu);
}
Expand Down Expand Up @@ -307,4 +286,29 @@ public String getGhu(RSet<String> ghuAliveKey) {
}
return null;
}

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);
}
}

}

0 comments on commit dc6567d

Please sign in to comment.