Skip to content

Commit 1254632

Browse files
committed
improve the new Wait method
1 parent 7088291 commit 1254632

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

HISTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232
ctx.Writef("Hello, %s!", "World")
3333
})
3434

35-
app.Listen(":8080", iris.NonBlocking())
35+
app.Listen(":8080", iris.NonBlocking(), iris.WithoutServerError(iris.ErrServerClosed))
3636

3737
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
3838
defer cancel()

configuration.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"runtime"
99
"strings"
10+
"sync"
1011
"time"
1112

1213
"github.com/kataras/iris/v12/context"
@@ -976,13 +977,25 @@ type Configuration struct {
976977
//
977978
// Defaults to empty map.
978979
Other map[string]interface{} `ini:"other" json:"other,omitempty" yaml:"Other" toml:"Other"`
980+
981+
mu sync.RWMutex // mutex for some of the configuration fields that may change during parallel jobs (see Application.NonBlocking & Wait).
979982
}
980983

981984
var _ context.ConfigurationReadOnly = (*Configuration)(nil)
982985

983-
// GetVHost returns the non-exported vhost config field.
986+
// GetVHost returns the non-exported VHost config field.
984987
func (c *Configuration) GetVHost() string {
985-
return c.VHost
988+
c.mu.RLock()
989+
vhost := c.VHost
990+
c.mu.RUnlock()
991+
return vhost
992+
}
993+
994+
// SetVHost sets the non-exported VHost config field.
995+
func (c *Configuration) SetVHost(s string) {
996+
c.mu.Lock()
997+
c.VHost = s
998+
c.mu.Unlock()
986999
}
9871000

9881001
// GetLogLevel returns the LogLevel field.

iris.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -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 {
629629
func (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.
817814
func 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.
11391136
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-
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

Comments
 (0)