@@ -395,17 +395,25 @@ func (m *Manager) listenSSE(ctx context.Context, conn *EndpointConnection) {
395395 if ! lastEventTime .IsZero () {
396396 idleDuration := time .Since (lastEventTime )
397397 if idleDuration > maxIdleTime {
398- log .Warnf ("[Master-%d#SSE]检测到僵尸连接:已%v未收到任何事件,主动断开重连" ,
399- conn .EndpointID , idleDuration .Round (time .Second ))
400- conn .SetConnected (false )
401- if ! conn .IsManuallyDisconnected () {
402- m .markEndpointFail (conn .EndpointID )
403- conn .ResetLastConnectAttempt ()
398+ // 先检查是否有活跃的隧道,如果有活跃隧道才判定为僵尸连接
399+ hasActive := m .hasActiveTunnels (conn .EndpointID )
400+ if hasActive {
401+ log .Warnf ("[Master-%d#SSE]检测到僵尸连接:已%v未收到任何事件(存在活跃隧道),主动断开重连" ,
402+ conn .EndpointID , idleDuration .Round (time .Second ))
403+ conn .SetConnected (false )
404+ if ! conn .IsManuallyDisconnected () {
405+ m .markEndpointFail (conn .EndpointID )
406+ conn .ResetLastConnectAttempt ()
407+ }
408+ return
409+ } else {
410+ log .Debugf ("[Master-%d#SSE]空闲检查:已%v未收到事件,但无活跃隧道,跳过僵尸连接检测" ,
411+ conn .EndpointID , idleDuration .Round (time .Second ))
404412 }
405- return
413+ } else {
414+ log .Debugf ("[Master-%d#SSE]空闲检查:距离上次事件%v(最大允许%v)" ,
415+ conn .EndpointID , idleDuration .Round (time .Second ), maxIdleTime )
406416 }
407- log .Debugf ("[Master-%d#SSE]空闲检查:距离上次事件%v(最大允许%v)" ,
408- conn .EndpointID , idleDuration .Round (time .Second ), maxIdleTime )
409417 }
410418 }
411419 case ev , ok := <- events :
@@ -492,6 +500,23 @@ func (m *Manager) GetConnectionStatus() map[int64]map[string]interface{} {
492500 return status
493501}
494502
503+ // hasActiveTunnels 检查端点是否有活跃的隧道(状态为 running)
504+ func (m * Manager ) hasActiveTunnels (endpointID int64 ) bool {
505+ var count int
506+ err := m .db .QueryRow (`
507+ SELECT COUNT(*)
508+ FROM tunnels
509+ WHERE endpoint_id = ? AND status = 'running'
510+ ` , endpointID ).Scan (& count )
511+
512+ if err != nil {
513+ log .Errorf ("[Master-%d#SSE]查询活跃隧道数量失败: %v" , endpointID , err )
514+ return false
515+ }
516+
517+ return count > 0
518+ }
519+
495520// markEndpointFail 更新端点状态为 FAIL
496521func (m * Manager ) markEndpointFail (endpointID int64 ) {
497522 // 更新端点状态为 FAIL,避免重复写
0 commit comments