@@ -974,10 +974,15 @@ func checkAndResetCycleTraffic(clientID uint64) {
974974
975975 // 读取上次重置参考时间(仍在读锁下,随后立即释放)
976976 lastResetTime := time.Time {}
977+ var nextUpdate time.Time
978+ hasNextUpdate := false
977979 if stats , exists := singleton .AlertsCycleTransferStatsStore [matchingAlert .ID ]; exists && stats != nil {
978- if nextUpdate , has := stats .NextUpdate [clientID ]; has {
979- if nextUpdate .Before (currentCycleStart ) {
980- lastResetTime = nextUpdate
980+ if nu , has := stats .NextUpdate [clientID ]; has {
981+ hasNextUpdate = true
982+ nextUpdate = nu
983+ // 如果NextUpdate在当前周期开始之前,说明是上个周期的记录,可以作为重置参考
984+ if nu .Before (currentCycleStart ) {
985+ lastResetTime = nu
981986 }
982987 }
983988 }
@@ -986,9 +991,47 @@ func checkAndResetCycleTraffic(clientID uint64) {
986991 // 2) 判断是否需要重置(锁外计算)
987992 needReset := false
988993 now := time .Now ()
989- if ! lastResetTime .IsZero () && now .After (currentCycleStart ) && lastResetTime .Before (currentCycleStart ) {
990- needReset = true
994+
995+ // 添加调试日志
996+ if clientID == 30 || clientID == 17 { // 只记录特定服务器的日志避免日志过多
997+ log .Printf ("[流量重置调试] 服务器ID=%d, 当前时间=%s, 周期开始=%s, 周期结束=%s" ,
998+ clientID , now .Format ("2006-01-02 15:04:05" ),
999+ currentCycleStart .Format ("2006-01-02 15:04:05" ),
1000+ currentCycleEnd .Format ("2006-01-02 15:04:05" ))
1001+ log .Printf ("[流量重置调试] 服务器ID=%d, hasNextUpdate=%v, NextUpdate=%s, lastResetTime=%s, lastResetTime.IsZero=%v" ,
1002+ clientID , hasNextUpdate , nextUpdate .Format ("2006-01-02 15:04:05" ),
1003+ lastResetTime .Format ("2006-01-02 15:04:05" ), lastResetTime .IsZero ())
9911004 }
1005+
1006+ // 重置条件:
1007+ // 1. 有NextUpdate记录
1008+ // 2. 当前时间已进入新周期
1009+ // 3. lastResetTime在当前周期之前(如果有的话)或NextUpdate本身在当前周期之前
1010+ if hasNextUpdate && now .After (currentCycleStart ) {
1011+ // 情况1: lastResetTime不为空且在周期开始之前(正常情况)
1012+ if ! lastResetTime .IsZero () && lastResetTime .Before (currentCycleStart ) {
1013+ needReset = true
1014+ } else if lastResetTime .IsZero () {
1015+ // 情况2: lastResetTime为空,说明NextUpdate >= currentCycleStart
1016+ // 这种情况发生在:上个周期末更新了NextUpdate为新周期的时间
1017+ // 需要检查服务器的累计流量来判断是否需要重置
1018+ singleton .ServerLock .RLock ()
1019+ server := singleton .ServerList [clientID ]
1020+ if server != nil && (server .CumulativeNetInTransfer > 0 || server .CumulativeNetOutTransfer > 0 ) {
1021+ needReset = true
1022+ if clientID == 30 || clientID == 17 {
1023+ log .Printf ("[流量重置调试] 服务器ID=%d, NextUpdate在新周期内但服务器有累计流量,触发重置。入站=%d, 出站=%d" ,
1024+ clientID , server .CumulativeNetInTransfer , server .CumulativeNetOutTransfer )
1025+ }
1026+ }
1027+ singleton .ServerLock .RUnlock ()
1028+ }
1029+ }
1030+
1031+ if clientID == 30 || clientID == 17 {
1032+ log .Printf ("[流量重置调试] 服务器ID=%d, needReset=%v" , clientID , needReset )
1033+ }
1034+
9921035 if ! needReset {
9931036 return
9941037 }
0 commit comments