@@ -123,16 +123,39 @@ func (s *ServerHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.R
123123 // 检查是否是服务器重启
124124 isRestart := false
125125 if singleton .ServerList [clientID ].Host != nil && singleton .ServerList [clientID ].State != nil {
126- // 如果当前流量小于之前保存的流量,可能是重启
127- if originalNetInTransfer < singleton .ServerList [clientID ].State .NetInTransfer ||
128- originalNetOutTransfer < singleton .ServerList [clientID ].State .NetOutTransfer {
129- isRestart = true
130- log .Printf ("检测到服务器 %s 可能重启: 当前入站=%d < 之前入站=%d 或 当前出站=%d < 之前出站=%d" ,
131- singleton .ServerList [clientID ].Name ,
132- originalNetInTransfer ,
133- singleton .ServerList [clientID ].State .NetInTransfer ,
134- originalNetOutTransfer ,
135- singleton .ServerList [clientID ].State .NetOutTransfer )
126+ // 获取之前的流量值(原始值,不包含累计)
127+ prevNetIn := singleton .ServerList [clientID ].State .NetInTransfer - singleton .ServerList [clientID ].CumulativeNetInTransfer
128+ prevNetOut := singleton .ServerList [clientID ].State .NetOutTransfer - singleton .ServerList [clientID ].CumulativeNetOutTransfer
129+
130+ // 计算流量差值的绝对值和百分比
131+ netInDiff := float64 (prevNetIn - originalNetInTransfer )
132+ netOutDiff := float64 (prevNetOut - originalNetOutTransfer )
133+
134+ // 设置重启检测阈值:
135+ // 1. 流量回退超过1GB且回退幅度超过50%
136+ // 2. 或者两个方向的流量都大幅回退超过500MB
137+ const restartThresholdBytes = 1024 * 1024 * 1024 // 1GB
138+ const restartThresholdSmall = 500 * 1024 * 1024 // 500MB
139+ const restartPercentThreshold = 0.5 // 50%
140+
141+ if prevNetIn > 0 && prevNetOut > 0 { // 确保有历史数据
142+ netInPercent := netInDiff / float64 (prevNetIn )
143+ netOutPercent := netOutDiff / float64 (prevNetOut )
144+
145+ // 重启条件:大幅流量回退
146+ largeRollback := (netInDiff > restartThresholdBytes && netInPercent > restartPercentThreshold ) ||
147+ (netOutDiff > restartThresholdBytes && netOutPercent > restartPercentThreshold )
148+
149+ // 或者两个方向都有明显回退
150+ bothRollback := netInDiff > restartThresholdSmall && netOutDiff > restartThresholdSmall
151+
152+ if largeRollback || bothRollback {
153+ isRestart = true
154+ log .Printf ("检测到服务器 %s 可能重启: 入站回退=%.2fMB(%.1f%%), 出站回退=%.2fMB(%.1f%%)" ,
155+ singleton .ServerList [clientID ].Name ,
156+ netInDiff / (1024 * 1024 ), netInPercent * 100 ,
157+ netOutDiff / (1024 * 1024 ), netOutPercent * 100 )
158+ }
136159 }
137160 }
138161
0 commit comments