feat: add redis gateway protection and task recovery

This commit is contained in:
wlx committed 2026-09-08 18:02:45 +08:00
1 parent cbc7317aa8
commit 473479fc4c
17 files changed
+272 -10

No files matched your search

@@ -22,15 +22,18 @@ import org.springframework.web.bind.annotation.RestController;
public class GatewayController {
private final CatalogService catalogService;
private final OpenAiCompatibleProvider provider;
private final GatewayGuard guard;
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider) {
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard) {
this.catalogService = catalogService;
this.provider = provider;
this.guard = guard;
}
@GetMapping("/models")
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
Map<String, Object> key = requireKey(authorization);
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true);
List<Map<String, Object>> data = catalogService.listModels(false).stream()
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id"))))
.map(model -> {
@@ -57,9 +60,16 @@ public class GatewayController {
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
try (GatewayGuard.Slot slot = guard.acquire(model)) {
return invoke(key, modelId, model, input);
}
}
private ResponseEntity<Map<String, Object>> invoke(Map<String, Object> key, String modelId, Map<String, Object> model, Map<String, Object> input) {
String providerId = String.valueOf(model.get("providerId"));
if (provider.isConfigured(providerId)) {
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
List<?> messages = input.get("messages") instanceof List<?> values ? values : List.of();
Map<String, Object> upstream = provider.chat(providerId, String.valueOf(model.get("upstreamModel")), messages);
upstream.put("model", modelId);