graceful shutdown using manners
This commit is contained in:
parent
9edd4874ac
commit
6d06f1a862
3 changed files with 49 additions and 45 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",
|
||||||
|
|
|
@ -32,7 +32,7 @@ It supports several backends ([Docker :whale:](https://www.docker.com/), [Mesos/
|
||||||
* [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
|
||||||
|
|
||||||
|
|
78
traefik.go
78
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,15 +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)
|
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
|
||||||
|
@ -93,9 +97,14 @@ func main() {
|
||||||
currentConfiguration = configuration
|
currentConfiguration = configuration
|
||||||
configurationRouter = newConfigurationRouter
|
configurationRouter = newConfigurationRouter
|
||||||
oldServer := srv
|
oldServer := srv
|
||||||
srv = prepareServer(configurationRouter, globalConfiguration, loggerMiddleware, metrics)
|
newsrv := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
|
||||||
stopServer(oldServer, globalConfiguration)
|
go startServer(newsrv, globalConfiguration)
|
||||||
time.Sleep(3 * time.Second)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -133,33 +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")
|
||||||
stopServer(srv, globalConfiguration)
|
srv.Close()
|
||||||
|
stopChan <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
||||||
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
||||||
srv = prepareServer(configurationRouter, globalConfiguration, loggerMiddleware, metrics)
|
srv = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics)
|
||||||
|
go startServer(srv, globalConfiguration)
|
||||||
|
|
||||||
for {
|
<-stopChan
|
||||||
if goAway {
|
log.Info("Shutting down")
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
startServer(srv, globalConfiguration)
|
|
||||||
}()
|
|
||||||
log.Info("Started")
|
|
||||||
<-srv.StopChan()
|
|
||||||
log.Info("Stopped")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer(srv *graceful.Server, globalConfiguration *GlobalConfiguration){
|
func startServer(srv *manners.GracefulServer, globalConfiguration *GlobalConfiguration) {
|
||||||
|
log.Info("Starting server")
|
||||||
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 {
|
||||||
|
@ -177,13 +178,11 @@ func startServer(srv *graceful.Server, globalConfiguration *GlobalConfiguration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Info("Server stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopServer(srv *graceful.Server, globalConfiguration *GlobalConfiguration){
|
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, oldServer *manners.GracefulServer, middlewares ...negroni.Handler) *manners.GracefulServer {
|
||||||
srv.Stop(time.Duration(globalConfiguration.GraceTimeOut) * time.Second)
|
log.Info("Preparing server")
|
||||||
}
|
|
||||||
|
|
||||||
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, middlewares...negroni.Handler) (*graceful.Server){
|
|
||||||
// middlewares
|
// middlewares
|
||||||
var negroni = negroni.New()
|
var negroni = negroni.New()
|
||||||
for _, middleware := range middlewares {
|
for _, middleware := range middlewares {
|
||||||
|
@ -191,14 +190,23 @@ func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration,
|
||||||
}
|
}
|
||||||
negroni.UseHandler(router)
|
negroni.UseHandler(router)
|
||||||
|
|
||||||
return &graceful.Server{
|
if oldServer == nil {
|
||||||
Timeout: time.Duration(globalConfiguration.GraceTimeOut) * time.Second,
|
return manners.NewWithServer(
|
||||||
NoSignalHandling: true,
|
&http.Server{
|
||||||
|
Addr: globalConfiguration.Port,
|
||||||
Server: &http.Server{
|
Handler: negroni,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
server, err := oldServer.HijackListener(&http.Server{
|
||||||
Addr: globalConfiguration.Port,
|
Addr: globalConfiguration.Port,
|
||||||
Handler: negroni,
|
Handler: negroni,
|
||||||
},
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error hijacking server %s", err)
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return server
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +239,7 @@ func LoadConfig(configuration *Configuration, globalConfiguration *GlobalConfigu
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Reusing backend %s", frontend.Backend)
|
log.Debugf("Reusing backend %s", frontend.Backend)
|
||||||
}
|
}
|
||||||
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(globalConfiguration.Replay)), stream.Logger(oxyLogger))
|
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(globalConfiguration.Replay)), stream.Logger(oxyLogger))
|
||||||
var negroni = negroni.New()
|
var negroni = negroni.New()
|
||||||
negroni.Use(middlewares.NewCircuitBreaker(backends[frontend.Backend], cbreaker.Logger(oxyLogger)))
|
negroni.Use(middlewares.NewCircuitBreaker(backends[frontend.Backend], cbreaker.Logger(oxyLogger)))
|
||||||
newRoute.Handler(negroni)
|
newRoute.Handler(negroni)
|
||||||
|
|
Loading…
Reference in a new issue