Merge pull request #126 from emilevauge/lock-prepareserver

Add mutex around prepareserver
This commit is contained in:
Vincent Demeester 2015-11-24 10:20:44 +01:00
commit 1604786285

View file

@ -26,6 +26,7 @@ import (
"github.com/mailgun/oxy/roundrobin" "github.com/mailgun/oxy/roundrobin"
"github.com/thoas/stats" "github.com/thoas/stats"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
"sync"
) )
var ( var (
@ -53,6 +54,7 @@ func main() {
defer close(stopChan) defer close(stopChan)
var providers = []provider.Provider{} var providers = []provider.Provider{}
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var serverLock sync.Mutex
// load global configuration // load global configuration
globalConfiguration := LoadFileConfig(*globalConfigFile) globalConfiguration := LoadFileConfig(*globalConfigFile)
@ -124,6 +126,7 @@ func main() {
newConfigurationRouter, err := LoadConfig(newConfigurations, globalConfiguration) newConfigurationRouter, err := LoadConfig(newConfigurations, globalConfiguration)
if err == nil { if err == nil {
serverLock.Lock()
currentConfigurations = newConfigurations currentConfigurations = newConfigurations
configurationRouter = newConfigurationRouter configurationRouter = newConfigurationRouter
oldServer := srv oldServer := srv
@ -138,6 +141,7 @@ func main() {
log.Info("Stopping old server") log.Info("Stopping old server")
oldServer.Close() oldServer.Close()
} }
serverLock.Unlock()
} else { } else {
log.Error("Error loading new configuration, aborted ", err) log.Error("Error loading new configuration, aborted ", err)
} }
@ -199,11 +203,13 @@ func main() {
//negroni.Use(middlewares.NewRoutes(configurationRouter)) //negroni.Use(middlewares.NewRoutes(configurationRouter))
var er error var er error
serverLock.Lock()
srv, er = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics) srv, er = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics)
if er != nil { if er != nil {
log.Fatal("Error preparing server: ", er) log.Fatal("Error preparing server: ", er)
} }
go startServer(srv, globalConfiguration) go startServer(srv, globalConfiguration)
serverLock.Unlock()
<-stopChan <-stopChan
log.Info("Shutting down") log.Info("Shutting down")