feat: add redis gateway protection and task recovery
This commit is contained in:
1 parent
cbc7317aa8
commit
473479fc4c
17 files changed
+272
-10
No files matched your search
@@ -10,3 +10,6 @@ infra/control-plane/.env
|
|||||||
infra/moyi/.env
|
infra/moyi/.env
|
||||||
infra/moyi/.env.cloud
|
infra/moyi/.env.cloud
|
||||||
prototype/.env
|
prototype/.env
|
||||||
|
.tools/
|
||||||
|
control-plane-java/.tools/
|
||||||
|
control-plane-java/target/
|
||||||
+3
-3
@@ -26,18 +26,18 @@ public class CatalogController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/models")
|
@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));
|
authService.requireUser(AuthController.sessionToken(request));
|
||||||
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false));
|
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/overview")
|
@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);
|
return catalogService.overview(AuthController.sessionToken(request), authService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/projects")
|
@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));
|
return Map.of("projects", catalogService.listProjects(AuthController.sessionToken(request), authService));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.moyi.controlplane.common;
|
package com.moyi.controlplane.common;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import com.moyi.controlplane.gateway.GatewayLimitException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
@@ -12,6 +13,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
public class ApiExceptionHandler {
|
public class ApiExceptionHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(ApiExceptionHandler.class);
|
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)
|
@ExceptionHandler(ApiException.class)
|
||||||
public ResponseEntity<Map<String, Object>> handleApiException(ApiException exception) {
|
public ResponseEntity<Map<String, Object>> handleApiException(ApiException exception) {
|
||||||
return ResponseEntity.status(exception.getStatus()).body(error(exception.getCode(), exception.getMessage()));
|
return ResponseEntity.status(exception.getStatus()).body(error(exception.getCode(), exception.getMessage()));
|
||||||
|
|||||||
+12
-2
@@ -22,15 +22,18 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class GatewayController {
|
public class GatewayController {
|
||||||
private final CatalogService catalogService;
|
private final CatalogService catalogService;
|
||||||
private final OpenAiCompatibleProvider provider;
|
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.catalogService = catalogService;
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
|
this.guard = guard;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/models")
|
@GetMapping("/models")
|
||||||
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
|
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
|
||||||
Map<String, Object> key = requireKey(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()
|
List<Map<String, Object>> data = catalogService.listModels(false).stream()
|
||||||
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id"))))
|
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id"))))
|
||||||
.map(model -> {
|
.map(model -> {
|
||||||
@@ -57,9 +60,16 @@ public class GatewayController {
|
|||||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||||
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
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 ("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"));
|
String providerId = String.valueOf(model.get("providerId"));
|
||||||
if (provider.isConfigured(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();
|
List<?> messages = input.get("messages") instanceof List<?> values ? values : List.of();
|
||||||
Map<String, Object> upstream = provider.chat(providerId, String.valueOf(model.get("upstreamModel")), messages);
|
Map<String, Object> upstream = provider.chat(providerId, String.valueOf(model.get("upstreamModel")), messages);
|
||||||
upstream.put("model", modelId);
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -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.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.moyi.controlplane.catalog.CatalogService;
|
import com.moyi.controlplane.catalog.CatalogService;
|
||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
@@ -15,6 +16,7 @@ import java.util.Map;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -23,21 +25,35 @@ public class AsyncTaskService {
|
|||||||
private final JdbcTemplate jdbc;
|
private final JdbcTemplate jdbc;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final CatalogService catalogService;
|
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.jdbc = jdbc;
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.catalogService = catalogService;
|
this.catalogService = catalogService;
|
||||||
|
this.maxQueuedPerModel = maxQueuedPerModel;
|
||||||
|
this.gatewayGuard = gatewayGuard;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
||||||
Map<String, Object> key = requireKey(authorization);
|
Map<String, Object> key = requireKey(authorization);
|
||||||
|
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||||
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
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);
|
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();
|
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
|
||||||
if (prompt.isBlank()) throw new ApiException("PROMPT_REQUIRED", "生成提示词不能为空");
|
if (prompt.isBlank()) throw new ApiException("PROMPT_REQUIRED", "生成提示词不能为空");
|
||||||
String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
|
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() {
|
public void completeMockTasks() {
|
||||||
jdbc.update(
|
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'",
|
"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'",
|
||||||
|
|||||||
+29
@@ -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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
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.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -43,4 +44,13 @@ public class TaskController {
|
|||||||
) {
|
) {
|
||||||
return Map.of("task", tasks.get(authorization, id));
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,8 @@ spring:
|
|||||||
data:
|
data:
|
||||||
redis:
|
redis:
|
||||||
url: ${SPRING_DATA_REDIS_URL:redis://localhost:6379}
|
url: ${SPRING_DATA_REDIS_URL:redis://localhost:6379}
|
||||||
|
connect-timeout: 2s
|
||||||
|
timeout: 2s
|
||||||
flyway:
|
flyway:
|
||||||
enabled: ${FLYWAY_ENABLED:true}
|
enabled: ${FLYWAY_ENABLED:true}
|
||||||
baseline-on-migrate: true
|
baseline-on-migrate: true
|
||||||
@@ -31,6 +33,13 @@ management:
|
|||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
moyi:
|
moyi:
|
||||||
|
gateway:
|
||||||
|
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
|
||||||
|
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
|
||||||
|
default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:4}
|
||||||
|
tasks:
|
||||||
|
max-queued-per-model: ${TASK_MAX_QUEUED_PER_MODEL:100}
|
||||||
|
worker-interval-ms: ${MOYI_TASK_WORKER_INTERVAL_MS:1000}
|
||||||
web:
|
web:
|
||||||
cors-origins: ${CONTROL_PLANE_CORS_ORIGINS:http://localhost:5173,http://127.0.0.1:5173}
|
cors-origins: ${CONTROL_PLANE_CORS_ORIGINS:http://localhost:5173,http://127.0.0.1:5173}
|
||||||
auth:
|
auth:
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
local time = redis.call('TIME')
|
||||||
|
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
||||||
|
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
|
||||||
|
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
|
||||||
|
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
||||||
|
return 1
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
local count = redis.call('INCR', KEYS[1])
|
||||||
|
if count == 1 then
|
||||||
|
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
||||||
|
end
|
||||||
|
if count > tonumber(ARGV[1]) then
|
||||||
|
return math.max(1, redis.call('PTTL', KEYS[1]))
|
||||||
|
end
|
||||||
|
return 0
|
||||||
@@ -35,6 +35,8 @@
|
|||||||
| `POST /v1/3d/tasks` | 平台 API Key | 创建 3D 异步任务(当前 Mock 执行器) |
|
| `POST /v1/3d/tasks` | 平台 API Key | 创建 3D 异步任务(当前 Mock 执行器) |
|
||||||
| `POST /v1/video/tasks` | 平台 API Key | 创建视频异步任务(当前 Mock 执行器) |
|
| `POST /v1/video/tasks` | 平台 API Key | 创建视频异步任务(当前 Mock 执行器) |
|
||||||
| `GET /v1/tasks/{id}` | 平台 API Key | 查询任务状态和结果 |
|
| `GET /v1/tasks/{id}` | 平台 API Key | 查询任务状态和结果 |
|
||||||
|
| `GET /v1/tasks` | 平台 API Key | 查询当前 Key 所属用户的最近任务 |
|
||||||
|
| `GET /api/tasks` | 登录会话 | 用户端恢复当前用户的最近任务 |
|
||||||
|
|
||||||
## 数据库迁移
|
## 数据库迁移
|
||||||
|
|
||||||
@@ -62,13 +64,13 @@ Invoke-WebRequest http://localhost:8080/health
|
|||||||
|
|
||||||
## 验收边界
|
## 验收边界
|
||||||
|
|
||||||
本节点完成的是“控制面基础闭环 + 同步供应商转发”,不是生产网关完成版:
|
本节点完成的是“控制面基础闭环 + 同步供应商转发 + Redis 网关保护 + 持久化任务队列”,不是生产网关完成版:
|
||||||
|
|
||||||
- 数据已写入 PostgreSQL,Redis 连接配置已预留,但限流、队列和分布式幂等尚未接入;
|
- 数据已写入 PostgreSQL;Redis 已用于按 API Key 限流、按模型并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次、模型并发 4,均可通过环境变量调整;Redis 不可用时网关返回 503,不会绕过保护直接请求上游;
|
||||||
- `/v1/chat/completions` 已支持 DeepSeek、Kimi、GLM 的 OpenAI 兼容转发;管理员在目录中新增带凭证的兼容供应商后,也可按模型绑定动态渠道;未配置 Key 时返回 Mock,流式响应暂未开放;
|
- `/v1/chat/completions` 已支持 DeepSeek、Kimi、GLM 的 OpenAI 兼容转发;管理员在目录中新增带凭证的兼容供应商后,也可按模型绑定动态渠道;未配置 Key 时返回 Mock,流式响应暂未开放;
|
||||||
- `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker,真实供应商执行器尚未替换;
|
- `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker,真实供应商执行器尚未替换;
|
||||||
- Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java;
|
- Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java;
|
||||||
- 价格、额度扣减、计费账单和并发排队仍沿用下一节点计划;
|
- 异步任务已持久化并提供按模型的排队上限(默认 100,可由 `TASK_MAX_QUEUED_PER_MODEL` 调整),但真实供应商执行器、优先级调度、价格/额度扣减和计费账单仍沿用下一节点计划;
|
||||||
- Docker 镜像需要在具备 Maven 和 Docker daemon 的环境中构建验证。
|
- Docker 镜像需要在具备 Maven 和 Docker daemon 的环境中构建验证。
|
||||||
|
|
||||||
## 下一节点
|
## 下一节点
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ MOYI_DB_PASSWORD=replace-with-a-long-random-password
|
|||||||
# Alibaba Cloud Redis/Tair private endpoint. Use TLS parameters if enabled by the instance.
|
# Alibaba Cloud Redis/Tair private endpoint. Use TLS parameters if enabled by the instance.
|
||||||
MOYI_REDIS_URL=rediss://:replace-with-redis-password@redis-private-endpoint:6379/0
|
MOYI_REDIS_URL=rediss://:replace-with-redis-password@redis-private-endpoint:6379/0
|
||||||
|
|
||||||
|
GATEWAY_REQUESTS_PER_MINUTE=60
|
||||||
|
GATEWAY_READS_PER_MINUTE=300
|
||||||
|
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
|
||||||
|
TASK_MAX_QUEUED_PER_MODEL=100
|
||||||
|
MOYI_TASK_WORKER_INTERVAL_MS=1000
|
||||||
|
|
||||||
SESSION_TTL_HOURS=168
|
SESSION_TTL_HOURS=168
|
||||||
AUTH_PASSWORD_PEPPER=replace-with-a-long-random-secret-at-least-32-chars
|
AUTH_PASSWORD_PEPPER=replace-with-a-long-random-secret-at-least-32-chars
|
||||||
CREDENTIAL_ENCRYPTION_KEY=replace-with-a-long-random-secret-for-provider-credentials
|
CREDENTIAL_ENCRYPTION_KEY=replace-with-a-long-random-secret-for-provider-credentials
|
||||||
|
|||||||
@@ -7,6 +7,13 @@ POSTGRES_DB=moyi_control
|
|||||||
|
|
||||||
REDIS_PASSWORD=replace-with-a-long-random-password
|
REDIS_PASSWORD=replace-with-a-long-random-password
|
||||||
|
|
||||||
|
# Gateway protection defaults. Tune after observing real traffic.
|
||||||
|
GATEWAY_REQUESTS_PER_MINUTE=60
|
||||||
|
GATEWAY_READS_PER_MINUTE=300
|
||||||
|
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
|
||||||
|
TASK_MAX_QUEUED_PER_MODEL=100
|
||||||
|
MOYI_TASK_WORKER_INTERVAL_MS=1000
|
||||||
|
|
||||||
SESSION_TTL_HOURS=168
|
SESSION_TTL_HOURS=168
|
||||||
AUTH_PASSWORD_PEPPER=replace-with-a-long-random-secret-at-least-32-chars
|
AUTH_PASSWORD_PEPPER=replace-with-a-long-random-secret-at-least-32-chars
|
||||||
CREDENTIAL_ENCRYPTION_KEY=replace-with-a-long-random-secret-for-provider-credentials
|
CREDENTIAL_ENCRYPTION_KEY=replace-with-a-long-random-secret-for-provider-credentials
|
||||||
|
|||||||
@@ -200,6 +200,18 @@ class HttpCatalogClient {
|
|||||||
return payload.task
|
return payload.task
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async listTasks(apiKey: string, limit = 50) {
|
||||||
|
const payload = await this.request<{ tasks: AsyncTaskResult[] }>(`/v1/tasks?limit=${Math.max(1, Math.min(limit, 100))}`, {
|
||||||
|
headers: { Authorization: `Bearer ${apiKey}` },
|
||||||
|
})
|
||||||
|
return payload.tasks
|
||||||
|
}
|
||||||
|
|
||||||
|
async listSessionTasks(limit = 50) {
|
||||||
|
const payload = await this.request<{ tasks: AsyncTaskResult[] }>(`/api/tasks?limit=${Math.max(1, Math.min(limit, 100))}`)
|
||||||
|
return payload.tasks
|
||||||
|
}
|
||||||
|
|
||||||
async listAdminModels() {
|
async listAdminModels() {
|
||||||
return this.request<{ models: CatalogModel[] }>('/api/admin/models')
|
return this.request<{ models: CatalogModel[] }>('/api/admin/models')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,12 @@ onMounted(async () => {
|
|||||||
overview.value = overviewResult
|
overview.value = overviewResult
|
||||||
overviewLoaded.value = true
|
overviewLoaded.value = true
|
||||||
catalogConnected.value = true
|
catalogConnected.value = true
|
||||||
|
try {
|
||||||
|
const sessionTasks = await catalogClient.listSessionTasks()
|
||||||
|
tasks.value = sessionTasks.map((task) => mapAsyncTask(task, catalogModels.value.find((model) => model.id === task.model) || emptyModel))
|
||||||
|
} catch {
|
||||||
|
// The Java task recovery endpoint is optional while older control-plane nodes are being migrated.
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
catalogModels.value = []
|
catalogModels.value = []
|
||||||
keys.value = []
|
keys.value = []
|
||||||
|
|||||||
Reference in new issue
Block a user