Skip to content

Commit 3054151

Browse files
committed
fix(dynamic): check all backends for load-balanced servers
Previously, checkServerConnectivity only checked the first backend address (from ServerInfo.Addr()), which could fail if that specific backend was unreachable from the proxy's network. Now checks all backends for load-balanced servers - if ANY backend is reachable via MC ping, the server is considered ready. This fixes timeout issues when the first backend is unreachable but others are available (e.g., remote address fails but localhost succeeds). For regular (non-load-balanced) servers, behavior is unchanged.
1 parent 0656bf4 commit 3054151

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

internal/dynamicserver/manager.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/go-logr/logr"
1111
"go.minekube.com/gate/pkg/edition/java/proxy"
1212

13+
"github.com/RMS-Server/RMS-Gate/internal/loadbalancer"
1314
"github.com/RMS-Server/RMS-Gate/internal/mcsmanager"
1415
"github.com/RMS-Server/RMS-Gate/internal/minecraft"
1516
)
@@ -185,16 +186,14 @@ func (m *Manager) waitForServerReady(serverName, instanceUUID string) bool {
185186
}
186187

187188
func (m *Manager) checkServerConnectivity(serverName string) bool {
188-
pollInterval := time.Duration(m.cfg.PollIntervalSeconds) * time.Second
189-
maxAttempts := m.cfg.ConnectivityTimeoutSeconds / m.cfg.PollIntervalSeconds
190-
191189
server := m.proxy.Server(serverName)
192190
if server == nil {
193191
m.log.Error(nil, "Server not registered in proxy", "server", serverName)
194192
return false
195193
}
196194

197-
addr := server.ServerInfo().Addr()
195+
pollInterval := time.Duration(m.cfg.PollIntervalSeconds) * time.Second
196+
maxAttempts := m.cfg.ConnectivityTimeoutSeconds / m.cfg.PollIntervalSeconds
198197

199198
for attempt := 0; attempt < maxAttempts; attempt++ {
200199
select {
@@ -203,20 +202,56 @@ func (m *Manager) checkServerConnectivity(serverName string) bool {
203202
default:
204203
}
205204

206-
err := minecraft.MCPing(addr, 3*time.Second)
207-
if err == nil {
208-
m.log.Info("Server is accepting connections (MC ping success)", "server", serverName)
205+
// Check connectivity - try all backends if load-balanced
206+
if m.checkAnyBackendReachable(server, serverName) {
209207
return true
210208
}
211209

212-
m.log.V(1).Info("Server not yet accepting connections", "server", serverName, "attempt", attempt+1, "error", err)
210+
m.log.V(1).Info("Server not yet accepting connections", "server", serverName, "attempt", attempt+1)
213211
time.Sleep(pollInterval)
214212
}
215213

216214
m.log.Error(nil, "Server connectivity check timed out", "server", serverName)
217215
return false
218216
}
219217

218+
func (m *Manager) checkAnyBackendReachable(server proxy.RegisteredServer, serverName string) bool {
219+
serverInfo := server.ServerInfo()
220+
221+
// Check if this is a load-balanced server with multiple backends
222+
type backendProvider interface {
223+
Backends() []*loadbalancer.Backend
224+
}
225+
226+
if lbInfo, ok := serverInfo.(backendProvider); ok {
227+
// Load-balanced server - check all backends
228+
backends := lbInfo.Backends()
229+
for _, backend := range backends {
230+
addr, err := net.ResolveTCPAddr("tcp", backend.Addr)
231+
if err != nil {
232+
m.log.V(1).Info("Failed to resolve backend address", "backend", backend.Addr, "error", err)
233+
continue
234+
}
235+
236+
if minecraft.MCPing(addr, 3*time.Second) == nil {
237+
m.log.Info("Server is accepting connections (MC ping success)",
238+
"server", serverName, "backend", backend.Addr)
239+
return true
240+
}
241+
}
242+
return false
243+
}
244+
245+
// Regular server - check single address
246+
addr := serverInfo.Addr()
247+
if minecraft.MCPing(addr, 3*time.Second) == nil {
248+
m.log.Info("Server is accepting connections (MC ping success)",
249+
"server", serverName, "addr", addr.String())
250+
return true
251+
}
252+
return false
253+
}
254+
220255
func (m *Manager) IsServerStarting(serverName string) bool {
221256
m.mu.Lock()
222257
defer m.mu.Unlock()

0 commit comments

Comments
 (0)