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

@@ -26,18 +26,18 @@ public class CatalogController {
}
@GetMapping("/models")
public Map<String, Object> models(@jakarta.servlet.http.HttpServletRequest request) {
public Map<String, Object> models(jakarta.servlet.http.HttpServletRequest request) {
authService.requireUser(AuthController.sessionToken(request));
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false));
}
@GetMapping("/overview")
public Map<String, Object> overview(@jakarta.servlet.http.HttpServletRequest request) {
public Map<String, Object> overview(jakarta.servlet.http.HttpServletRequest request) {
return catalogService.overview(AuthController.sessionToken(request), authService);
}
@GetMapping("/projects")
public Map<String, Object> projects(@jakarta.servlet.http.HttpServletRequest request) {
public Map<String, Object> projects(jakarta.servlet.http.HttpServletRequest request) {
return Map.of("projects", catalogService.listProjects(AuthController.sessionToken(request), authService));
}
@@ -1,6 +1,7 @@
package com.moyi.controlplane.common;
import java.util.Map;
import com.moyi.controlplane.gateway.GatewayLimitException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
@@ -12,6 +13,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
public class ApiExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(ApiExceptionHandler.class);
@ExceptionHandler(GatewayLimitException.class)
public ResponseEntity<Map<String, Object>> handleGatewayLimit(GatewayLimitException exception) {
return ResponseEntity.status(exception.getStatus())
.header("Retry-After", Integer.toString(exception.getRetryAfterSeconds()))
.body(error(exception.getCode(), exception.getMessage()));
}
@ExceptionHandler(ApiException.class)
public ResponseEntity<Map<String, Object>> handleApiException(ApiException exception) {
return ResponseEntity.status(exception.getStatus()).body(error(exception.getCode(), exception.getMessage()));
@@ -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);
@@ -0,0 +1,100 @@
package com.moyi.controlplane.gateway;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
@Service
public class GatewayGuard {
private static final Logger log = LoggerFactory.getLogger(GatewayGuard.class);
// Longer than the synchronous provider's 90-second HTTP request timeout.
private static final long SLOT_TTL_MS = 180_000;
private static final DefaultRedisScript<Long> RATE = script("redis/rate-limit.lua");
private static final DefaultRedisScript<Long> ACQUIRE = script("redis/acquire-slot.lua");
private static final DefaultRedisScript<Long> RELEASE = new DefaultRedisScript<>("return redis.call('ZREM', KEYS[1], ARGV[1])", Long.class);
private final StringRedisTemplate redis;
private final int requestsPerMinute;
private final int readsPerMinute;
private final int defaultModelConcurrency;
private static DefaultRedisScript<Long> script(String location) {
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
script.setLocation(new ClassPathResource(location));
script.setResultType(Long.class);
return script;
}
public GatewayGuard(
StringRedisTemplate redis,
@Value("${moyi.gateway.requests-per-minute:60}") int requestsPerMinute,
@Value("${moyi.gateway.reads-per-minute:300}") int readsPerMinute,
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency
) {
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1) {
throw new IllegalArgumentException("Gateway limits must be positive");
}
this.redis = redis;
this.requestsPerMinute = requestsPerMinute;
this.readsPerMinute = readsPerMinute;
this.defaultModelConcurrency = defaultModelConcurrency;
}
public void checkRate(UUID keyId, boolean readOnly) {
String bucket = "moyi:gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId;
long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000");
if (retryMs > 0) {
throw new GatewayLimitException("RATE_LIMITED", "请求过于频繁,请稍后重试", 429, (int) ((retryMs + 999) / 1000));
}
}
public int modelConcurrency(Map<String, Object> model) {
Object limit = model.get("maxConcurrency");
return limit instanceof Number number && number.intValue() > 0 ? number.intValue() : defaultModelConcurrency;
}
public Slot acquire(Map<String, Object> model) {
String bucket = "moyi:gateway:slots:" + model.get("id");
String token = UUID.randomUUID().toString();
long acquired = execute(ACQUIRE, bucket, Integer.toString(modelConcurrency(model)), Long.toString(SLOT_TTL_MS), token);
if (acquired != 1) throw new GatewayLimitException("MODEL_BUSY", "当前模型并发已满,请稍后重试", 429, 1);
return new Slot(bucket, token);
}
private long execute(DefaultRedisScript<Long> script, String key, String... args) {
try {
Long value = redis.execute(script, List.of(key), args);
if (value != null) return value;
} catch (DataAccessException exception) {
log.warn("Gateway guard Redis operation failed ({})", exception.getClass().getSimpleName());
}
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
}
public final class Slot implements AutoCloseable {
private final String key;
private final String token;
private final AtomicBoolean closed = new AtomicBoolean();
private Slot(String key, String token) { this.key = key; this.token = token; }
@Override
public void close() {
if (!closed.compareAndSet(false, true)) return;
try {
execute(RELEASE, key, token);
} catch (GatewayLimitException exception) {
// A failed release expires automatically and must not replace the upstream response.
log.warn("Gateway concurrency slot release failed; waiting for lease expiry");
}
}
}
}
@@ -0,0 +1,14 @@
package com.moyi.controlplane.gateway;
import com.moyi.controlplane.common.ApiException;
public class GatewayLimitException extends ApiException {
private final int retryAfterSeconds;
public GatewayLimitException(String code, String message, int status, int retryAfterSeconds) {
super(code, message, status);
this.retryAfterSeconds = Math.max(1, retryAfterSeconds);
}
public int getRetryAfterSeconds() { return retryAfterSeconds; }
}
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.gateway.GatewayGuard;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.OffsetDateTime;
@@ -15,6 +16,7 @@ import java.util.Map;
import java.util.UUID;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -23,21 +25,35 @@ public class AsyncTaskService {
private final JdbcTemplate jdbc;
private final ObjectMapper objectMapper;
private final CatalogService catalogService;
private final int maxQueuedPerModel;
private final GatewayGuard gatewayGuard;
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService) {
public AsyncTaskService(
JdbcTemplate jdbc,
ObjectMapper objectMapper,
CatalogService catalogService,
@Value("${moyi.tasks.max-queued-per-model:100}") int maxQueuedPerModel,
GatewayGuard gatewayGuard
) {
if (maxQueuedPerModel < 1) throw new IllegalArgumentException("Task queue limit must be positive");
this.jdbc = jdbc;
this.objectMapper = objectMapper;
this.catalogService = catalogService;
this.maxQueuedPerModel = maxQueuedPerModel;
this.gatewayGuard = gatewayGuard;
}
@Transactional
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
Map<String, Object> key = requireKey(authorization);
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
String modelId = String.valueOf(input.getOrDefault("model", ""));
Map<String, Object> model = catalogService.activeModel(modelId);
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
if (prompt.isBlank()) throw new ApiException("PROMPT_REQUIRED", "生成提示词不能为空");
String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
@@ -62,7 +78,20 @@ public class AsyncTaskService {
}
}
@Scheduled(fixedDelayString = "${MOYI_TASK_WORKER_INTERVAL_MS:1000}")
public List<Map<String, Object>> list(String authorization, int limit) {
Map<String, Object> key = requireKey(authorization);
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
int boundedLimit = Math.max(1, Math.min(limit, 100));
return jdbc.query("SELECT * FROM model_tasks WHERE user_id = ? ORDER BY created_at DESC LIMIT ?", this::mapTask, userId, boundedLimit);
}
public List<Map<String, Object>> listForUser(String sessionToken, com.moyi.controlplane.auth.AuthService auth, int limit) {
UUID userId = auth.requireUser(sessionToken).id();
int boundedLimit = Math.max(1, Math.min(limit, 100));
return jdbc.query("SELECT * FROM model_tasks WHERE user_id = ? ORDER BY created_at DESC LIMIT ?", this::mapTask, userId, boundedLimit);
}
@Scheduled(fixedDelayString = "${moyi.tasks.worker-interval-ms:1000}")
public void completeMockTasks() {
jdbc.update(
"UPDATE model_tasks SET status = 'succeeded', progress = 100, result = CAST(CASE WHEN task_type = '3d' THEN ? ELSE ? END AS jsonb), updated_at = ?, completed_at = ? WHERE status IN ('queued', 'running') AND provider_task_id = 'mock' AND created_at < now() - INTERVAL '500 milliseconds'",
@@ -0,0 +1,29 @@
package com.moyi.controlplane.task;
import com.moyi.controlplane.auth.AuthController;
import com.moyi.controlplane.auth.AuthService;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/tasks")
public class SessionTaskController {
private final AsyncTaskService tasks;
private final AuthService auth;
public SessionTaskController(AsyncTaskService tasks, AuthService auth) {
this.tasks = tasks;
this.auth = auth;
}
@GetMapping
public Map<String, Object> list(
jakarta.servlet.http.HttpServletRequest request,
@RequestParam(defaultValue = "50") int limit
) {
return Map.of("tasks", tasks.listForUser(AuthController.sessionToken(request), auth, limit));
}
}
@@ -6,6 +6,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -43,4 +44,13 @@ public class TaskController {
) {
return Map.of("task", tasks.get(authorization, id));
}
@GetMapping("/tasks")
public Map<String, Object> list(
@RequestHeader(value = "Authorization", required = false) String authorization,
@RequestParam(defaultValue = "50") int limit
) {
return Map.of("tasks", tasks.list(authorization, limit));
}
}