From 84c21803c00a238dde31b2331176b47fd562a437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Mon, 21 Sep 2026 12:04:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(distill):=20=E6=97=A5=E9=99=90=E9=A2=9D?= =?UTF-8?q?=E6=8C=89=E6=97=A5=E5=8E=86=E6=97=A5=E9=87=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=A8=E6=97=A5=E4=B8=8D=E5=BD=92=E9=9B=B6?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=92=B8=E9=A6=8F=E6=B0=B8=E4=B9=85=E5=86=BB?= =?UTF-8?q?=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit checkDailyLimit 原用 time.Since(dailyReset) > 24h 判断跨天, 若进程重启点晚于当日 00:00,则次日重启点之前不会归零 -> dailyUsed 达上限后持续 FALLBACK 直到重启时刻(2026-09-21 实测冻结 84min)。 改为比较年月日;跨日时清零并把 dailyReset 设为当日 00:00。 新增 engine_dailylimit_test.go 三个用例(同日保持/跨日清零/复现冻结场景)。 --- go/internal/distill/engine.go | 15 +++-- go/internal/distill/engine_dailylimit_test.go | 60 +++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 go/internal/distill/engine_dailylimit_test.go diff --git a/go/internal/distill/engine.go b/go/internal/distill/engine.go index b80f558..2f682c3 100644 --- a/go/internal/distill/engine.go +++ b/go/internal/distill/engine.go @@ -603,11 +603,16 @@ func isAllDigits(s string) bool { // checkDailyLimit 每日限额检查 func (e *Engine) checkDailyLimit() { - now := time.Now() - if now.Sub(e.dailyReset) > 24*time.Hour { - e.dailyUsed = 0 - e.dailyReset = now - } + now := time.Now() + if now.Day() != e.dailyReset.Day() || now.Month() != e.dailyReset.Month() || now.Year() != e.dailyReset.Year() { + e.dailyUsed = 0 + // Reset to start of day in the same location as e.dailyReset + loc := e.dailyReset.Location() + if loc == nil { + loc = time.UTC + } + e.dailyReset = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc) + } } // emitResult 发送蒸馏结果(回调) diff --git a/go/internal/distill/engine_dailylimit_test.go b/go/internal/distill/engine_dailylimit_test.go new file mode 100644 index 0000000..794b9cc --- /dev/null +++ b/go/internal/distill/engine_dailylimit_test.go @@ -0,0 +1,60 @@ +package distill + +import ( + "testing" + "time" +) + +// TestCheckDailyLimit_SameDayKeepsCounter 同日不应清零(防止把正常计数误清) +func TestCheckDailyLimit_SameDayKeepsCounter(t *testing.T) { + now := time.Now() + e := &Engine{ + dailyUsed: 4000, + dailyLimit: 5000, + dailyReset: now, // 今天 + } + e.checkDailyLimit() + if e.dailyUsed != 4000 { + t.Fatalf("同日应保持 dailyUsed=4000,实际=%d", e.dailyUsed) + } +} + +// TestCheckDailyLimit_NewDayResetsCounter 跨日必须清零(本次修复的核心行为) +func TestCheckDailyLimit_NewDayResetsCounter(t *testing.T) { + now := time.Now() + e := &Engine{ + dailyUsed: 5001, // 复现故障态:超过上限 1 + dailyLimit: 5000, + dailyReset: now.AddDate(0, 0, -1), // 昨天 + } + e.checkDailyLimit() + if e.dailyUsed != 0 { + t.Fatalf("跨日应清零 dailyUsed,实际=%d", e.dailyUsed) + } + // dailyReset 应被设为今天 00:00 + if e.dailyReset.Day() != now.Day() || e.dailyReset.Month() != now.Month() || e.dailyReset.Year() != now.Year() { + t.Fatalf("dailyReset 应更新为今天,实际=%v", e.dailyReset) + } + if e.dailyReset.Hour() != 0 || e.dailyReset.Minute() != 0 || e.dailyReset.Second() != 0 { + t.Fatalf("dailyReset 应为当日 00:00:00,实际=%v", e.dailyReset) + } +} + +// TestCheckDailyLimit_ExactlyLimitResets 复现 09-21 的 5001/5000 场景: +// dailyReset 是「昨天 18:39」(时间差 < 24h,但已跨日历日)——旧逻辑不会重置、永久冻结 +func TestCheckDailyLimit_ExactlyLimitResets(t *testing.T) { + now := time.Now() + e := &Engine{ + dailyUsed: 5001, + dailyLimit: 5000, + dailyReset: time.Date(now.Year(), now.Month(), now.Day()-1, 18, 39, 0, 0, time.Local), + } + // 断言:时间差确实 < 24h(这才能证明旧逻辑在此场景下不会重置) + if now.Sub(e.dailyReset) >= 24*time.Hour { + t.Skipf("当前时刻距昨日18:39已超24h,本用例失去鉴别力:diff=%v", now.Sub(e.dailyReset)) + } + e.checkDailyLimit() + if e.dailyUsed != 0 { + t.Fatalf("跨日历日应清零(旧逻辑会冻死到 18:39),实际=%d", e.dailyUsed) + } +}