Skip to content

Commit

Permalink
fix race-condition on application.cfg when reloaded very fast
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jan 14, 2016
1 parent 4775340 commit 8ca3d86
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
3 changes: 1 addition & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ func (a *Application) Reload(cfg *config.Config) error {
if err := a.checkConfigCouldBeReloaded(cfg); err != nil {
return err
}
a.cfg = cfg
return a.reinitFromConfig()
return a.reinitFromConfig(cfg)
}

// Wait subscribes iteself to few signals and waits for any of them to be received.
Expand Down
8 changes: 5 additions & 3 deletions app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"github.com/ironsmile/nedomi/utils"
)

func (a *Application) reinitFromConfig() (err error) {
func (a *Application) reinitFromConfig(cfg *config.Config) (err error) {
app := *a // copy
app.cfg = cfg

app.virtualHosts = make(map[string]*VirtualHost)
app.upstreams = make(map[string]types.Upstream)
Expand Down Expand Up @@ -70,6 +71,7 @@ func (a *Application) reinitFromConfig() (err error) {

a.Lock()
defer a.Unlock()
a.cfg = app.cfg
a.SetLogger(app.GetLogger())
a.virtualHosts = app.virtualHosts
a.upstreams = app.upstreams
Expand All @@ -92,7 +94,7 @@ func (a *Application) reinitFromConfig() (err error) {
return nil
}

// initFromConfig should be called when starting or reloading the app. It makes
// initFromConfig should be called when starting but not when reloading the app. It makes
// all the connections between cache zones, virtual hosts and upstreams.
func (a *Application) initFromConfig() (err error) {
// Make the vhost and cacheZone maps
Expand All @@ -101,7 +103,7 @@ func (a *Application) initFromConfig() (err error) {
a.ctx, a.ctxCancel = context.WithCancel(context.Background())
a.ctx = contexts.NewAppContext(a.ctx, a)
a.ctx = contexts.NewCacheZonesContext(a.ctx, a.cacheZones)
return a.reinitFromConfig()
return a.reinitFromConfig(a.cfg)
}

func (a *Application) initCacheZone(cfgCz *config.CacheZone) (err error) {
Expand Down
3 changes: 1 addition & 2 deletions app/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func TestReinit(t *testing.T) {
Algorithm: "lru",
}
replaceZone(&cfg, "zone2", cfg.CacheZones["zone3"])
app.cfg = &cfg
if err := app.reinitFromConfig(); err != nil {
if err := app.reinitFromConfig(&cfg); err != nil {
t.Fatalf("Error upon reiniting app: %s", err)
}

Expand Down

0 comments on commit 8ca3d86

Please sign in to comment.