fix(models): harden background concurrency governor
Follow-up hardening on the per-model background concurrency governor: - limiter: heartbeat now refreshes the ZSET key's PEXPIRE alongside the lease score, so a semaphore that stays saturated with no slot turnover can't let the whole key expire (which would drop every lease and admit over the limit); reuse one poll timer instead of allocating time.After each iteration under contention. - governor: extract the process-wide governor + Gate helper into the limiter package so chat and vlm share one limiter/limit without importing each other; add an in-process localLimiter for Lite mode. - vlm: gate background multimodal (VLM) calls through the same governor; previously only chat was throttled. - chat: ChatStream releases its slot when the consumer abandons the stream and cancels ctx (select on ctx.Done, drain inner), fixing a slot/goroutine leak. - container/sync_task: install the local governor in Lite mode and tag SyncTaskExecutor runs as background; honour model.max_concurrency=0 as an explicit disable instead of falling back to the default. - tests: cover the governor gating, local limiter, and stream release.
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package limiter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func withBackground() context.Context {
|
||||
return types.WithBackgroundTask(context.Background())
|
||||
}
|
||||
|
||||
// TestGateOnlyGovernsBackground verifies interactive calls always pass through
|
||||
// (no-op release) and only background calls consult the installed limiter.
|
||||
func TestGateOnlyGovernsBackground(t *testing.T) {
|
||||
t.Cleanup(func() { SetGovernor(nil, 0) })
|
||||
SetGovernor(NewLocalLimiter(), 1)
|
||||
|
||||
// Interactive (non-background) ctx: never gated, even at limit 1.
|
||||
rel1 := Gate(context.Background(), "m")
|
||||
rel2 := Gate(context.Background(), "m")
|
||||
rel1()
|
||||
rel2()
|
||||
|
||||
// Background ctx: gated. First slot taken; second would block, so a
|
||||
// cancelled second acquire must fail open (return a usable release).
|
||||
held := Gate(withBackground(), "m")
|
||||
defer held()
|
||||
|
||||
ctx, cancel := context.WithTimeout(withBackground(), 30*time.Millisecond)
|
||||
defer cancel()
|
||||
rel := Gate(ctx, "m")
|
||||
if rel == nil {
|
||||
t.Fatal("cancelled background gate must fail open with a usable release")
|
||||
}
|
||||
rel()
|
||||
}
|
||||
|
||||
// TestGateDisabledWhenNoGovernor verifies Gate is a passthrough when no
|
||||
// governor is installed.
|
||||
func TestGateDisabledWhenNoGovernor(t *testing.T) {
|
||||
SetGovernor(nil, 0)
|
||||
if rel := Gate(withBackground(), "m"); rel == nil {
|
||||
t.Fatal("gate with no governor must return a usable release")
|
||||
} else {
|
||||
rel()
|
||||
}
|
||||
}
|
||||
|
||||
// TestLocalLimiterCaps verifies the in-process limiter enforces the cap and
|
||||
// releases slots.
|
||||
func TestLocalLimiterCaps(t *testing.T) {
|
||||
l := NewLocalLimiter()
|
||||
ctx := context.Background()
|
||||
|
||||
r1, _ := l.Acquire(ctx, "k", 2)
|
||||
r2, _ := l.Acquire(ctx, "k", 2)
|
||||
|
||||
// Third acquire must block while full.
|
||||
done := make(chan func(), 1)
|
||||
go func() {
|
||||
r3, _ := l.Acquire(ctx, "k", 2)
|
||||
done <- r3
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("third acquire should block while at limit")
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
|
||||
r1() // free a slot; the waiter proceeds
|
||||
select {
|
||||
case r3 := <-done:
|
||||
r3()
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("waiter should proceed after a slot frees")
|
||||
}
|
||||
r2()
|
||||
}
|
||||
|
||||
// TestLocalLimiterIndependentKeys verifies per-key budgets are independent.
|
||||
func TestLocalLimiterIndependentKeys(t *testing.T) {
|
||||
l := NewLocalLimiter()
|
||||
ctx := context.Background()
|
||||
ra, _ := l.Acquire(ctx, "a", 1)
|
||||
rb, _ := l.Acquire(ctx, "b", 1) // different key: must not block
|
||||
ra()
|
||||
rb()
|
||||
}
|
||||
|
||||
// TestLocalLimiterFailOpen verifies degraded inputs pass through.
|
||||
func TestLocalLimiterFailOpen(t *testing.T) {
|
||||
l := NewLocalLimiter()
|
||||
ctx := context.Background()
|
||||
if r, err := l.Acquire(ctx, "", 4); err != nil || r == nil {
|
||||
t.Fatal("empty key should fail open")
|
||||
} else {
|
||||
r()
|
||||
}
|
||||
if r, err := l.Acquire(ctx, "k", 0); err != nil || r == nil {
|
||||
t.Fatal("limit<=0 should fail open")
|
||||
} else {
|
||||
r()
|
||||
}
|
||||
}
|
||||
|
||||
// TestLocalLimiterReleaseIdempotent verifies double release doesn't
|
||||
// over-free a slot.
|
||||
func TestLocalLimiterReleaseIdempotent(t *testing.T) {
|
||||
l := NewLocalLimiter()
|
||||
ctx := context.Background()
|
||||
r1, _ := l.Acquire(ctx, "k", 1)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() { defer wg.Done(); r1() }()
|
||||
go func() { defer wg.Done(); r1() }()
|
||||
wg.Wait()
|
||||
|
||||
// After idempotent release the single slot is free again.
|
||||
r2, _ := l.Acquire(ctx, "k", 1)
|
||||
done := make(chan struct{})
|
||||
go func() { r3, _ := l.Acquire(ctx, "k", 1); r3(); close(done) }()
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("a double-release must not leave the slot permanently free")
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
r2()
|
||||
<-done
|
||||
}
|
||||
Reference in New Issue
Block a user