Allow mc-router to scale a backend StatefulSet while routing traffic to a proxy via proxyServerName (#512)

This commit is contained in:
Chris Farhood
2026-02-13 08:07:27 -05:00
committed by GitHub
parent 1f86f88536
commit 21f349c2da
16 changed files with 527 additions and 135 deletions
+13 -8
View File
@@ -77,6 +77,7 @@ func NewConnector(ctx context.Context, metrics *ConnectorMetrics, sendProxyProto
recordLogins: recordLogins,
autoScaleUpAllowDenyConfig: autoScaleUpAllowDenyConfig,
activeConnections: NewActiveConnections(),
scaleActiveConnections: NewActiveConnections(),
}
}
@@ -95,6 +96,7 @@ type Connector struct {
trustedProxyNets []*net.IPNet
totalActiveConnections int32
activeConnections *ActiveConnections
scaleActiveConnections *ActiveConnections
connectionsCond *sync.Cond
ngrok NgrokConnector
clientFilter *ClientFilter
@@ -484,7 +486,7 @@ func (c *Connector) readPlayerInfo(protocolVersion mcproto.ProtocolVersion, buff
}
}
func (c *Connector) cleanupBackendConnection(clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string, cleanupMetrics bool, checkScaleDown bool) {
func (c *Connector) cleanupBackendConnection(clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string, scalingTarget string, cleanupMetrics bool, checkScaleDown bool) {
if c.connectionNotifier != nil {
err := c.connectionNotifier.NotifyDisconnected(c.ctx, clientAddr, serverAddress, playerInfo, backendHostPort)
if err != nil {
@@ -501,6 +503,8 @@ func (c *Connector) cleanupBackendConnection(clientAddr net.Addr, serverAddress
With("server_address", serverAddress).
Set(float64(c.activeConnections.GetCount(backendHostPort)))
c.scaleActiveConnections.Decrement(scalingTarget)
if c.recordLogins && playerInfo != nil {
c.metrics.ServerActivePlayer.
With("player_name", playerInfo.Name).
@@ -514,8 +518,8 @@ func (c *Connector) cleanupBackendConnection(clientAddr net.Addr, serverAddress
WithField("backendHostPort", backendHostPort).
WithField("connectionCount", c.activeConnections.GetCount(backendHostPort)).
Info("Closed connection to backend")
if checkScaleDown && c.activeConnections.GetCount(backendHostPort) <= 0 {
DownScaler.Begin(backendHostPort)
if checkScaleDown && c.scaleActiveConnections.GetCount(scalingTarget) <= 0 {
DownScaler.Begin(scalingTarget)
}
c.connectionsCond.Signal()
}
@@ -523,12 +527,12 @@ func (c *Connector) cleanupBackendConnection(clientAddr net.Addr, serverAddress
func (c *Connector) findAndConnectBackend(frontendConn net.Conn,
clientAddr net.Addr, preReadContent io.Reader, serverAddress string, playerInfo *PlayerInfo, nextState mcproto.State, isLegacy bool, clientProtocol int) {
backendHostPort, resolvedHost, waker, _ := Routes.FindBackendForServerAddress(c.ctx, serverAddress)
backendHostPort, resolvedHost, scalingTarget, waker, _ := Routes.FindBackendForServerAddress(c.ctx, serverAddress)
cleanupMetrics := false
cleanupCheckScaleDown := false
defer func() {
c.cleanupBackendConnection(clientAddr, serverAddress, playerInfo, backendHostPort, cleanupMetrics, cleanupCheckScaleDown)
c.cleanupBackendConnection(clientAddr, serverAddress, playerInfo, backendHostPort, scalingTarget, cleanupMetrics, cleanupCheckScaleDown)
}()
if waker != nil && nextState > mcproto.StateStatus {
@@ -541,8 +545,8 @@ func (c *Connector) findAndConnectBackend(frontendConn net.Conn,
Debug("checked if player is allowed to wake up the server")
if serverAllowsPlayer {
// Cancel down scaler if active before scale up
if backendHostPort != "" {
DownScaler.Cancel(backendHostPort)
if scalingTarget != "" {
DownScaler.Cancel(scalingTarget)
}
cleanupCheckScaleDown = true
logrus.WithField("serverAddress", serverAddress).Info("Waking up backend server")
@@ -558,7 +562,7 @@ func (c *Connector) findAndConnectBackend(frontendConn net.Conn,
return
}
// Cancel again in case any routes were changed during wake up
DownScaler.Cancel(newBackendHostPort)
DownScaler.Cancel(scalingTarget)
backendHostPort = newBackendHostPort
logrus.WithFields(logrus.Fields{
"serverAddress": serverAddress,
@@ -642,6 +646,7 @@ func (c *Connector) findAndConnectBackend(frontendConn net.Conn,
atomic.AddInt32(&c.totalActiveConnections, 1)))
c.activeConnections.Increment(backendHostPort)
c.scaleActiveConnections.Increment(scalingTarget)
c.metrics.ServerActiveConnections.
With("server_address", serverAddress).
Set(float64(c.activeConnections.GetCount(backendHostPort)))