From 46ddd4259cac975be1eb11b4f1192264f582db16 Mon Sep 17 00:00:00 2001 From: Josep Jesus Bigorra Algaba <42377845+averageflow@users.noreply.github.com> Date: Wed, 13 Jan 2021 02:06:12 +0100 Subject: [PATCH] Fixes to the graceful shutdown example (#2552) * Change error comparison to use errors.Is() and add a line of whitespace before the if statement on graceful shutdown * Change from log.Fatalf to log.Printf to ensure the graceful shutdown actually works Co-authored-by: J. J. Bigorra Co-authored-by: thinkerou --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18b19430f1..0c2632441b 100644 --- a/README.md +++ b/README.md @@ -1793,8 +1793,8 @@ func main() { // Initializing the server in a goroutine so that // it won't block the graceful shutdown handling below go func() { - if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Fatalf("listen: %s\n", err) + if err := srv.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) { + log.Printf("listen: %s\n", err) } }() @@ -1812,6 +1812,7 @@ func main() { // the request it is currently handling ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() + if err := srv.Shutdown(ctx); err != nil { log.Fatal("Server forced to shutdown:", err) }