Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(models): implement model.max_concurrency for background tasks

wizardchen4404ea74e3bd
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index 0b31db1dd..228b630a9 100755
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -3471,6 +3471,9 @@ export default {
asynq: {
concurrency: 'Async task worker concurrency',
},
+ model: {
+ max_concurrency: 'Default per-model concurrency limit',
+ },
},
keyDescriptions: {
auth: {
@@ -3491,6 +3494,10 @@ export default {
concurrency:
'Async task worker concurrency (asynq thread-pool size). Document parsing, embedding, and similar tasks are mostly I/O-bound, so raising this value can shorten queue time for bulk uploads. Requires a service process restart to take effect.',
},
+ model: {
+ max_concurrency:
+ 'Default cap on concurrent background (ingestion/enrichment) calls to a single model, keyed by model ID and shared across replicas. Read on every call and applied immediately with no restart. 0 or a negative value disables the default cap (each model still honours its own limit configured in model management). Affects background tasks only, not interactive chat.',
+ },
},
enumLabels: {
auth: {
diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts
index 622b12ca7..52d2c1a6d 100755
--- a/frontend/src/i18n/locales/ko-KR.ts
+++ b/frontend/src/i18n/locales/ko-KR.ts
@@ -2476,6 +2476,9 @@ export default {
asynq: {
concurrency: "비동기 작업 워커 동시 처리 수",
},
+ model: {
+ max_concurrency: "모델 기본 동시 처리 상한",
+ },
},
keyDescriptions: {
auth: {
@@ -2496,6 +2499,10 @@ export default {
concurrency:
"비동기 작업 worker 동시 처리 수(asynq 스레드 풀 크기)입니다. 문서 파싱·임베딩 등은 대부분 I/O 대기이므로 값을 올리면 대량 업로드 대기 시간을 줄일 수 있습니다. 적용하려면 서비스 프로세스를 재시작해야 합니다.",
},
+ model: {
+ max_concurrency:
+ "백그라운드 작업(문서 색인/보강)이 단일 모델에 대해 갖는 기본 동시 호출 상한이며, 모델 ID 기준으로 모든 복제본이 공유합니다. 매 호출마다 실시간으로 읽고 재시작 없이 즉시 적용됩니다. 0 또는 음수는 기본 상한을 해제합니다(각 모델은 모델 관리에서 설정한 자체 상한을 계속 준수함). 백그라운드 작업에만 영향을 주며 대화형 채팅에는 영향을 주지 않습니다.",
+ },
},
enumLabels: {
auth: {
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index c723ce9bc..e44f814f6 100755
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -2174,6 +2174,9 @@ export default {
asynq: {
concurrency: 'Параллелизм воркеров асинхронных задач',
},
+ model: {
+ max_concurrency: 'Лимит параллелизма модели по умолчанию',
+ },
},
keyDescriptions: {
auth: {
@@ -2194,6 +2197,10 @@ export default {
concurrency:
'Параллелизм воркеров асинхронных задач (размер пула потоков asynq). Парсинг документов, эмбеддинги и подобные задачи в основном ждут I/O, поэтому увеличение значения сокращает очередь при массовой загрузке. Для применения требуется перезапуск процесса сервиса.',
},
+ model: {
+ max_concurrency:
+ 'Лимит по умолчанию на количество одновременных фоновых вызовов (индексация/обогащение) к одной модели, привязанный к ID модели и общий для всех реплик. Читается при каждом вызове и применяется сразу без перезапуска. 0 или отрицательное значение отключает лимит по умолчанию (каждая модель всё равно соблюдает собственный лимит, заданный в управлении моделями). Влияет только на фоновые задачи, не на интерактивный чат.',
+ },
},
enumLabels: {
auth: {
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index 94adda9c1..c45b22345 100755
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -2479,6 +2479,9 @@ export default {
asynq: {
concurrency: "异步任务并发数",
},
+ model: {
+ max_concurrency: "模型默认并发上限",
+ },
},
keyDescriptions: {
auth: {
@@ -2506,6 +2509,12 @@ export default {
"文档解析、嵌入等任务多为 I/O 等待,适当提高可缩短批量上传排队时间。" +
"修改后需重启服务进程方可生效。",
},
+ model: {
+ max_concurrency:
+ "后台任务(文档入库/富化)对单个模型的默认并发上限,按模型 ID 全副本共享。" +
+ "每次调用实时读取,修改后立即生效、无需重启。0 或负数表示关闭默认限制" +
+ "(各模型仍会尊重自身在模型管理里配置的上限)。仅影响后台任务,不影响交互式对话。",
+ },
},
enumLabels: {
auth: {
diff --git a/internal/application/service/system_setting.go b/internal/application/service/system_setting.go
index 2bffa0815..e7826ce04 100644
--- a/internal/application/service/system_setting.go
+++ b/internal/application/service/system_setting.go
@@ -19,6 +19,7 @@ import (
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/logger"
+ "github.com/Tencent/WeKnora/internal/models/limiter"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
@@ -193,6 +194,23 @@ var registry = map[string]settingSpec{
"Wiki 生成以合成大模型调用为主,独立并发预算可避免上传高峰期被解析任务饿死," +
"同时不会因 Wiki 洪峰拖慢用户面解析。修改后需重启服务进程方可生效。",
},
+ // model.max_concurrency is the DEFAULT per-model cap on concurrent
+ // background (ingestion/enrichment) LLM/embedding/VLM calls, keyed by
+ // model ID and shared across replicas. Read at every gated call via the
+ // limiter governor; a runtime bridge (applyModelMaxConcurrency) pushes UI
+ // edits into limiter.SetGlobalLimit so no restart is needed. Individual
+ // models may override this via their own max_concurrency parameter.
+ // Mirrors WEKNORA_MODEL_MAX_CONCURRENCY (default 8). 0/negative disables
+ // the default cap.
+ "model.max_concurrency": {
+ Type: "int",
+ EnvName: "WEKNORA_MODEL_MAX_CONCURRENCY",
+ Default: int64(8),
+ Category: "worker",
+ Description: "后台任务(文档入库/富化)对单个模型的默认并发上限,按模型 ID 全副本共享。" +
+ "每次调用实时读取,修改后立即生效、无需重启。0 或负数表示关闭默认限制" +
+ "(各模型仍会尊重自身在模型管理里配置的上限)。仅影响后台任务,不影响交互式对话。",
+ },
}
// systemSettingService wires the repository, audit log, and (P2)
@@ -298,6 +316,7 @@ func (s *systemSettingService) preload(ctx context.Context) {
// the subsystem doesn't lag the cache by a full request cycle.
// Add new bridges here as more env vars get migrated.
s.applySSRFWhitelist(ctx)
+ s.applyModelMaxConcurrency(ctx)
}
// encodeDefault produces the JSONB encoding for a spec's built-in
@@ -389,6 +408,8 @@ func (s *systemSettingService) dispatchSideEffects(ctx context.Context, changedK
switch changedKey {
case "ssrf.whitelist":
s.applySSRFWhitelist(ctx)
+ case "model.max_concurrency":
+ s.applyModelMaxConcurrency(ctx)
}
}
@@ -417,6 +438,21 @@ func (s *systemSettingService) applySSRFWhitelist(ctx context.Context) {
len(list), extra != "")
}
+// applyModelMaxConcurrency resolves model.max_concurrency via the 3-tier
+// resolver and pushes it into the model concurrency governor so UI edits take
+// effect without a restart. Only the process-wide default limit is retuned;
+// the installed limiter backend (redis/local) stays intact. The default (8)
+// deliberately mirrors container.defaultModelMaxConcurrency so the value here
+// matches what the container installs at boot.
+//
+// Called at preload (initial sync), after Update (this replica's edit), and
+// after reload (peer's edit via pubsub).
+func (s *systemSettingService) applyModelMaxConcurrency(ctx context.Context) {
+ limit := int(s.GetInt(ctx, "model.max_concurrency", "WEKNORA_MODEL_MAX_CONCURRENCY", 8))
+ limiter.SetGlobalLimit(limit)
+ logger.Infof(ctx, "[system_settings] model.max_concurrency applied (limit=%d)", limit)
+}
+
// publishChange fans the change out to peers. Best-effort: a Redis
// outage logs a warning but does not fail the Update — the DB write
// already succeeded and our local cache is up-to-date. Other replicas
diff --git a/internal/models/limiter/governor.go b/internal/models/limiter/governor.go
index cfa2b7f56..ce7951a96 100644
--- a/internal/models/limiter/governor.go
+++ b/internal/models/limiter/governor.go
@@ -28,6 +28,17 @@ func SetGovernor(l ModelConcurrencyLimiter, limit int) {
governorN = limit
}
+// SetGlobalLimit updates ONLY the process-wide default per-model limit,
+// leaving the installed limiter backend intact. Used by the system-settings
+// runtime bridge so an operator can retune model.max_concurrency without a
+// restart. A non-positive value disables the default (models that carry their
+// own MaxConcurrency still honour it).
+func SetGlobalLimit(limit int) {
+ governorMu.Lock()
+ defer governorMu.Unlock()
+ governorN = limit
+}
+
// Gate acquires a per-model concurrency slot using the process-wide default
// limit. Equivalent to GateN(ctx, modelID, 0).
func Gate(ctx context.Context, modelID string) func() {