@@ -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,28 @@ 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+ // 首次运行,初始化LastResetTime为当前时间
1006+ singleton .AlertsLock .Lock ()
1007+ if stats , exists := singleton .AlertsCycleTransferStatsStore [matchingAlert .ID ]; exists && stats != nil {
1008+ if stats .LastResetTime == nil {
1009+ stats .LastResetTime = make (map [uint64 ]time.Time )
1010+ }
1011+ stats .LastResetTime [clientID ] = now
1012+ log .Printf ("[周期流量初始化] 服务器ID=%d 首次记录重置时间: %s" , clientID , now .Format ("2006-01-02 15:04:05" ))
1013+ }
1014+ singleton .AlertsLock .Unlock ()
1015+ return
9961016 }
9971017
9981018 if ! needReset {
@@ -1045,8 +1065,12 @@ func checkAndResetCycleTraffic(clientID uint64) {
10451065 if stats .Transfer == nil {
10461066 stats .Transfer = make (map [uint64 ]uint64 )
10471067 }
1068+ if stats .LastResetTime == nil {
1069+ stats .LastResetTime = make (map [uint64 ]time.Time )
1070+ }
10481071 stats .NextUpdate [clientID ] = now
10491072 stats .Transfer [clientID ] = 0
1073+ stats .LastResetTime [clientID ] = now // 更新上次重置时间
10501074 stats .From = currentCycleStart
10511075 stats .To = currentCycleEnd
10521076 }
0 commit comments