From 60690fc7281c4a1c8a880fea6215e6e4afb42746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Ga=C4=87e=C5=A1a?= Date: Wed, 26 May 2021 12:31:42 +0200 Subject: [PATCH] fixed: graceful shutdown of http servers --- server/server.go | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/server/server.go b/server/server.go index 3c27e9f..581b32f 100644 --- a/server/server.go +++ b/server/server.go @@ -44,10 +44,8 @@ func (s Server) listenAndServe(ctx context.Context) error { Handler: s.Handler, } g.Go(func() error { - select { - case <-ctx.Done(): - return s1.Shutdown(ctx) - } + <-ctx.Done() + return s1.Shutdown(context.Background()) }) g.Go(func() error { return s1.ListenAndServe() @@ -75,12 +73,18 @@ func (s Server) listenAndServeTLS(ctx context.Context) error { ) }) g.Go(func() error { - select { - case <-ctx.Done(): - s1.Shutdown(ctx) - s2.Shutdown(ctx) - return nil - } + <-ctx.Done() + + var gShutdown errgroup.Group + + gShutdown.Go(func() error { + return s1.Shutdown(context.Background()) + }) + gShutdown.Go(func() error { + return s2.Shutdown(context.Background()) + }) + + return gShutdown.Wait() }) return g.Wait() } @@ -114,12 +118,18 @@ func (s Server) listenAndServeAcme(ctx context.Context) error { return s2.ListenAndServeTLS("", "") }) g.Go(func() error { - select { - case <-ctx.Done(): - s1.Shutdown(ctx) - s2.Shutdown(ctx) - return nil - } + <-ctx.Done() + + var gShutdown errgroup.Group + + gShutdown.Go(func() error { + return s1.Shutdown(context.Background()) + }) + gShutdown.Go(func() error { + return s2.Shutdown(context.Background()) + }) + + return gShutdown.Wait() }) return g.Wait() }