From 52566376b2616f8648fbaab7ba78efa4c57939d0 Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 6 Feb 2017 09:39:40 +0100 Subject: [PATCH 1/2] tech(server): remove unused code --- pkg/cmd/grafana-server/server.go | 14 -------------- pkg/models/server.go | 4 ---- 2 files changed, 18 deletions(-) diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index 94eb8e845ea..af8f79e9eda 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -95,17 +95,3 @@ func (g *GrafanaServerImpl) Shutdown(code int, reason string) { log.Close() os.Exit(code) } - -// implement context.Context -func (g *GrafanaServerImpl) Deadline() (deadline time.Time, ok bool) { - return g.context.Deadline() -} -func (g *GrafanaServerImpl) Done() <-chan struct{} { - return g.context.Done() -} -func (g *GrafanaServerImpl) Err() error { - return g.context.Err() -} -func (g *GrafanaServerImpl) Value(key interface{}) interface{} { - return g.context.Value(key) -} diff --git a/pkg/models/server.go b/pkg/models/server.go index 876fc91dd01..4d683835256 100644 --- a/pkg/models/server.go +++ b/pkg/models/server.go @@ -1,10 +1,6 @@ package models -import "context" - type GrafanaServer interface { - context.Context - Start() Shutdown(code int, reason string) } From cf871b1284972166db94248d26e6254210ff53fc Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 6 Feb 2017 09:40:07 +0100 Subject: [PATCH 2/2] http: close http server gracefully. --- pkg/api/http_server.go | 21 +++++++++++++++++++-- pkg/cmd/grafana-server/server.go | 14 ++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index b1024e2da9f..c1a3b238041 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -25,6 +25,8 @@ type HttpServer struct { macaron *macaron.Macaron context context.Context streamManager *live.StreamManager + + httpSrv *http.Server } func NewHttpServer() *HttpServer { @@ -46,11 +48,20 @@ func (hs *HttpServer) Start(ctx context.Context) error { listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort) hs.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl) + hs.httpSrv = &http.Server{Addr: listenAddr, Handler: hs.macaron} switch setting.Protocol { case setting.HTTP: - err = http.ListenAndServe(listenAddr, hs.macaron) + err = hs.httpSrv.ListenAndServe() + if err == http.ErrServerClosed { + hs.log.Debug("server was shutdown gracefully") + return nil + } case setting.HTTPS: - err = hs.listenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile) + err = hs.httpSrv.ListenAndServeTLS(setting.CertFile, setting.KeyFile) + if err == http.ErrServerClosed { + hs.log.Debug("server was shutdown gracefully") + return nil + } default: hs.log.Error("Invalid protocol", "protocol", setting.Protocol) err = errors.New("Invalid Protocol") @@ -59,6 +70,12 @@ func (hs *HttpServer) Start(ctx context.Context) error { return err } +func (hs *HttpServer) Shutdown(ctx context.Context) error { + err := hs.httpSrv.Shutdown(ctx) + hs.log.Info("stopped http server") + return err +} + func (hs *HttpServer) listenAndServeTLS(listenAddr, certfile, keyfile string) error { if certfile == "" { return fmt.Errorf("cert_file cannot be empty when using HTTPS") diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index af8f79e9eda..30da2342c2b 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -3,7 +3,6 @@ package main import ( "context" "os" - "time" "golang.org/x/sync/errgroup" @@ -39,6 +38,8 @@ type GrafanaServerImpl struct { shutdownFn context.CancelFunc childRoutines *errgroup.Group log log.Logger + + httpServer *api.HttpServer } func (g *GrafanaServerImpl) Start() { @@ -74,9 +75,9 @@ func (g *GrafanaServerImpl) Start() { } func (g *GrafanaServerImpl) startHttpServer() { - httpServer := api.NewHttpServer() + g.httpServer = api.NewHttpServer() - err := httpServer.Start(g.context) + err := g.httpServer.Start(g.context) if err != nil { g.log.Error("Fail to start server", "error", err) @@ -88,8 +89,13 @@ func (g *GrafanaServerImpl) startHttpServer() { func (g *GrafanaServerImpl) Shutdown(code int, reason string) { g.log.Info("Shutdown started", "code", code, "reason", reason) + err := g.httpServer.Shutdown(g.context) + if err != nil { + g.log.Error("Failed to shutdown server", "error", err) + } + g.shutdownFn() - err := g.childRoutines.Wait() + err = g.childRoutines.Wait() g.log.Info("Shutdown completed", "reason", err) log.Close()