fix(distill): 日限额按日历日重置,修复跨日不归零导致蒸馏永久冻结
checkDailyLimit 原用 time.Since(dailyReset) > 24h 判断跨天, 若进程重启点晚于当日 00:00,则次日重启点之前不会归零 -> dailyUsed 达上限后持续 FALLBACK 直到重启时刻(2026-09-21 实测冻结 84min)。 改为比较年月日;跨日时清零并把 dailyReset 设为当日 00:00。 新增 engine_dailylimit_test.go 三个用例(同日保持/跨日清零/复现冻结场景)。
This commit is contained in:
@@ -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 发送蒸馏结果(回调)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user