feat(models): cap background LLM concurrency per model

Background ingestion/enrichment stages (summary, question, graph,
multimodal) all target the same chat model, and asynq queue weights are
scheduling priority rather than throttling. A large upload could therefore
burst up to the entire worker pool of concurrent chat calls against a
single provider, unbounded across replicas, with no model-side rate limit.

Govern concurrency at the chat-client layer (the one place that sees every
task type), keyed by model ID:

- internal/models/limiter: Redis ZSET + Lua distributed semaphore with a
  lease heartbeat, expired-lease reclaim for crashed holders, and fail-open
  on any backend error or context cancellation.
- chat: concurrencyChat decorator (outermost in NewChat) acquires a
  per-model slot only for background tasks; interactive chat is never gated.
- router/task: asynq middleware tags every worker execution as background.
- container: wire the Redis limiter with WEKNORA_MODEL_MAX_CONCURRENCY
  (model.max_concurrency), default 8; enabled only when Redis is present.

Tested with miniredis: concurrency cap, expired-lease reclaim, per-key
isolation, and all fail-open paths (nil client, backend down, cancelled ctx).
This commit is contained in:
wizardchen
2026-07-09 18:13:09 +08:00
committed by lyingbug
parent 76a8092483
commit 63198c5d9f
10 changed files with 516 additions and 3 deletions
+18
View File
@@ -154,6 +154,18 @@ func newAsynqServer(concurrency int, queues map[string]int) *asynq.Server {
)
}
// backgroundTaskMiddleware tags every task's context as a background worker
// execution (types.WithBackgroundTask) so the per-model chat concurrency
// governor applies to ingestion/enrichment LLM calls but not to interactive
// user-facing chat.
func backgroundTaskMiddleware() asynq.MiddlewareFunc {
return func(next asynq.Handler) asynq.Handler {
return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error {
return next.ProcessTask(types.WithBackgroundTask(ctx), t)
})
}
}
// NewParseAsynqServer builds the upstream pool: every queue EXCEPT QueueWiki.
// This is the user-facing document parse / post-process / enrichment pipeline.
// Concurrency comes from WEKNORA_ASYNQ_CONCURRENCY (default 32), unchanged from
@@ -218,6 +230,12 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
knowledgeFailer := newDeadLetterKnowledgeFailer(params.KnowledgeService, params.SpanTracker)
mux.Use(asynqdl.MiddlewareWithCallback(params.DeadLetterRepo, knowledgeFailer))
// Mark every asynq worker execution as a background task so the chat
// concurrency governor throttles ingestion/enrichment LLM traffic while
// leaving user-facing interactive chat ungated. Installed early so all
// downstream handlers (and their model calls) inherit the flag.
mux.Use(backgroundTaskMiddleware())
// Install Langfuse middleware BEFORE handler registration so every task
// type is automatically wrapped. When Langfuse is disabled the middleware
// is a pass-through; when enabled it resumes the upstream HTTP trace (if