Merge pull request #24 from EmileVauge/zero-downtime-reload
Zero downtime reload
This commit is contained in:
commit
4fb25ca358
3 changed files with 78 additions and 58 deletions
14
Godeps/Godeps.json
generated
14
Godeps/Godeps.json
generated
|
@ -69,6 +69,11 @@
|
||||||
"ImportPath": "github.com/mailgun/log",
|
"ImportPath": "github.com/mailgun/log",
|
||||||
"Rev": "44874009257d4d47ba9806f1b7f72a32a015e4d8"
|
"Rev": "44874009257d4d47ba9806f1b7f72a32a015e4d8"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/mailgun/manners",
|
||||||
|
"Comment": "0.3.1-30-g37136f7",
|
||||||
|
"Rev": "37136f736785d7c6aa3b9a27b4b2dd1028ca6d79"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/mailgun/oxy/cbreaker",
|
"ImportPath": "github.com/mailgun/oxy/cbreaker",
|
||||||
"Rev": "547c334d658398c05b346c0b79d8f47ba2e1473b"
|
"Rev": "547c334d658398c05b346c0b79d8f47ba2e1473b"
|
||||||
|
@ -101,19 +106,10 @@
|
||||||
"ImportPath": "github.com/thoas/stats",
|
"ImportPath": "github.com/thoas/stats",
|
||||||
"Rev": "54ed61c2b47e263ae2f01b86837b0c4bd1da28e8"
|
"Rev": "54ed61c2b47e263ae2f01b86837b0c4bd1da28e8"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"ImportPath": "github.com/tylerb/graceful",
|
|
||||||
"Comment": "v1.2.1",
|
|
||||||
"Rev": "ac9ebe4f1ee151ac1eeeaef32957085cba64d508"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/unrolled/render",
|
"ImportPath": "github.com/unrolled/render",
|
||||||
"Rev": "26b4e3aac686940fe29521545afad9966ddfc80c"
|
"Rev": "26b4e3aac686940fe29521545afad9966ddfc80c"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"ImportPath": "golang.org/x/net/netutil",
|
|
||||||
"Rev": "d9558e5c97f85372afee28cf2b6059d7d3818919"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"ImportPath": "gopkg.in/alecthomas/kingpin.v2",
|
"ImportPath": "gopkg.in/alecthomas/kingpin.v2",
|
||||||
"Comment": "v2.0.12",
|
"Comment": "v2.0.12",
|
||||||
|
|
|
@ -38,7 +38,7 @@ Here is a demo of Træfɪk using Docker backend, showing a load-balancing betwee
|
||||||
* [Oxy](https://github.com/mailgun/oxy/): an awsome proxy library made by Mailgun guys
|
* [Oxy](https://github.com/mailgun/oxy/): an awsome proxy library made by Mailgun guys
|
||||||
* [Gorilla mux](https://github.com/gorilla/mux): famous request router
|
* [Gorilla mux](https://github.com/gorilla/mux): famous request router
|
||||||
* [Negroni](https://github.com/codegangsta/negroni): web middlewares made simple
|
* [Negroni](https://github.com/codegangsta/negroni): web middlewares made simple
|
||||||
* [Graceful](https://github.com/tylerb/graceful): graceful shutdown of http.Handler servers
|
* [Manners](https://github.com/mailgun/manners): graceful shutdown of http.Handler servers
|
||||||
|
|
||||||
# Quick start
|
# Quick start
|
||||||
|
|
||||||
|
|
88
traefik.go
88
traefik.go
|
@ -17,13 +17,14 @@ import (
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/emilevauge/traefik/middlewares"
|
"github.com/emilevauge/traefik/middlewares"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/mailgun/manners"
|
||||||
"github.com/mailgun/oxy/cbreaker"
|
"github.com/mailgun/oxy/cbreaker"
|
||||||
"github.com/mailgun/oxy/forward"
|
"github.com/mailgun/oxy/forward"
|
||||||
"github.com/mailgun/oxy/roundrobin"
|
"github.com/mailgun/oxy/roundrobin"
|
||||||
"github.com/thoas/stats"
|
"github.com/thoas/stats"
|
||||||
"github.com/tylerb/graceful"
|
|
||||||
"github.com/unrolled/render"
|
"github.com/unrolled/render"
|
||||||
"gopkg.in/alecthomas/kingpin.v2"
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -39,14 +40,18 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
kingpin.Parse()
|
kingpin.Parse()
|
||||||
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
|
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
|
||||||
var srv *graceful.Server
|
var srv *manners.GracefulServer
|
||||||
var configurationRouter *mux.Router
|
var configurationRouter *mux.Router
|
||||||
var configurationChan = make(chan *Configuration, 10)
|
var configurationChan = make(chan *Configuration, 10)
|
||||||
defer close(configurationChan)
|
defer close(configurationChan)
|
||||||
var providers = []Provider{}
|
|
||||||
var sigs = make(chan os.Signal, 1)
|
var sigs = make(chan os.Signal, 1)
|
||||||
|
defer close(sigs)
|
||||||
|
var stopChan = make(chan bool)
|
||||||
|
defer close(stopChan)
|
||||||
|
var providers = []Provider{}
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
// load global configuration
|
// load global configuration
|
||||||
|
@ -91,8 +96,15 @@ func main() {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
currentConfiguration = configuration
|
currentConfiguration = configuration
|
||||||
configurationRouter = newConfigurationRouter
|
configurationRouter = newConfigurationRouter
|
||||||
srv.Stop(time.Duration(globalConfiguration.GraceTimeOut) * time.Second)
|
oldServer := srv
|
||||||
time.Sleep(3 * time.Second)
|
newsrv := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
|
||||||
|
go startServer(newsrv, globalConfiguration)
|
||||||
|
srv = newsrv
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
if oldServer != nil {
|
||||||
|
log.Info("Stopping old server")
|
||||||
|
oldServer.Close()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Error("Error loading new configuration, aborted ", err)
|
log.Error("Error loading new configuration, aborted ", err)
|
||||||
}
|
}
|
||||||
|
@ -130,38 +142,25 @@ func main() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
goAway := false
|
|
||||||
go func() {
|
go func() {
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
log.Infof("I have to go... %+v", sig)
|
log.Infof("I have to go... %+v", sig)
|
||||||
goAway = true
|
log.Info("Stopping server")
|
||||||
srv.Stop(time.Duration(globalConfiguration.GraceTimeOut) * time.Second)
|
srv.Close()
|
||||||
|
stopChan <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
|
||||||
if goAway {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// middlewares
|
|
||||||
var negroni = negroni.New()
|
|
||||||
negroni.Use(metrics)
|
|
||||||
negroni.Use(loggerMiddleware)
|
|
||||||
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
||||||
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
||||||
negroni.UseHandler(configurationRouter)
|
srv = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics)
|
||||||
|
go startServer(srv, globalConfiguration)
|
||||||
|
|
||||||
srv = &graceful.Server{
|
<-stopChan
|
||||||
Timeout: time.Duration(globalConfiguration.GraceTimeOut) * time.Second,
|
log.Info("Shutting down")
|
||||||
NoSignalHandling: true,
|
}
|
||||||
|
|
||||||
Server: &http.Server{
|
func startServer(srv *manners.GracefulServer, globalConfiguration *GlobalConfiguration) {
|
||||||
Addr: globalConfiguration.Port,
|
log.Info("Starting server")
|
||||||
Handler: negroni,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
|
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
|
||||||
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -179,10 +178,35 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
log.Info("Server stopped")
|
||||||
log.Info("Started")
|
}
|
||||||
<-srv.StopChan()
|
|
||||||
log.Info("Stopped")
|
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, oldServer *manners.GracefulServer, middlewares ...negroni.Handler) *manners.GracefulServer {
|
||||||
|
log.Info("Preparing server")
|
||||||
|
// middlewares
|
||||||
|
var negroni = negroni.New()
|
||||||
|
for _, middleware := range middlewares {
|
||||||
|
negroni.Use(middleware)
|
||||||
|
}
|
||||||
|
negroni.UseHandler(router)
|
||||||
|
|
||||||
|
if oldServer == nil {
|
||||||
|
return manners.NewWithServer(
|
||||||
|
&http.Server{
|
||||||
|
Addr: globalConfiguration.Port,
|
||||||
|
Handler: negroni,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
server, err := oldServer.HijackListener(&http.Server{
|
||||||
|
Addr: globalConfiguration.Port,
|
||||||
|
Handler: negroni,
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error hijacking server %s", err)
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return server
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue