@@ -552,17 +552,17 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
552552 // bind the constructed server and return it
553553 su := host .New (srv )
554554
555- if app .config .VHost == "" { // vhost now is useful for router subdomain on wildcard subdomains,
555+ if app .config .GetVHost () == "" { // vhost now is useful for router subdomain on wildcard subdomains,
556556 // in order to correct decide what to do on:
557557 // mydomain.com -> invalid
558558 // localhost -> invalid
559559 // sub.mydomain.com -> valid
560560 // sub.localhost -> valid
561561 // we need the host (without port if 80 or 443) in order to validate these, so:
562- app .config .VHost = netutil .ResolveVHost (srv .Addr )
562+ app .config .SetVHost ( netutil .ResolveVHost (srv .Addr ) )
563563 } else {
564564 context .GetDomain = func (_ string ) string { // #1886
565- return app .config .VHost
565+ return app .config .VHost // GetVHost: here we don't need mutex protection as it's request-time and all modifications are already made.
566566 }
567567 }
568568
@@ -629,10 +629,7 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
629629func (app * Application ) Shutdown (ctx stdContext.Context ) error {
630630 app .mu .Lock ()
631631 defer app .mu .Unlock ()
632-
633- defer func () {
634- app .setRunError (ErrServerClosed ) // make sure to set the error so any .Wait calls return.
635- }()
632+ defer app .setRunError (ErrServerClosed ) // make sure to set the error so any .Wait calls return.
636633
637634 for i , su := range app .Hosts {
638635 app .logger .Debugf ("Host[%d]: Shutdown now" , i )
@@ -816,7 +813,7 @@ type Runner func(*Application) error
816813// See `Run` for more.
817814func Listener (l net.Listener , hostConfigs ... host.Configurator ) Runner {
818815 return func (app * Application ) error {
819- app .config .VHost = netutil .ResolveVHost (l .Addr ().String ())
816+ app .config .SetVHost ( netutil .ResolveVHost (l .Addr ().String () ))
820817 return app .NewHost (& http.Server {Addr : l .Addr ().String ()}).
821818 Configure (hostConfigs ... ).
822819 Serve (l )
@@ -1137,8 +1134,6 @@ func getMaxRetries(retryInterval time.Duration, base float64) int {
11371134
11381135// tryConnect tries to connect to the server with the given context and retry parameters.
11391136func (app * Application ) tryConnect (ctx stdContext.Context , maxRetries int , retryInterval time.Duration , base float64 ) error {
1140- address := app .config .GetVHost () // Get this server's listening address.
1141-
11421137 // Try to connect to the server in a loop.
11431138 for i := 0 ; i < maxRetries ; i ++ {
11441139 // Check the context before each attempt.
@@ -1147,6 +1142,13 @@ func (app *Application) tryConnect(ctx stdContext.Context, maxRetries int, retry
11471142 // Context is canceled, return the context error.
11481143 return ctx .Err ()
11491144 default :
1145+ address := app .config .GetVHost () // Get this server's listening address.
1146+ if address == "" {
1147+ i -- // Note that this may be modified at another go routine of the serve method. So it may be empty at first chance. So retry fetching the VHost every 1 second.
1148+ time .Sleep (time .Second )
1149+ continue
1150+ }
1151+
11501152 // Context is not canceled, proceed with the attempt.
11511153 conn , err := net .Dial ("tcp" , address )
11521154 if err == nil {
@@ -1198,7 +1200,7 @@ func (app *Application) tryStartTunneling() {
11981200
11991201 publicAddr := publicAddrs [0 ]
12001202 // to make subdomains resolution still based on this new remote, public addresses.
1201- app .config .VHost = publicAddr [strings .Index (publicAddr , "://" )+ 3 :]
1203+ app .config .SetVHost ( publicAddr [strings .Index (publicAddr , "://" )+ 3 :])
12021204
12031205 directLog := []byte (fmt .Sprintf ("• Public Address: %s\n " , publicAddr ))
12041206 app .logger .Printer .Write (directLog ) // nolint:errcheck
0 commit comments