@@ -552,17 +552,17 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
552
552
// bind the constructed server and return it
553
553
su := host .New (srv )
554
554
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,
556
556
// in order to correct decide what to do on:
557
557
// mydomain.com -> invalid
558
558
// localhost -> invalid
559
559
// sub.mydomain.com -> valid
560
560
// sub.localhost -> valid
561
561
// 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 ) )
563
563
} else {
564
564
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.
566
566
}
567
567
}
568
568
@@ -629,10 +629,7 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
629
629
func (app * Application ) Shutdown (ctx stdContext.Context ) error {
630
630
app .mu .Lock ()
631
631
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.
636
633
637
634
for i , su := range app .Hosts {
638
635
app .logger .Debugf ("Host[%d]: Shutdown now" , i )
@@ -816,7 +813,7 @@ type Runner func(*Application) error
816
813
// See `Run` for more.
817
814
func Listener (l net.Listener , hostConfigs ... host.Configurator ) Runner {
818
815
return func (app * Application ) error {
819
- app .config .VHost = netutil .ResolveVHost (l .Addr ().String ())
816
+ app .config .SetVHost ( netutil .ResolveVHost (l .Addr ().String () ))
820
817
return app .NewHost (& http.Server {Addr : l .Addr ().String ()}).
821
818
Configure (hostConfigs ... ).
822
819
Serve (l )
@@ -1137,8 +1134,6 @@ func getMaxRetries(retryInterval time.Duration, base float64) int {
1137
1134
1138
1135
// tryConnect tries to connect to the server with the given context and retry parameters.
1139
1136
func (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
-
1142
1137
// Try to connect to the server in a loop.
1143
1138
for i := 0 ; i < maxRetries ; i ++ {
1144
1139
// Check the context before each attempt.
@@ -1147,6 +1142,13 @@ func (app *Application) tryConnect(ctx stdContext.Context, maxRetries int, retry
1147
1142
// Context is canceled, return the context error.
1148
1143
return ctx .Err ()
1149
1144
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
+
1150
1152
// Context is not canceled, proceed with the attempt.
1151
1153
conn , err := net .Dial ("tcp" , address )
1152
1154
if err == nil {
@@ -1198,7 +1200,7 @@ func (app *Application) tryStartTunneling() {
1198
1200
1199
1201
publicAddr := publicAddrs [0 ]
1200
1202
// 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 :])
1202
1204
1203
1205
directLog := []byte (fmt .Sprintf ("• Public Address: %s\n " , publicAddr ))
1204
1206
app .logger .Printer .Write (directLog ) // nolint:errcheck
0 commit comments