Skip to content

Commit b0a5a2e

Browse files
committed
refactor: bundle config for connector
1 parent 0132bac commit b0a5a2e

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

cmd/mc-router/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,18 @@ func main() {
170170
trustedIpNets = append(trustedIpNets, ipNet)
171171
}
172172

173-
connector := server.NewConnector(metricsBuilder.BuildConnectorMetrics(), config.UseProxyProtocol, config.ReceiveProxyProtocol, trustedIpNets, config.RecordLogins, autoScaleUpAllowDenyConfig, config.FakeOnline, config.FakeOnlineMOTD)
173+
connectorConfig := server.ConnectorConfig{
174+
SendProxyProto: config.UseProxyProtocol,
175+
ReceiveProxyProto: config.ReceiveProxyProtocol,
176+
TrustedProxyNets: trustedIpNets,
177+
RecordLogins: config.RecordLogins,
178+
AutoScaleUpAllowDenyConfig: autoScaleUpAllowDenyConfig,
179+
AutoScaleUp: config.AutoScaleUp,
180+
FakeOnline: config.FakeOnline,
181+
FakeOnlineMOTD: config.FakeOnlineMOTD,
182+
}
183+
184+
connector := server.NewConnector(metricsBuilder.BuildConnectorMetrics(), connectorConfig)
174185

175186
clientFilter, err := server.NewClientFilter(config.ClientsToAllow, config.ClientsToDeny)
176187
if err != nil {

server/connector.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool, receiveProxyPr
116116

117117
return &Connector{
118118
metrics: metrics,
119-
sendProxyProto: sendProxyProto,
120119
connectionsCond: sync.NewCond(&sync.Mutex{}),
121120
receiveProxyProto: receiveProxyProto,
122121
trustedProxyNets: trustedProxyNets,
@@ -129,20 +128,14 @@ func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool, receiveProxyPr
129128
type Connector struct {
130129
state mcproto.State
131130
metrics *ConnectorMetrics
132-
sendProxyProto bool
133-
receiveProxyProto bool
134-
recordLogins bool
135-
trustedProxyNets []*net.IPNet
136131

137132
activeConnections int32
138133
serverMetrics *ServerMetrics
139134
connectionsCond *sync.Cond
140135
ngrokToken string
141136
clientFilter *ClientFilter
142-
autoScaleUpAllowDenyConfig *AllowDenyConfig
143137

144-
fakeOnline bool
145-
fakeOnlineMOTD string
138+
config ConnectorConfig
146139

147140
connectionNotifier ConnectionNotifier
148141
}
@@ -187,7 +180,7 @@ func (c *Connector) createListener(ctx context.Context, listenAddress string) (n
187180
}
188181
logrus.WithField("listenAddress", listenAddress).Info("Listening for Minecraft client connections")
189182

190-
if c.receiveProxyProto {
183+
if c.config.ReceiveProxyProto {
191184
proxyListener := &proxyproto.Listener{
192185
Listener: listener,
193186
Policy: c.createProxyProtoPolicy(),
@@ -201,7 +194,7 @@ func (c *Connector) createListener(ctx context.Context, listenAddress string) (n
201194

202195
func (c *Connector) createProxyProtoPolicy() func(upstream net.Addr) (proxyproto.Policy, error) {
203196
return func(upstream net.Addr) (proxyproto.Policy, error) {
204-
trustedIpNets := c.trustedProxyNets
197+
trustedIpNets := c.config.TrustedProxyNets
205198

206199
if len(trustedIpNets) == 0 {
207200
logrus.Debug("No trusted proxy networks configured, using the PROXY header by default")
@@ -497,7 +490,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
497490
case mcproto.StateLogin:
498491
backendTry = retry.NewConstant(backendRetryInterval)
499492
// Connect request: if autoscaler is enabled, try to connect until backendTimeout is reached
500-
if waker != nil {
493+
if c.config.AutoScaleUp {
501494
// Autoscaler enabled: retry until backendTimeout is reached
502495
backendTry = retry.WithMaxDuration(backendTimeout, backendTry)
503496
} else {
@@ -539,11 +532,11 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
539532
}
540533
}
541534

542-
if nextState == mcproto.StateStatus && c.fakeOnline && waker != nil {
535+
if nextState == mcproto.StateStatus && c.config.FakeOnline && c.config.AutoScaleUp {
543536
logrus.Info("Server is offline, sending fakeOnlineMOTD for status request")
544537
writeStatusErr := mcproto.WriteStatusResponse(
545538
frontendConn,
546-
c.fakeOnlineMOTD,
539+
c.config.FakeOnlineMOTD,
547540
)
548541

549542
if writeStatusErr != nil {
@@ -600,7 +593,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
600593
cleanupMetrics = true
601594

602595
// PROXY protocol implementation
603-
if c.sendProxyProto {
596+
if c.config.SendProxyProto {
604597

605598
// Determine transport protocol for the PROXY header by "analyzing" the frontend connection's address
606599
transportProtocol := proxyproto.TCPv4

0 commit comments

Comments
 (0)