distill: 尺寸感知超时(120s→90s+tok/400s, 封顶300s)+ 超时不重试

容量墙:15B 上 84.7K prompt prefill 实测 165–170s @≈500 tok/s,撞死 120s 客户端死线
⇒ 大集必然 fallback 降级入库(最富的记忆源被打成残次品)。9B 886 tok/s 时 96s 恰好塞得下,
换模型后这条墙才显形。
同时:超时不再重试(每集省 ~4–6 分钟槽位;22:21–22:37 的 10+ 次 cancel = 3~4 集×3 次尝试的算术)。
This commit is contained in:
小怡
2026-12-06 02:19:02 +08:00
parent 628327e1b4
commit 6d06cb4e0c
+45 -3
View File
@@ -5,11 +5,14 @@ package distill
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"os"
"strconv"
@@ -285,7 +288,10 @@ func NewEngine(llmEndpoint, llmModel, apiKey string) *Engine {
batchTimeout: 5 * time.Minute,
dailyLimit: 5000,
flushTokenThreshold: 2000, // LightMem short-term buffer
client: &http.Client{Timeout: 120 * time.Second},
// 2026-12-06 容量墙修复:全局 120s 死线对 15B 的大 prompt 是硬伤
// (实测 84.7K prompt prefill 165–170s @≈500 tok/s;9B 886 tok/s 时 96s 恰好塞得下)。
// 改为 client 无全局超时,由 callLLM5D 按 prompt 尺寸给每请求死线(下限 120s,上限 300s)。
client: &http.Client{},
}
// AC2:启动即探测端点槽位,把并发降到 min(槽数, 封顶)。探测失败保守取 1
// (宁串行也不打爆未知槽位);之后每 flush 周期可 RefreshConcurrency() 再探。
@@ -851,9 +857,22 @@ SCORE|五个数字用竖线分隔
var respBody []byte
var lastStatus int
// 尺寸感知死线:estTok 保守按 2 字符/token 估(中文≈1 字符/token,英文≈4 字符/token)。
// 下限 120s 保持与旧行为一致;上限 300s 防单请求无限占槽。
estTok := len(prompt) / 2
timeout := time.Duration(90+estTok/400) * time.Second
if timeout < 120*time.Second {
timeout = 120 * time.Second
}
if timeout > 300*time.Second {
timeout = 300 * time.Second
}
for attempt := 0; ; attempt++ {
req, reqErr := http.NewRequest("POST", e.LLMEndpoint, bytes.NewReader(jsonBody))
ctx, cancel := context.WithTimeout(context.Background(), timeout)
req, reqErr := http.NewRequestWithContext(ctx, "POST", e.LLMEndpoint, bytes.NewReader(jsonBody))
if reqErr != nil {
cancel()
return LLMResponse{}, reqErr
}
req.Header.Set("Content-Type", "application/json")
@@ -862,8 +881,16 @@ SCORE|五个数字用竖线分隔
}
resp, doErr := e.client.Do(req)
cancel()
if doErr != nil {
// 网络/超时错误:可重试;到上限即上抛,让上层走 fallback。
// 2026-12-06:超时不重试——超时说明服务端正被更大的请求占槽或模型吃不下,
// 再等 2 次只会把槽位多烧 2×timeout(实测每集烧 4–6 分钟,22:21–22:37 那 10+ 次 cancel 就是这么来的)。
if isTimeoutErr(doErr) {
log.Printf("[distill] callLLM5D timeout after %v (prompt=%d bytes ≈%d tok): %v",
timeout, len(prompt), estTok, doErr)
return LLMResponse{}, fmt.Errorf("callLLM5D timeout after %v (prompt≈%d tok): %w", timeout, estTok, doErr)
}
// 其他网络错误可重试;到上限即上抛,让上层走 fallback。
if attempt < maxRetries {
d := backoffDelay(attempt, initialDelay, maxDelay, rng)
log.Printf("[distill] callLLM5D transient err (attempt %d/%d): %v → retry in %v",
@@ -990,6 +1017,21 @@ SCORE|五个数字用竖线分隔
// AC1 辅助:指数退避 + 抖动。
// 基础为指数(initialDelay << attempt),封顶 maxDelay;叠加 ±25% 抖动避免惊群。
// 用 math/rand(Go 1.20+ 全局自动随机化,无需手动 seed)。
// isTimeoutErr 判断是否为超时类错误(含 url.Error 包裹的 net.Error.Timeout 与 ctx deadline)。
func isTimeoutErr(err error) bool {
if err == nil {
return false
}
if errors.Is(err, context.DeadlineExceeded) {
return true
}
var ne net.Error
if errors.As(err, &ne) && ne.Timeout() {
return true
}
return false
}
func newRand() *rand.Rand { return rand.New(rand.NewSource(time.Now().UnixNano())) }
// backoffDelay 返回第 attempt 次(0 基)重试前应等待的时长: