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:
@@ -39,6 +39,12 @@ const (
|
||||
LangfuseTraceContextKey ContextKey = "LangfuseTrace"
|
||||
// SystemAdminContextKey is the context key indicating whether the user is a system administrator
|
||||
SystemAdminContextKey ContextKey = "SystemAdmin"
|
||||
// BackgroundTaskContextKey marks a context whose model calls originate from
|
||||
// an asynq background worker (document parse / summary / question / graph /
|
||||
// multimodal enrichment) rather than a user-facing HTTP request. The chat
|
||||
// concurrency governor uses this to throttle only background LLM traffic,
|
||||
// leaving interactive chat latency untouched. See WithBackgroundTask.
|
||||
BackgroundTaskContextKey ContextKey = "BackgroundTask"
|
||||
// MCPOAuthNonInteractiveContextKey marks a request whose channel cannot
|
||||
// resolve an in-conversation MCP OAuth prompt (e.g. an IM bot: there is no
|
||||
// live client to click "Authorize" and call the resolve endpoint). When set,
|
||||
|
||||
@@ -131,6 +131,25 @@ func IsMCPOAuthNonInteractive(ctx context.Context) bool {
|
||||
return v
|
||||
}
|
||||
|
||||
// WithBackgroundTask marks ctx as originating from an asynq background worker
|
||||
// (document parse / summary / question / graph / multimodal enrichment). The
|
||||
// chat concurrency governor throttles only background LLM traffic, so this flag
|
||||
// determines whether the per-model concurrency limit applies. See
|
||||
// BackgroundTaskContextKey.
|
||||
func WithBackgroundTask(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, BackgroundTaskContextKey, true)
|
||||
}
|
||||
|
||||
// IsBackgroundTask reports whether ctx was marked as a background worker task
|
||||
// (see WithBackgroundTask). Returns false for interactive / HTTP request paths.
|
||||
func IsBackgroundTask(ctx context.Context) bool {
|
||||
if ctx == nil {
|
||||
return false
|
||||
}
|
||||
v, _ := ctx.Value(BackgroundTaskContextKey).(bool)
|
||||
return v
|
||||
}
|
||||
|
||||
// LanguageFromContext extracts the language locale string from ctx (e.g. "zh-CN", "en-US").
|
||||
// Returns ("zh-CN", false) when the key is absent.
|
||||
func LanguageFromContext(ctx context.Context) (string, bool) {
|
||||
|
||||
Reference in New Issue
Block a user