From f5f855c751b1227169cf4018947dce42b99181bd Mon Sep 17 00:00:00 2001 From: WangLX <519407855@qq.com> Date: Wed, 23 Sep 2026 17:55:31 +0800 Subject: [PATCH] feat: separate api key and upstream concurrency --- .../gateway/GatewayController.java | 3 ++- .../controlplane/gateway/GatewayGuard.java | 23 +++++++++++++++++-- .../src/main/resources/application.yml | 1 + .../gateway/GatewayDeploymentTest.java | 11 +++++++++ docs/09-java-control-plane-node.md | 2 +- docs/11-server-test-deployment.md | 2 +- docs/17-catalog-and-key-selection.md | 17 ++++++++++++++ infra/moyi/.env.cloud.example | 1 + infra/moyi/.env.example | 1 + infra/moyi/docker-compose.cloud.yml | 1 + prototype/src/components/CatalogAdmin.vue | 4 ++-- prototype/src/styles.css | 2 ++ prototype/src/views/UserConsole.vue | 14 ++++++++++- 13 files changed, 74 insertions(+), 8 deletions(-) diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java index 2ddd527..24192ed 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java @@ -80,7 +80,8 @@ public class GatewayController { if(!mock && !provider.isConfigured(providerId)) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503); ChatMeter.Request request=ChatMeter.request(input,json); guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false); - try (GatewayGuard.Slot slot = guard.acquire(model)) { + try (GatewayGuard.Slot keySlot = guard.acquireKey(UUID.fromString(String.valueOf(key.get("id")))); + GatewayGuard.Slot modelSlot = guard.acquire(model)) { var ticket=billing.reserve(key,model,request,idempotencyKey); servletResponse.setHeader("X-Request-Id",ticket.id().toString()); try { diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java index ca816c2..5389cf4 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java @@ -25,6 +25,7 @@ public class GatewayGuard { private final int requestsPerMinute; private final int readsPerMinute; private final int defaultModelConcurrency; + private final int defaultKeyConcurrency; private final String redisPrefix; private static DefaultRedisScript script(String location) { @@ -34,26 +35,35 @@ public class GatewayGuard { return script; } + @org.springframework.beans.factory.annotation.Autowired 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, + @Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency, @Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix ) { - if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1) { + if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1) { throw new IllegalArgumentException("Gateway limits must be positive"); } this.redis = redis; this.requestsPerMinute = requestsPerMinute; this.readsPerMinute = readsPerMinute; this.defaultModelConcurrency = defaultModelConcurrency; + this.defaultKeyConcurrency = defaultKeyConcurrency; 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; } + /** Compatibility constructor for isolated unit tests and local callers. */ + public GatewayGuard(StringRedisTemplate redis, int requestsPerMinute, int readsPerMinute, + int defaultModelConcurrency, String redisPrefix) { + this(redis, requestsPerMinute, readsPerMinute, defaultModelConcurrency, 4, redisPrefix); + } + public void checkRate(UUID keyId, boolean readOnly) { String bucket = redisPrefix + "gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId; long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000"); @@ -68,13 +78,22 @@ public class GatewayGuard { } public Slot acquire(Map model) { - String bucket = redisPrefix + "gateway:slots:" + model.get("id"); + String bucket = redisPrefix + "gateway:slots:model:" + 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); } + /** Limits simultaneous in-flight requests made through one MOYI platform API Key. */ + public Slot acquireKey(UUID keyId) { + String bucket = redisPrefix + "gateway:slots:key:" + keyId; + String token = UUID.randomUUID().toString(); + long acquired = execute(ACQUIRE, bucket, Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token); + if (acquired != 1) throw new GatewayLimitException("KEY_BUSY", "当前 API Key 的并发已满,请稍后重试", 429, 1); + return new Slot(bucket, token); + } + private long execute(DefaultRedisScript script, String key, String... args) { try { Long value = redis.execute(script, List.of(key), (Object[]) args); diff --git a/control-plane-java/src/main/resources/application.yml b/control-plane-java/src/main/resources/application.yml index b6eee8b..c9a9f1a 100644 --- a/control-plane-java/src/main/resources/application.yml +++ b/control-plane-java/src/main/resources/application.yml @@ -44,6 +44,7 @@ moyi: requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60} reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300} default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:4} + default-key-concurrency: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:4} tasks: max-queued-per-model: ${TASK_MAX_QUEUED_PER_MODEL:100} worker-interval-ms: ${MOYI_TASK_WORKER_INTERVAL_MS:1000} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java index 72d1516..77cef5d 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java @@ -29,6 +29,17 @@ class GatewayDeploymentTest { verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:rate:invoke:" + keyId)), any(Object[].class)); } + @Test + void keyConcurrencyUsesDedicatedRedisBucket() { + StringRedisTemplate redis = mock(StringRedisTemplate.class); + doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class)); + UUID keyId = UUID.randomUUID(); + try (GatewayGuard.Slot ignored = new GatewayGuard(redis, 60, 300, 4, 2, "moyi:test:").acquireKey(keyId)) { + // The lease is intentionally held for the duration of this assertion. + } + verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class)); + } + @Test void invalidRedisPrefixIsRejected() { assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, "")); diff --git a/docs/09-java-control-plane-node.md b/docs/09-java-control-plane-node.md index bbfc862..64976eb 100644 --- a/docs/09-java-control-plane-node.md +++ b/docs/09-java-control-plane-node.md @@ -77,7 +77,7 @@ Invoke-WebRequest http://localhost:8080/health 本节点完成的是“控制面基础闭环 + 同步供应商转发 + Redis 网关保护 + 持久化任务队列”,不是生产网关完成版: -- 数据已写入 PostgreSQL;Redis 已用于按 API Key 限流、按模型并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次、模型并发 4,均可通过环境变量调整;Redis 不可用时网关返回 503,不会绕过保护直接请求上游; +- 数据已写入 PostgreSQL;Redis 已用于 API Key 频率限制、API Key 并发租约、上游分组并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次(`GATEWAY_REQUESTS_PER_MINUTE`)、每 Key 同时 4 个请求(`GATEWAY_DEFAULT_KEY_CONCURRENCY`);分组并发由管理端模型分组的 `maxConcurrency` 配置,未配置时使用 `GATEWAY_DEFAULT_MODEL_CONCURRENCY`。Redis 不可用时网关返回 503,不会绕过保护直接请求上游; - `/v1/chat/completions` 支持 OpenAI 兼容同步/流式文本聊天;真实供应商未配置 Key 时返回 503,只有 `provider-mock` 可以返回 Mock。V13 起按人民币分组售价预留并结算,详见 [流式与计费](16-streaming-chat-billing.md); - `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker;真实供应商执行器尚未替换,返回 501 而非伪造任务成功; - Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java; diff --git a/docs/11-server-test-deployment.md b/docs/11-server-test-deployment.md index 65aa606..1e4d260 100644 --- a/docs/11-server-test-deployment.md +++ b/docs/11-server-test-deployment.md @@ -16,7 +16,7 @@ 真实供应商没有配置密钥时返回 503,不再返回 Mock 成功。只有明确绑定 `provider-mock` 的模型可以走 Mock;真实异步供应商返回 501。已有种子目录和价格只是联调数据,实际模型 ID/能力须由供应商控制台核验后修改。 -注意更正早期说明:当前登录会话和任务状态保存于 PostgreSQL;Tair 已实现的是按 Key 限流、按模型并发租约,不是已完成的 MQ、幂等或异步调度平台。不需要现在购买 MQ。 +注意更正早期说明:当前登录会话和任务状态保存于 PostgreSQL;Tair 已实现的是按 Key 限流、按 API Key 并发租约、按上游分组并发租约,不是已完成的 MQ、幂等或异步调度平台。不需要现在购买 MQ。 ## 2. 你需要在阿里云完成的配置 diff --git a/docs/17-catalog-and-key-selection.md b/docs/17-catalog-and-key-selection.md index e4aaf71..b8a1e72 100644 --- a/docs/17-catalog-and-key-selection.md +++ b/docs/17-catalog-and-key-selection.md @@ -8,6 +8,23 @@ API Key 权限窗口支持同时勾选多个模型,再选择各自的分组; 模型广场、管理端模型/渠道和 API Key 列表默认每页 12 项,可选 24/48 项。权限列表默认每页 8 个模型,可选 16/32 项。筛选或每页数量改变时回到首页;删除导致页数减少时自动回到有效页。 +## 两层并发限制 + +一次同步模型调用要同时通过两层并发保护: + +1. **平台 API Key 并发**:限制同一个用户 Key 同时进行中的请求数量。默认值为 4,可在服务端环境变量 `GATEWAY_DEFAULT_KEY_CONCURRENCY` 调整;每个 Key 独立计数,不会因为其他用户占满而互相阻塞。 +2. **上游分组并发**:限制同一个渠道分组向中转站发出的同时请求数量,供使用该分组的多个用户共享。管理员在模型与分组中填写“上游分组并发上限”;1 表示串行,4 表示最多同时处理 4 个请求。 + +两层限制会同时生效,任一层达到上限都会返回 429,并带有短暂重试提示。API Key 的每分钟请求频率和并发数是不同设置;异步任务另受任务队列上限控制。 + +## 用户操作指南 + +1. 在模型广场先按模型厂商筛选,再打开模型详情选择渠道分组;复制详情中的“调用模型 ID”。 +2. 在 API Keys 创建密钥时,可以一次勾选多个模型和多个分组。完整密钥只显示一次,请立即保存;停用的密钥可以重新启用。 +3. 在线调试选择已有的启用 API Key、模型和分组即可,不需要为每次测试重新创建密钥;请求和响应会保留到使用记录。 +4. 外部应用使用用户端显示的统一 Base URL,路径为 `/v1`,聊天接口为 `/v1/chat/completions`,通过 `Authorization: Bearer ` 认证。 +5. 使用记录查看模型、厂商、渠道分组、Token、费用、耗时和时间;任务中心查看异步任务进度与结果。余额、权限或并发不足时,按错误提示处理后再重试。 + ## 验证 - `cd prototype; npm test`:目录合并、无可用分组的模型、多模型授权集合和分页边界,以及既有流式响应测试。 diff --git a/infra/moyi/.env.cloud.example b/infra/moyi/.env.cloud.example index 45a6aab..181a4e1 100644 --- a/infra/moyi/.env.cloud.example +++ b/infra/moyi/.env.cloud.example @@ -43,6 +43,7 @@ JAVA_OPTS='-Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError' GATEWAY_REQUESTS_PER_MINUTE=60 GATEWAY_READS_PER_MINUTE=300 GATEWAY_DEFAULT_MODEL_CONCURRENCY=4 +GATEWAY_DEFAULT_KEY_CONCURRENCY=4 TASK_MAX_QUEUED_PER_MODEL=100 MOYI_TASK_WORKER_INTERVAL_MS=1000 diff --git a/infra/moyi/.env.example b/infra/moyi/.env.example index b8e2954..3e39dc2 100644 --- a/infra/moyi/.env.example +++ b/infra/moyi/.env.example @@ -11,6 +11,7 @@ REDIS_PASSWORD=replace-with-a-long-random-password GATEWAY_REQUESTS_PER_MINUTE=60 GATEWAY_READS_PER_MINUTE=300 GATEWAY_DEFAULT_MODEL_CONCURRENCY=4 +GATEWAY_DEFAULT_KEY_CONCURRENCY=4 TASK_MAX_QUEUED_PER_MODEL=100 MOYI_TASK_WORKER_INTERVAL_MS=1000 diff --git a/infra/moyi/docker-compose.cloud.yml b/infra/moyi/docker-compose.cloud.yml index 2b66c4c..9ea2dff 100644 --- a/infra/moyi/docker-compose.cloud.yml +++ b/infra/moyi/docker-compose.cloud.yml @@ -46,6 +46,7 @@ services: GATEWAY_REQUESTS_PER_MINUTE: ${GATEWAY_REQUESTS_PER_MINUTE:-60} GATEWAY_READS_PER_MINUTE: ${GATEWAY_READS_PER_MINUTE:-300} GATEWAY_DEFAULT_MODEL_CONCURRENCY: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:-4} + GATEWAY_DEFAULT_KEY_CONCURRENCY: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:-4} TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100} MOYI_TASK_WORKER_INTERVAL_MS: ${MOYI_TASK_WORKER_INTERVAL_MS:-1000} DB_POOL_MAX_SIZE: ${DB_POOL_MAX_SIZE:-10} diff --git a/prototype/src/components/CatalogAdmin.vue b/prototype/src/components/CatalogAdmin.vue index 92cc646..791f8bb 100644 --- a/prototype/src/components/CatalogAdmin.vue +++ b/prototype/src/components/CatalogAdmin.vue @@ -108,7 +108,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider

{{ props.section === 'channels' ? '渠道管理' : '模型与分组' }}

{{ error }}

-

启用模型后会显示在用户端模型广场。模型厂商表示模型的实际研发方,渠道表示请求经过的中转站或官方接口;分组与所属渠道也启用后,用户才能为 API Key 选择该分组。并发上限按分组生效,填 1 才是串行,填 4 表示同一分组最多同时处理 4 个请求。

+

启用模型后会显示在用户端模型广场。模型厂商表示模型的实际研发方,渠道表示请求经过的中转站或官方接口;分组与所属渠道也启用后,用户才能为 API Key 选择该分组。平台 API Key 并发和上游渠道/模型并发是两层独立限制:前者限制同一个 Key 的同时请求数,后者在下方按分组填写,填 1 才是串行,填 4 表示同一渠道分组最多同时处理 4 个请求。

加载中…

渠道来源 / 协议Base URL凭据状态操作
{{ item.name }}{{ item.code }}{{ item.sourceType }}{{ item.protocol }}{{ item.baseUrl || '未配置' }}{{ item.credentialMasked || '未配置' }}
暂无渠道
@@ -153,7 +153,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider 文本聊天自动扣费仅支持 CNY / 1M tokens(元/百万 Token)。输入、输出均需填写;填 0 表示免费,留空则禁止调用。请填写平台对用户的售价。 - + diff --git a/prototype/src/styles.css b/prototype/src/styles.css index e43692f..9981df8 100644 --- a/prototype/src/styles.css +++ b/prototype/src/styles.css @@ -213,3 +213,5 @@ td strong{font-size:12px} @media(min-width:1151px){.model-filter-grid{grid-template-columns:minmax(220px,1.7fr) repeat(5,minmax(125px,1fr))}} /* Usage audit console */ .usage-filter{display:flex;align-items:center;gap:10px;flex-wrap:wrap}.usage-filter .search-input{min-width:260px;flex:1}.usage-filter select{min-width:120px}.button.danger{color:#fff;border-color:#d94b62;background:#d94b62}.button.danger:hover{background:#bd354d}.danger-icon{color:#c43d55}.usage-table th:first-child,.usage-table td:first-child{width:36px}.usage-table input[type=checkbox]{width:15px;height:15px;accent-color:#6351df}.usage-table code{font-size:10px}.usage-detail-list{margin-bottom:18px}.usage-payload{max-height:180px;overflow:auto;margin:6px 0 15px;padding:11px;border:1px solid var(--border);border-radius:8px;color:#344054;font:11px/1.55 "DM Mono",monospace;white-space:pre-wrap;word-break:break-word;background:#f7f8fb}.table-pagination{display:flex;align-items:center;justify-content:space-between;padding:12px 16px;border-top:1px solid var(--border);color:var(--muted);font-size:11px}.table-pagination>div{display:flex;gap:6px}.rotate-90{transform:rotate(90deg)}.rotate--90{transform:rotate(-90deg)} +.docs-guide-extra{padding:24px 28px}.docs-guide-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px}.docs-guide-grid article{display:flex;gap:12px;min-height:112px;padding:15px;border:1px solid var(--border);border-radius:10px;background:var(--soft)}.docs-guide-grid article>b{display:grid;flex:0 0 30px;width:30px;height:30px;place-items:center;border:1px solid #d8d0fb;border-radius:8px;color:var(--primary);font-family:"DM Mono",monospace;font-size:10px;background:var(--primary-soft)}.docs-guide-grid h4{margin:1px 0 7px;font-size:13px}.docs-guide-grid p{margin:0;color:var(--text-soft);font-size:11px;line-height:1.65}.docs-guide-grid code{padding:2px 4px;border-radius:4px;color:var(--primary);font-family:"DM Mono",monospace;background:var(--primary-soft)} +@media(max-width:700px){.docs-guide-extra{padding:20px 16px}.docs-guide-grid{grid-template-columns:1fr}} diff --git a/prototype/src/views/UserConsole.vue b/prototype/src/views/UserConsole.vue index c75dd7f..a7b0732 100644 --- a/prototype/src/views/UserConsole.vue +++ b/prototype/src/views/UserConsole.vue @@ -640,7 +640,19 @@ MOYI_MODEL=<model-id>
02
< -H "Authorization: Bearer $MOYI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "<model-id>", "messages": [...] }'

保持开放与兼容。具体模型支持的协议和参数以模型详情与版本化 API 文档为准。

- + + +
+
USER GUIDE

从模型广场到第一次调用

按步骤操作
+
+
01

选择模型与来源

模型广场先按模型厂商筛选,例如 OpenAI、智谱、月之暗面或字节跳动;进入详情后再选择可用的渠道分组。模型 ID 使用详情里的“调用模型 ID”。

+
02

创建并授权 API Key

在 API Keys 中创建 Key,授权窗口可以同时勾选多个模型和多个分组。密钥只在创建时完整显示,请立即保存;停用后的 Key 可重新启用。

+
03

在线调试

在线调试直接选择已有的启用状态 API Key,再选择模型和分组;无需为每次调试重新创建 Key。调试结果和请求会保留在历史记录中。

+
04

接入应用

将 Base URL、Key 和模型调用 ID 配置到 SDK 或程序中。统一地址为 {{ platformApiBaseUrl }},聊天接口是 /chat/completions,请求头使用 Bearer Key。

+
05

查看用量与任务

使用记录展示模型、分组、Token、费用和耗时;任务中心展示异步任务状态和结果。额度不足、模型停用或分组并发已满时,请先查看错误提示和重试时间。

+
06

理解两层并发

同一个 API Key 有独立的并发上限;上游分组还有一个可供多个用户共享的并发上限。一次请求必须同时取得两类额度,任一达到上限都会稍后重试。

+
+
{{ selectedModel.vendorName || '未标注厂商' }}{{ selectedModel.type }} · {{ selectedModel.mode }}调用渠道:{{ selectedModel.provider }}