@@ -972,16 +972,17 @@ func checkAndResetCycleTraffic(clientID uint64) {
972972 currentCycleStart := transferRule .GetTransferDurationStart ()
973973 currentCycleEnd := transferRule .GetTransferDurationEnd ()
974974
975- // 读取上次重置参考时间 (仍在读锁下,随后立即释放)
975+ // 读取上次重置时间 (仍在读锁下,随后立即释放)
976976 lastResetTime := time.Time {}
977- hasNextUpdate := false
977+ hasResetRecord := false
978978 if stats , exists := singleton .AlertsCycleTransferStatsStore [matchingAlert .ID ]; exists && stats != nil {
979- if nu , has := stats .NextUpdate [clientID ]; has {
980- hasNextUpdate = true
981- // 如果NextUpdate在当前周期开始之前,说明是上个周期的记录,可以作为重置参考
982- if nu .Before (currentCycleStart ) {
983- lastResetTime = nu
984- }
979+ // 确保LastResetTime map已初始化
980+ if stats .LastResetTime == nil {
981+ stats .LastResetTime = make (map [uint64 ]time.Time )
982+ }
983+ if resetTime , has := stats .LastResetTime [clientID ]; has {
984+ hasResetRecord = true
985+ lastResetTime = resetTime
985986 }
986987 }
987988 singleton .AlertsLock .RUnlock ()
@@ -990,9 +991,35 @@ func checkAndResetCycleTraffic(clientID uint64) {
990991 needReset := false
991992 now := time .Now ()
992993
993- // 重置条件:有NextUpdate记录且lastResetTime在当前周期之前
994- if hasNextUpdate && now .After (currentCycleStart ) && ! lastResetTime .IsZero () && lastResetTime .Before (currentCycleStart ) {
995- needReset = true
994+ // 重置条件:
995+ // - 如果从未重置过(!hasResetRecord),且当前时间在周期内,则首次初始化,不重置
996+ // - 如果有重置记录,检查lastResetTime是否在当前周期之前
997+ if hasResetRecord {
998+ // 有重置记录,检查是否需要重置
999+ if lastResetTime .Before (currentCycleStart ) && now .After (currentCycleStart ) {
1000+ needReset = true
1001+ log .Printf ("[周期流量重置] 服务器ID=%d 触发重置: 上次重置时间=%s 在当前周期 %s 之前" ,
1002+ clientID , lastResetTime .Format ("2006-01-02 15:04:05" ), currentCycleStart .Format ("2006-01-02 15:04:05" ))
1003+ }
1004+ } else {
1005+ if now .After (currentCycleStart ) {
1006+ // 没有重置记录但当前时间已进入周期,说明此前未按时重置,需要立即补偿
1007+ needReset = true
1008+ log .Printf ("[周期流量重置] 服务器ID=%d 首次检测发现缺失记录,立即补偿重置。当前周期开始=%s" ,
1009+ clientID , currentCycleStart .Format ("2006-01-02 15:04:05" ))
1010+ } else {
1011+ // 周期尚未开始,记录初始化时间,等待周期开始后再重置
1012+ singleton .AlertsLock .Lock ()
1013+ if stats , exists := singleton .AlertsCycleTransferStatsStore [matchingAlert .ID ]; exists && stats != nil {
1014+ if stats .LastResetTime == nil {
1015+ stats .LastResetTime = make (map [uint64 ]time.Time )
1016+ }
1017+ stats .LastResetTime [clientID ] = now
1018+ log .Printf ("[周期流量初始化] 服务器ID=%d 首次记录重置时间: %s" , clientID , now .Format ("2006-01-02 15:04:05" ))
1019+ }
1020+ singleton .AlertsLock .Unlock ()
1021+ return
1022+ }
9961023 }
9971024
9981025 if ! needReset {
@@ -1045,8 +1072,12 @@ func checkAndResetCycleTraffic(clientID uint64) {
10451072 if stats .Transfer == nil {
10461073 stats .Transfer = make (map [uint64 ]uint64 )
10471074 }
1075+ if stats .LastResetTime == nil {
1076+ stats .LastResetTime = make (map [uint64 ]time.Time )
1077+ }
10481078 stats .NextUpdate [clientID ] = now
10491079 stats .Transfer [clientID ] = 0
1080+ stats .LastResetTime [clientID ] = now // 更新上次重置时间
10501081 stats .From = currentCycleStart
10511082 stats .To = currentCycleEnd
10521083 }
0 commit comments