Skip to content

Commit 462a005

Browse files
committed
Update.
1 parent 5f2a3ad commit 462a005

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

service/rpc/server.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,11 @@ func checkAndResetCycleTraffic(clientID uint64) {
975975
// 读取上次重置参考时间(仍在读锁下,随后立即释放)
976976
lastResetTime := time.Time{}
977977
hasNextUpdate := false
978+
var nextUpdateValue time.Time
978979
if stats, exists := singleton.AlertsCycleTransferStatsStore[matchingAlert.ID]; exists && stats != nil {
979980
if nu, has := stats.NextUpdate[clientID]; has {
980981
hasNextUpdate = true
982+
nextUpdateValue = nu
981983
// 如果NextUpdate在当前周期开始之前,说明是上个周期的记录,可以作为重置参考
982984
if nu.Before(currentCycleStart) {
983985
lastResetTime = nu
@@ -990,9 +992,41 @@ func checkAndResetCycleTraffic(clientID uint64) {
990992
needReset := false
991993
now := time.Now()
992994

993-
// 重置条件:有NextUpdate记录且lastResetTime在当前周期之前
994-
if hasNextUpdate && now.After(currentCycleStart) && !lastResetTime.IsZero() && lastResetTime.Before(currentCycleStart) {
995+
// 重置判断逻辑:
996+
// 1. 如果没有NextUpdate记录,说明是首次运行,不需要重置
997+
// 2. 如果有NextUpdate记录:
998+
// - 若NextUpdate在当前周期之前:说明上个周期结束时设置的,需要重置
999+
// - 若NextUpdate在当前周期之后:正常情况,不需要重置
1000+
// - 若NextUpdate在当前周期内:需要检查服务器的累计流量是否为0
1001+
// 如果累计流量不为0,说明还没重置过,需要重置
1002+
1003+
if !hasNextUpdate {
1004+
// 没有记录,首次运行或数据丢失,不重置
1005+
return
1006+
}
1007+
1008+
if !lastResetTime.IsZero() && lastResetTime.Before(currentCycleStart) {
1009+
// NextUpdate在当前周期之前,需要重置
9951010
needReset = true
1011+
log.Printf("[周期流量重置] 服务器ID=%d 触发重置: NextUpdate=%s 在当前周期 %s 之前",
1012+
clientID, nextUpdateValue.Format("2006-01-02 15:04:05"), currentCycleStart.Format("2006-01-02 15:04:05"))
1013+
} else if nextUpdateValue.After(currentCycleStart) && nextUpdateValue.Before(currentCycleEnd) {
1014+
// NextUpdate在当前周期内,检查服务器累计流量
1015+
singleton.ServerLock.RLock()
1016+
server, exists := singleton.ServerList[clientID]
1017+
singleton.ServerLock.RUnlock()
1018+
1019+
if exists && server != nil {
1020+
// 如果累计流量不为0,说明还没重置,需要重置
1021+
if server.CumulativeNetInTransfer > 0 || server.CumulativeNetOutTransfer > 0 {
1022+
needReset = true
1023+
log.Printf("[周期流量重置] 服务器ID=%d 触发重置: NextUpdate=%s 在当前周期内,但累计流量不为0 (in=%d, out=%d)",
1024+
clientID, nextUpdateValue.Format("2006-01-02 15:04:05"),
1025+
server.CumulativeNetInTransfer, server.CumulativeNetOutTransfer)
1026+
} else {
1027+
log.Printf("[周期流量检查] 服务器ID=%d NextUpdate在当前周期内且累计流量为0,无需重置", clientID)
1028+
}
1029+
}
9961030
}
9971031

9981032
if !needReset {

0 commit comments

Comments
 (0)