From b29bb78a7e0325a2522e14c25053c06a9a8ea1db Mon Sep 17 00:00:00 2001 From: "Stephen McQuay (smcquay)" Date: Sat, 15 Jul 2017 12:33:00 -0600 Subject: [PATCH] net/http: add example to Server.Shutdown Fixes #19579 Change-Id: Id99ca6de94d8d895dfaed1ed507e9d36c7f60670 Reviewed-on: https://go-review.googlesource.com/48869 Reviewed-by: Brad Fitzpatrick --- src/net/http/example_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/net/http/example_test.go b/src/net/http/example_test.go index 1774795d37..9de0893e87 100644 --- a/src/net/http/example_test.go +++ b/src/net/http/example_test.go @@ -5,11 +5,14 @@ package http_test import ( + "context" "fmt" "io" "io/ioutil" "log" "net/http" + "os" + "os/signal" ) func ExampleHijacker() { @@ -109,3 +112,28 @@ func ExampleResponseWriter_trailers() { w.Header().Set("AtEnd3", "value 3") // These will appear as trailers. }) } + +func ExampleServer_Shutdown() { + var srv http.Server + + idleConnsClosed := make(chan struct{}) + go func() { + sigint := make(chan os.Signal, 1) + signal.Notify(sigint, os.Interrupt) + <-sigint + + // We received an interrupt signal, shut down. + if err := srv.Shutdown(context.Background()); err != nil { + // Error from closing listeners, or context timeout: + log.Printf("HTTP server Shutdown: %v", err) + } + close(idleConnsClosed) + }() + + if err := srv.ListenAndServe(); err != http.ErrServerClosed { + // Error starting or closing listener: + log.Printf("HTTP server ListenAndServe: %v", err) + } + + <-idleConnsClosed +}