@@ -99,7 +99,7 @@ type Connector struct {
9999 autoScaleUpAllowDenyConfig * AllowDenyConfig
100100
101101 fakeOnline bool
102- fakeOnlineMOTD string
102+ fakeOnlineMOTD string
103103
104104 connectionNotifier ConnectionNotifier
105105}
@@ -357,157 +357,157 @@ func (c *Connector) readPlayerInfo(bufferedReader *bufio.Reader, clientAddr net.
357357}
358358
359359func (c * Connector ) findAndConnectBackend (ctx context.Context , frontendConn net.Conn ,
360- clientAddr net.Addr , preReadContent io.Reader , serverAddress string , playerInfo * PlayerInfo , nextState mcproto.State ) {
361-
362- backendHostPort , resolvedHost , waker := Routes .FindBackendForServerAddress (ctx , serverAddress )
363- if waker != nil && nextState > mcproto .StateStatus {
364- serverAllowsPlayer := c .autoScaleUpAllowDenyConfig .ServerAllowsPlayer (serverAddress , playerInfo )
365- logrus .
366- WithField ("client" , clientAddr ).
367- WithField ("server" , serverAddress ).
368- WithField ("player" , playerInfo ).
369- WithField ("serverAllowsPlayer" , serverAllowsPlayer ).
370- Debug ("checked if player is allowed to wake up the server" )
371- if serverAllowsPlayer {
372- if err := waker (ctx ); err != nil {
373- logrus .WithFields (logrus.Fields {"serverAddress" : serverAddress }).WithError (err ).Error ("failed to wake up backend" )
374- c .metrics .Errors .With ("type" , "wakeup_failed" ).Add (1 )
375- return
376- }
377- }
378- }
379-
380- if backendHostPort == "" {
381- logrus .
382- WithField ("serverAddress" , serverAddress ).
383- WithField ("resolvedHost" , resolvedHost ).
384- WithField ("player" , playerInfo ).
385- Warn ("Unable to find registered backend" )
386- c .metrics .Errors .With ("type" , "missing_backend" ).Add (1 )
387-
388- if c .connectionNotifier != nil {
389- err := c .connectionNotifier .NotifyMissingBackend (ctx , clientAddr , serverAddress , playerInfo )
390- if err != nil {
391- logrus .WithError (err ).Warn ("failed to notify missing backend" )
392- }
393- }
394-
395- return
396- }
397-
398- logrus .
399- WithField ("client" , clientAddr ).
400- WithField ("server" , serverAddress ).
401- WithField ("backendHostPort" , backendHostPort ).
402- WithField ("player" , playerInfo ).
403- Info ("Connecting to backend" )
404-
405- var backendConn net.Conn
406- var err error
407-
408- if nextState == mcproto .StateStatus {
409- // Status request: try to connect once with backendStatusTimeout
410- backendConn , err = net .DialTimeout ("tcp" , backendHostPort , backendStatusTimeout )
411- if err != nil {
412- logrus .
413- WithError (err ).
414- WithField ("client" , clientAddr ).
415- WithField ("serverAddress" , serverAddress ).
416- WithField ("backend" , backendHostPort ).
417- WithField ("player" , playerInfo ).
418- Warn ("Unable to connect to backend for status request" )
419- c .metrics .Errors .With ("type" , "backend_failed" ).Add (1 )
420-
421- if c .connectionNotifier != nil {
422- notifyErr := c .connectionNotifier .NotifyFailedBackendConnection (ctx , clientAddr , serverAddress , playerInfo , backendHostPort , err )
423- if notifyErr != nil {
424- logrus .WithError (notifyErr ).Warn ("failed to notify failed backend connection" )
425- }
426- }
427-
428- // Return fakeOnline MOTD
429- if c .fakeOnline && waker != nil {
430- logrus .Info ("Server is offline, sending fakeOnlineMOTD for status request" )
431- writeStatusErr := mcproto .WriteStatusResponse (
432- frontendConn ,
433- c .fakeOnlineMOTD ,
434- )
435- if writeStatusErr != nil {
436- logrus .
437- WithError (writeStatusErr ).
438- WithField ("client" , clientAddr ).
439- WithField ("serverAddress" , serverAddress ).
440- WithField ("backend" , backendHostPort ).
441- WithField ("player" , playerInfo ).
442- Error ("Failed to write status response" )
443- }
444- }
445- return
446- }
447- } else if nextState == mcproto .StateLogin {
448- // Connect request
449- if waker != nil {
360+ clientAddr net.Addr , preReadContent io.Reader , serverAddress string , playerInfo * PlayerInfo , nextState mcproto.State ) {
361+
362+ backendHostPort , resolvedHost , waker := Routes .FindBackendForServerAddress (ctx , serverAddress )
363+ if waker != nil && nextState > mcproto .StateStatus {
364+ serverAllowsPlayer := c .autoScaleUpAllowDenyConfig .ServerAllowsPlayer (serverAddress , playerInfo )
365+ logrus .
366+ WithField ("client" , clientAddr ).
367+ WithField ("server" , serverAddress ).
368+ WithField ("player" , playerInfo ).
369+ WithField ("serverAllowsPlayer" , serverAllowsPlayer ).
370+ Debug ("checked if player is allowed to wake up the server" )
371+ if serverAllowsPlayer {
372+ if err := waker (ctx ); err != nil {
373+ logrus .WithFields (logrus.Fields {"serverAddress" : serverAddress }).WithError (err ).Error ("failed to wake up backend" )
374+ c .metrics .Errors .With ("type" , "wakeup_failed" ).Add (1 )
375+ return
376+ }
377+ }
378+ }
379+
380+ if backendHostPort == "" {
381+ logrus .
382+ WithField ("serverAddress" , serverAddress ).
383+ WithField ("resolvedHost" , resolvedHost ).
384+ WithField ("player" , playerInfo ).
385+ Warn ("Unable to find registered backend" )
386+ c .metrics .Errors .With ("type" , "missing_backend" ).Add (1 )
387+
388+ if c .connectionNotifier != nil {
389+ err := c .connectionNotifier .NotifyMissingBackend (ctx , clientAddr , serverAddress , playerInfo )
390+ if err != nil {
391+ logrus .WithError (err ).Warn ("failed to notify missing backend" )
392+ }
393+ }
394+
395+ return
396+ }
397+
398+ logrus .
399+ WithField ("client" , clientAddr ).
400+ WithField ("server" , serverAddress ).
401+ WithField ("backendHostPort" , backendHostPort ).
402+ WithField ("player" , playerInfo ).
403+ Info ("Connecting to backend" )
404+
405+ var backendConn net.Conn
406+ var err error
407+
408+ if nextState == mcproto .StateStatus {
409+ // Status request: try to connect once with backendStatusTimeout
410+ backendConn , err = net .DialTimeout ("tcp" , backendHostPort , backendStatusTimeout )
411+ if err != nil {
412+ logrus .
413+ WithError (err ).
414+ WithField ("client" , clientAddr ).
415+ WithField ("serverAddress" , serverAddress ).
416+ WithField ("backend" , backendHostPort ).
417+ WithField ("player" , playerInfo ).
418+ Warn ("Unable to connect to backend for status request" )
419+ c .metrics .Errors .With ("type" , "backend_failed" ).Add (1 )
420+
421+ if c .connectionNotifier != nil {
422+ notifyErr := c .connectionNotifier .NotifyFailedBackendConnection (ctx , clientAddr , serverAddress , playerInfo , backendHostPort , err )
423+ if notifyErr != nil {
424+ logrus .WithError (notifyErr ).Warn ("failed to notify failed backend connection" )
425+ }
426+ }
427+
428+ // Return fakeOnline MOTD
429+ if c .fakeOnline && waker != nil {
430+ logrus .Info ("Server is offline, sending fakeOnlineMOTD for status request" )
431+ writeStatusErr := mcproto .WriteStatusResponse (
432+ frontendConn ,
433+ c .fakeOnlineMOTD ,
434+ )
435+ if writeStatusErr != nil {
436+ logrus .
437+ WithError (writeStatusErr ).
438+ WithField ("client" , clientAddr ).
439+ WithField ("serverAddress" , serverAddress ).
440+ WithField ("backend" , backendHostPort ).
441+ WithField ("player" , playerInfo ).
442+ Error ("Failed to write status response" )
443+ }
444+ }
445+ return
446+ }
447+ } else if nextState == mcproto .StateLogin {
448+ // Connect request
449+ if waker != nil {
450450 logrus .Debug ("Connect: Autoscaler is enabled, waiting for backend to be ready" )
451- // Autoscaler enabled: retry until backendTimeout is reached
452- deadline := time .Now ().Add (backendTimeout )
453- for {
454- backendConn , err = net .DialTimeout ("tcp" , backendHostPort , backendRetryInterval )
451+ // Autoscaler enabled: retry until backendTimeout is reached
452+ deadline := time .Now ().Add (backendTimeout )
453+ for {
454+ backendConn , err = net .DialTimeout ("tcp" , backendHostPort , backendRetryInterval )
455455 logrus .Debug ("Tries to connect to backend" )
456456
457- if err == nil {
458- break
459- }
460- if time .Now ().After (deadline ) {
461- logrus .
462- WithError (err ).
463- WithField ("client" , clientAddr ).
464- WithField ("serverAddress" , serverAddress ).
465- WithField ("backend" , backendHostPort ).
466- WithField ("player" , playerInfo ).
467- Warn ("Unable to connect to backend after retries (autoscaler enabled)" )
468- c .metrics .Errors .With ("type" , "backend_failed" ).Add (1 )
469- if c .connectionNotifier != nil {
470- notifyErr := c .connectionNotifier .NotifyFailedBackendConnection (ctx , clientAddr , serverAddress , playerInfo , backendHostPort , err )
471- if notifyErr != nil {
472- logrus .WithError (notifyErr ).Warn ("failed to notify failed backend connection" )
473- }
474- }
475- return
476- }
477- time .Sleep (backendRetryInterval )
478- }
479- } else {
457+ if err == nil {
458+ break
459+ }
460+ if time .Now ().After (deadline ) {
461+ logrus .
462+ WithError (err ).
463+ WithField ("client" , clientAddr ).
464+ WithField ("serverAddress" , serverAddress ).
465+ WithField ("backend" , backendHostPort ).
466+ WithField ("player" , playerInfo ).
467+ Warn ("Unable to connect to backend after retries (autoscaler enabled)" )
468+ c .metrics .Errors .With ("type" , "backend_failed" ).Add (1 )
469+ if c .connectionNotifier != nil {
470+ notifyErr := c .connectionNotifier .NotifyFailedBackendConnection (ctx , clientAddr , serverAddress , playerInfo , backendHostPort , err )
471+ if notifyErr != nil {
472+ logrus .WithError (notifyErr ).Warn ("failed to notify failed backend connection" )
473+ }
474+ }
475+ return
476+ }
477+ time .Sleep (backendRetryInterval )
478+ }
479+ } else {
480480 logrus .Debug ("Connect: Autoscaler is disabled, trying to connect once" )
481- // Autoscaler disabled: try to connect once with backendTimeout
482- backendConn , err = net .DialTimeout ("tcp" , backendHostPort , backendTimeout )
483- if err != nil {
484- logrus .
485- WithError (err ).
486- WithField ("client" , clientAddr ).
487- WithField ("serverAddress" , serverAddress ).
488- WithField ("backend" , backendHostPort ).
489- WithField ("player" , playerInfo ).
490- Warn ("Unable to connect to backend (autoscaler disabled)" )
491- c .metrics .Errors .With ("type" , "backend_failed" ).Add (1 )
492- if c .connectionNotifier != nil {
493- notifyErr := c .connectionNotifier .NotifyFailedBackendConnection (ctx , clientAddr , serverAddress , playerInfo , backendHostPort , err )
494- if notifyErr != nil {
495- logrus .WithError (notifyErr ).Warn ("failed to notify failed backend connection" )
496- }
497- }
498- return
499- }
500- }
501- } else {
502- // Unknown state, do nothing
481+ // Autoscaler disabled: try to connect once with backendTimeout
482+ backendConn , err = net .DialTimeout ("tcp" , backendHostPort , backendTimeout )
483+ if err != nil {
484+ logrus .
485+ WithError (err ).
486+ WithField ("client" , clientAddr ).
487+ WithField ("serverAddress" , serverAddress ).
488+ WithField ("backend" , backendHostPort ).
489+ WithField ("player" , playerInfo ).
490+ Warn ("Unable to connect to backend (autoscaler disabled)" )
491+ c .metrics .Errors .With ("type" , "backend_failed" ).Add (1 )
492+ if c .connectionNotifier != nil {
493+ notifyErr := c .connectionNotifier .NotifyFailedBackendConnection (ctx , clientAddr , serverAddress , playerInfo , backendHostPort , err )
494+ if notifyErr != nil {
495+ logrus .WithError (notifyErr ).Warn ("failed to notify failed backend connection" )
496+ }
497+ }
498+ return
499+ }
500+ }
501+ } else {
502+ // Unknown state, do nothing
503503 logrus .
504504 WithField ("client" , clientAddr ).
505505 WithField ("serverAddress" , serverAddress ).
506506 WithField ("nextState" , nextState ).
507507 WithField ("player" , playerInfo ).
508508 Warn ("Unknown state, unable to connect to backend" )
509- return
510- }
509+ return
510+ }
511511
512512 if c .connectionNotifier != nil {
513513 err := c .connectionNotifier .NotifyConnected (ctx , clientAddr , serverAddress , playerInfo , backendHostPort )
0 commit comments