Skip to content

Commit bd0d4a0

Browse files
authored
refactor: streamline connection cleanup in commonTCPLoop, commonUDPLoop, commonTCPOnce, and commonUDPOnce
1 parent 41d4761 commit bd0d4a0

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

internal/common.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -749,22 +749,7 @@ func (c *Common) commonTCPLoop() {
749749

750750
c.logger.Debug("Tunnel connection: get %v <- pool active %v", id, c.tunnelPool.Active())
751751

752-
// 注册取消函数
753-
c.cancelMap.Store(id, func() {
754-
if targetConn != nil {
755-
targetConn.SetReadDeadline(time.Now())
756-
}
757-
if remoteConn != nil {
758-
remoteConn.SetReadDeadline(time.Now())
759-
}
760-
})
761-
defer c.cancelMap.Delete(id)
762-
763752
defer func() {
764-
remoteConn.SetReadDeadline(time.Time{})
765-
c.tunnelPool.Put(id, remoteConn)
766-
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
767-
768753
// 发送关闭信号到对端
769754
closeURL := &url.URL{
770755
Scheme: "np",
@@ -775,10 +760,25 @@ func (c *Common) commonTCPLoop() {
775760
if err := c.writeSignal(closeURL); err != nil {
776761
c.logger.Error("commonTCPLoop: write close signal failed: %v", err)
777762
}
763+
764+
remoteConn.SetReadDeadline(time.Time{})
765+
c.tunnelPool.Put(id, remoteConn)
766+
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
778767
}()
779768

780769
c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
781770

771+
// 注册取消函数
772+
c.cancelMap.Store(id, func() {
773+
if targetConn != nil {
774+
targetConn.SetReadDeadline(time.Now())
775+
}
776+
if remoteConn != nil {
777+
remoteConn.SetReadDeadline(time.Now())
778+
}
779+
})
780+
defer c.cancelMap.Delete(id)
781+
782782
// 构建并发送启动URL到客户端
783783
launchURL := &url.URL{
784784
Scheme: "np",
@@ -873,10 +873,6 @@ func (c *Common) commonUDPLoop() {
873873
defer func() {
874874
cancel()
875875
c.cancelMap.Delete(id)
876-
// 重置池连接的读取超时
877-
remoteConn.SetReadDeadline(time.Time{})
878-
c.tunnelPool.Put(id, remoteConn)
879-
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
880876

881877
// 发送关闭信号到对端
882878
closeURL := &url.URL{
@@ -889,6 +885,11 @@ func (c *Common) commonUDPLoop() {
889885
c.logger.Error("commonUDPLoop: write close signal failed: %v", err)
890886
}
891887

888+
// 重置池连接的读取超时
889+
remoteConn.SetReadDeadline(time.Time{})
890+
c.tunnelPool.Put(id, remoteConn)
891+
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
892+
892893
// 清理UDP会话
893894
c.targetUDPSession.Delete(sessionKey)
894895
c.releaseSlot(true)
@@ -1069,10 +1070,6 @@ func (c *Common) commonTCPOnce(signalURL *url.URL) {
10691070
c.logger.Debug("Tunnel connection: get %v <- pool active %v", id, c.tunnelPool.Active())
10701071

10711072
defer func() {
1072-
remoteConn.SetReadDeadline(time.Time{})
1073-
c.tunnelPool.Put(id, remoteConn)
1074-
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
1075-
10761073
// 发送关闭信号到对端
10771074
closeURL := &url.URL{
10781075
Scheme: "np",
@@ -1083,6 +1080,10 @@ func (c *Common) commonTCPOnce(signalURL *url.URL) {
10831080
if err := c.writeSignal(closeURL); err != nil {
10841081
c.logger.Error("commonTCPOnce: write close signal failed: %v", err)
10851082
}
1083+
1084+
remoteConn.SetReadDeadline(time.Time{})
1085+
c.tunnelPool.Put(id, remoteConn)
1086+
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
10861087
}()
10871088

10881089
c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
@@ -1170,11 +1171,6 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
11701171
c.logger.Debug("Tunnel connection: %v <-> %v", remoteConn.LocalAddr(), remoteConn.RemoteAddr())
11711172

11721173
defer func() {
1173-
// 重置池连接的读取超时
1174-
remoteConn.SetReadDeadline(time.Time{})
1175-
c.tunnelPool.Put(id, remoteConn)
1176-
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
1177-
11781174
// 发送关闭信号到对端
11791175
closeURL := &url.URL{
11801176
Scheme: "np",
@@ -1185,6 +1181,11 @@ func (c *Common) commonUDPOnce(signalURL *url.URL) {
11851181
if err := c.writeSignal(closeURL); err != nil {
11861182
c.logger.Error("commonUDPOnce: write close signal failed: %v", err)
11871183
}
1184+
1185+
// 重置池连接的读取超时
1186+
remoteConn.SetReadDeadline(time.Time{})
1187+
c.tunnelPool.Put(id, remoteConn)
1188+
c.logger.Debug("Tunnel connection: put %v -> pool active %v", id, c.tunnelPool.Active())
11881189
}()
11891190

11901191
var targetConn net.Conn

0 commit comments

Comments
 (0)