feat: package Beijing test deployment with HTTPS and preflight checks
This commit is contained in:
1 parent
ae749a1fe9
commit
c7278c00a6
33 files changed
+1153
-54
No files matched your search
@@ -147,7 +147,7 @@ public class CatalogService {
|
||||
OffsetDateTime now = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO platform_api_keys (id, user_id, project_id, name, key_prefix, key_hash, scopes, model_ids, status, created_at) VALUES (?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), 'active', ?)",
|
||||
id, user.id(), projectId, name, plaintext.substring(0, 16), sha256(plaintext), writeJson(scopes), writeJson(modelIds), now
|
||||
UUID.fromString(id), user.id(), projectId, name, plaintext.substring(0, 16), sha256(plaintext), writeJson(scopes), writeJson(modelIds), now
|
||||
);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("key", plaintext);
|
||||
|
||||
@@ -86,6 +86,9 @@ public class GatewayController {
|
||||
catalogService.touchKey(key);
|
||||
return ResponseEntity.ok(upstream);
|
||||
}
|
||||
if (!"provider-mock".equals(providerId)) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该供应商尚未配置密钥,不能执行真实调用", 503);
|
||||
}
|
||||
String prompt = extractPrompt(input.get("messages"));
|
||||
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
|
||||
catalogService.touchKey(key);
|
||||
|
||||
@@ -25,6 +25,7 @@ public class GatewayGuard {
|
||||
private final int requestsPerMinute;
|
||||
private final int readsPerMinute;
|
||||
private final int defaultModelConcurrency;
|
||||
private final String redisPrefix;
|
||||
|
||||
private static DefaultRedisScript<Long> script(String location) {
|
||||
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
|
||||
@@ -37,7 +38,8 @@ public class 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
|
||||
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency,
|
||||
@Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix
|
||||
) {
|
||||
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1) {
|
||||
throw new IllegalArgumentException("Gateway limits must be positive");
|
||||
@@ -46,10 +48,14 @@ public class GatewayGuard {
|
||||
this.requestsPerMinute = requestsPerMinute;
|
||||
this.readsPerMinute = readsPerMinute;
|
||||
this.defaultModelConcurrency = defaultModelConcurrency;
|
||||
if (redisPrefix == null || !redisPrefix.matches("[A-Za-z0-9:_-]{2,80}:")) {
|
||||
throw new IllegalArgumentException("Redis prefix must end in ':' and contain only letters, numbers, ':', '_' or '-'");
|
||||
}
|
||||
this.redisPrefix = redisPrefix;
|
||||
}
|
||||
|
||||
public void checkRate(UUID keyId, boolean readOnly) {
|
||||
String bucket = "moyi:gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId;
|
||||
String bucket = redisPrefix + "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));
|
||||
@@ -62,7 +68,7 @@ public class GatewayGuard {
|
||||
}
|
||||
|
||||
public Slot acquire(Map<String, Object> model) {
|
||||
String bucket = "moyi:gateway:slots:" + model.get("id");
|
||||
String bucket = redisPrefix + "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);
|
||||
@@ -71,7 +77,7 @@ public class GatewayGuard {
|
||||
|
||||
private long execute(DefaultRedisScript<Long> script, String key, String... args) {
|
||||
try {
|
||||
Long value = redis.execute(script, List.of(key), args);
|
||||
Long value = redis.execute(script, List.of(key), (Object[]) args);
|
||||
if (value != null) return value;
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("Gateway guard Redis operation failed ({})", exception.getClass().getSimpleName());
|
||||
|
||||
@@ -56,6 +56,9 @@ public class AsyncTaskService {
|
||||
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);
|
||||
if (!"provider-mock".equals(model.get("providerId"))) {
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "Java 版尚未接入该供应商的异步执行器", 501);
|
||||
}
|
||||
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();
|
||||
|
||||
@@ -223,7 +223,13 @@ public class UsageAuditService {
|
||||
try {
|
||||
JsonNode sanitized = sanitize(objectMapper.valueToTree(value));
|
||||
String text = objectMapper.writeValueAsString(sanitized);
|
||||
return text.length() > MAX_PAYLOAD_CHARS ? text.substring(0, MAX_PAYLOAD_CHARS) + "…" : text;
|
||||
if (text.length() <= MAX_PAYLOAD_CHARS) return text;
|
||||
// Preserve valid JSON for PostgreSQL jsonb even when payloads are large.
|
||||
ObjectNode summary = objectMapper.createObjectNode();
|
||||
summary.put("_truncated", true);
|
||||
summary.put("_originalChars", text.length());
|
||||
summary.put("preview", text.substring(0, 8000));
|
||||
return objectMapper.writeValueAsString(summary);
|
||||
} catch (JsonProcessingException exception) { return "{\"_unserializable\":true}"; }
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user