Skip to content

Commit

Permalink
Replace mailgun/manners with go 1.8 graceful shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Emile Vauge <emile@vauge.com>
  • Loading branch information
emilevauge committed Mar 8, 2017
1 parent 0d60aaf commit b344f93
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -88,7 +88,6 @@ You can access to a simple HTML frontend of Træfik.
- [Oxy](https://github.com/vulcand/oxy): an awesome proxy library made by Mailgun guys
- [Gorilla mux](https://github.com/gorilla/mux): famous request router
- [Negroni](https://github.com/codegangsta/negroni): web middlewares made simple
- [Manners](https://github.com/mailgun/manners): graceful shutdown of http.Handler servers
- [Lego](https://github.com/xenolf/lego): the best [Let's Encrypt](https://letsencrypt.org) library in go

## Test it
Expand Down
6 changes: 2 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion glide.yaml
Expand Up @@ -47,7 +47,6 @@ import:
- package: github.com/hashicorp/consul
subpackages:
- api
- package: github.com/mailgun/manners
- package: github.com/streamrail/concurrent-map
- package: github.com/stretchr/testify
subpackages:
Expand Down
46 changes: 17 additions & 29 deletions server.go
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/containous/traefik/provider"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
"github.com/mailgun/manners"
"github.com/streamrail/concurrent-map"
"github.com/vulcand/oxy/cbreaker"
"github.com/vulcand/oxy/connlimit"
Expand Down Expand Up @@ -58,7 +57,7 @@ type Server struct {
type serverEntryPoints map[string]*serverEntryPoint

type serverEntryPoint struct {
httpServer *manners.GracefulServer
httpServer *http.Server
httpRouter *middlewares.HandlerSwitcher
}

Expand Down Expand Up @@ -114,14 +113,15 @@ func (server *Server) Wait() {

// Stop stops the server
func (server *Server) Stop() {
defer log.Info("Server stopped")
for serverEntryPointName, serverEntryPoint := range server.serverEntryPoints {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(server.globalConfiguration.GraceTimeOut)*time.Second)
go func() {
log.Debugf("Waiting %d seconds before killing connections on entrypoint %s...", 30, serverEntryPointName)
serverEntryPoint.httpServer.BlockingClose()
cancel()
}()
<-ctx.Done()
log.Debugf("Waiting %d seconds before killing connections on entrypoint %s...", server.globalConfiguration.GraceTimeOut, serverEntryPointName)
if err := serverEntryPoint.httpServer.Shutdown(ctx); err != nil {
log.Debugf("Wait is over due to: %s", err)
serverEntryPoint.httpServer.Close()
}
cancel()
}
server.stopChan <- true
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (server *Server) startHTTPServers() {
if server.globalConfiguration.EntryPoints[newServerEntryPointName].Compress {
serverMiddlewares = append(serverMiddlewares, &middlewares.Compress{})
}
newsrv, err := server.prepareServer(newServerEntryPointName, newServerEntryPoint.httpRouter, server.globalConfiguration.EntryPoints[newServerEntryPointName], nil, serverMiddlewares...)
newsrv, err := server.prepareServer(newServerEntryPointName, newServerEntryPoint.httpRouter, server.globalConfiguration.EntryPoints[newServerEntryPointName], serverMiddlewares...)
if err != nil {
log.Fatal("Error preparing server: ", err)
}
Expand Down Expand Up @@ -490,21 +490,22 @@ func (server *Server) createTLSConfig(entryPointName string, tlsOption *TLS, rou
return config, nil
}

func (server *Server) startServer(srv *manners.GracefulServer, globalConfiguration GlobalConfiguration) {
func (server *Server) startServer(srv *http.Server, globalConfiguration GlobalConfiguration) {
log.Infof("Starting server on %s", srv.Addr)
if srv.TLSConfig != nil {
if err := srv.ListenAndServeTLSWithConfig(srv.TLSConfig); err != nil {
err := srv.ListenAndServeTLS("", "")
if err != nil {
log.Fatal("Error creating server: ", err)
}
} else {
if err := srv.ListenAndServe(); err != nil {
err := srv.ListenAndServe()
if err != nil {
log.Fatal("Error creating server: ", err)
}
}
log.Info("Server stopped")
}

func (server *Server) prepareServer(entryPointName string, router *middlewares.HandlerSwitcher, entryPoint *EntryPoint, oldServer *manners.GracefulServer, middlewares ...negroni.Handler) (*manners.GracefulServer, error) {
func (server *Server) prepareServer(entryPointName string, router *middlewares.HandlerSwitcher, entryPoint *EntryPoint, middlewares ...negroni.Handler) (*http.Server, error) {
log.Infof("Preparing server %s %+v", entryPointName, entryPoint)
// middlewares
var negroni = negroni.New()
Expand All @@ -518,24 +519,11 @@ func (server *Server) prepareServer(entryPointName string, router *middlewares.H
return nil, err
}

if oldServer == nil {
return manners.NewWithServer(
&http.Server{
Addr: entryPoint.Address,
Handler: negroni,
TLSConfig: tlsConfig,
}), nil
}
gracefulServer, err := oldServer.HijackListener(&http.Server{
return &http.Server{
Addr: entryPoint.Address,
Handler: negroni,
TLSConfig: tlsConfig,
}, tlsConfig)
if err != nil {
log.Errorf("Error hijacking server: %s", err)
return nil, err
}
return gracefulServer, nil
}, nil
}

func (server *Server) buildEntryPoints(globalConfiguration GlobalConfiguration) map[string]*serverEntryPoint {
Expand Down

0 comments on commit b344f93

Please sign in to comment.