Skip to content

Commit eda4fb0

Browse files
refactor: improve handling of restored connections in Tunnel
- Streamline the logic for accepting restored connections by ensuring proper management of goroutines and connection closure. - Ensure that the connection is closed gracefully if no active client is available after a specified wait time. - Enhance logging for connection attempts to provide clearer feedback on client availability.
1 parent 15e3857 commit eda4fb0

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

server/internal/server/server.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,36 +1027,38 @@ func (t *Tunnel) acceptRestoredConnections(s *Server) {
10271027
log.Printf("🌐 External connection attempt to restored port %s from %s:%d",
10281028
t.RemotePort, clientAddr.IP.String(), clientAddr.Port)
10291029

1030-
// Check if the tunnel now has an active client
1031-
t.wg.Add(1)
1032-
go func(c net.Conn) {
1033-
defer t.wg.Done()
1034-
defer c.Close()
1035-
1036-
// Check if client is available (with a short wait)
1037-
maxWaitTime := 2 * time.Second
1038-
checkInterval := 100 * time.Millisecond
1039-
waited := time.Duration(0)
1040-
1041-
for waited < maxWaitTime {
1042-
if t.Client != nil {
1043-
// Client is now connected, handle this connection normally
1044-
log.Printf("✅ Client reconnected for restored port %s, handling connection", t.RemotePort)
1045-
t.handleConnection(c)
1046-
return
1047-
}
1048-
time.Sleep(checkInterval)
1049-
waited += checkInterval
1030+
// Check if the tunnel now has an active client
1031+
t.wg.Add(1)
1032+
go func(c net.Conn) {
1033+
// Check if client is available (with a short wait)
1034+
maxWaitTime := 2 * time.Second
1035+
checkInterval := 100 * time.Millisecond
1036+
waited := time.Duration(0)
1037+
1038+
for waited < maxWaitTime {
1039+
if t.Client != nil {
1040+
// Client is now connected, handle this connection normally
1041+
log.Printf("✅ Client reconnected for restored port %s, handling connection", t.RemotePort)
1042+
t.handleConnection(c)
1043+
// handleConnection will call t.wg.Done() and close the connection
1044+
return
10501045
}
1046+
time.Sleep(checkInterval)
1047+
waited += checkInterval
1048+
}
10511049

1052-
// Client still not available, close connection gracefully
1053-
log.Printf("⏰ Client not available for restored port %s, closing connection from %s:%d",
1054-
t.RemotePort, clientAddr.IP.String(), clientAddr.Port)
1050+
// Client still not available, close connection gracefully
1051+
// We need to call Done() and Close() here since handleConnection was not called
1052+
defer t.wg.Done()
1053+
defer c.Close()
1054+
1055+
log.Printf("⏰ Client not available for restored port %s, closing connection from %s:%d",
1056+
t.RemotePort, clientAddr.IP.String(), clientAddr.Port)
10551057

1056-
// Log the connection attempt
1057-
t.logConnectionAttempt(clientAddr.IP.String(), clientAddr.Port, "error",
1058-
"Tunnel client not connected")
1059-
}(conn)
1058+
// Log the connection attempt
1059+
t.logConnectionAttempt(clientAddr.IP.String(), clientAddr.Port, "error",
1060+
"Tunnel client not connected")
1061+
}(conn)
10601062
}
10611063
}
10621064
}

0 commit comments

Comments
 (0)