@@ -92,7 +92,7 @@ var ServerHandlerSingleton *ServerHandler
9292// goroutine 计数器,用于监控 RequestTask goroutine 数量
9393var (
9494 activeRequestTaskGoroutines int64
95- maxRequestTaskGoroutines int64 = 500 // 最大允许的 RequestTask goroutine 数量
95+ maxRequestTaskGoroutines int64 = 100 // 大幅降低最大允许的 RequestTask goroutine 数量
9696)
9797
9898type ServerHandler struct {
@@ -209,10 +209,18 @@ func (s *ServerHandler) RequestTask(h *pb.Host, stream pb.ServerService_RequestT
209209 current := activeRequestTaskGoroutines
210210 total := int64 (runtime .NumGoroutine ())
211211
212- // 如果总 goroutine 数量过多,拒绝新连接
213- if total > 1000 {
214- log .Printf ("警告:总 goroutine 数量过多 (%d),拒绝新的 RequestTask 连接" , total )
215- return - 1
212+ // 如果总 goroutine 数量过多,先尝试强制清理
213+ if total > 250 {
214+ log .Printf ("警告:总 goroutine 数量过多 (%d),尝试强制清理" , total )
215+ cleaned := ForceCleanupStaleConnections ()
216+ if cleaned > 0 {
217+ log .Printf ("强制清理了 %d 个连接,当前 goroutine 数量: %d" , cleaned , runtime .NumGoroutine ())
218+ }
219+ // 清理后仍然过多,拒绝新连接
220+ if runtime .NumGoroutine () > 300 {
221+ log .Printf ("清理后 goroutine 数量仍过多 (%d),拒绝新的 RequestTask 连接" , runtime .NumGoroutine ())
222+ return - 1
223+ }
216224 }
217225
218226 // 如果 RequestTask goroutine 数量超过限制,拒绝新连接
@@ -262,7 +270,7 @@ func (s *ServerHandler) RequestTask(h *pb.Host, stream pb.ServerService_RequestT
262270 singleton .ServerLock .RUnlock ()
263271
264272 // 创建一个带超时的上下文,大幅减少超时时间避免goroutine泄漏
265- ctx , cancel := context .WithTimeout (stream .Context (), 2 * time .Minute )
273+ ctx , cancel := context .WithTimeout (stream .Context (), 1 * time .Minute ) // 从2分钟缩短到1分钟
266274 defer cancel ()
267275
268276 // 监听连接状态,当连接断开时自动清理
@@ -275,7 +283,7 @@ func (s *ServerHandler) RequestTask(h *pb.Host, stream pb.ServerService_RequestT
275283 }()
276284
277285 // 使用定时器避免无限等待,缩短检查间隔
278- ticker := time .NewTicker (15 * time .Second )
286+ ticker := time .NewTicker (10 * time .Second ) // 从15秒进一步减少到10秒
279287 defer ticker .Stop ()
280288
281289 for {
@@ -333,7 +341,7 @@ func (s *ServerHandler) RequestTask(h *pb.Host, stream pb.ServerService_RequestT
333341 // 定期记录 goroutine 状态,帮助监控泄漏
334342 totalGoroutines := runtime .NumGoroutine ()
335343 activeRequestTasks := activeRequestTaskGoroutines
336- if totalGoroutines > 800 || activeRequestTasks > 400 {
344+ if totalGoroutines > 200 || activeRequestTasks > 80 {
337345 log .Printf ("Goroutine 监控 - 总数: %d, RequestTask: %d, 服务器: %d" ,
338346 totalGoroutines , activeRequestTasks , clientID )
339347 }
@@ -357,7 +365,7 @@ func (s *ServerHandler) RequestTask(h *pb.Host, stream pb.ServerService_RequestT
357365 }()
358366
359367 // 等待连接关闭或超时,使用定时器避免无限等待,缩短检查间隔
360- ticker := time .NewTicker (30 * time .Second )
368+ ticker := time .NewTicker (20 * time .Second ) // 从30秒进一步减少到20秒
361369 defer ticker .Stop ()
362370
363371 for {
@@ -399,7 +407,7 @@ func (s *ServerHandler) RequestTask(h *pb.Host, stream pb.ServerService_RequestT
399407
400408 // 检查 goroutine 泄漏情况,如果过多则强制退出
401409 totalGoroutines := runtime .NumGoroutine ()
402- if totalGoroutines > 1200 {
410+ if totalGoroutines > 400 {
403411 log .Printf ("严重警告:goroutine 数量过多 (%d),强制断开服务器 %d 的连接以防止崩溃" ,
404412 totalGoroutines , clientID )
405413 return fmt .Errorf ("goroutine 数量过多,强制断开连接" )
0 commit comments