1
0
mirror of https://github.com/golang/go synced 2024-11-20 00:44:45 -07:00

net/http: add example to Server.Shutdown

Fixes #19579

Change-Id: Id99ca6de94d8d895dfaed1ed507e9d36c7f60670
Reviewed-on: https://go-review.googlesource.com/48869
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Stephen McQuay (smcquay) 2017-07-15 12:33:00 -06:00 committed by Brad Fitzpatrick
parent f7df55d174
commit b29bb78a7e

View File

@ -5,11 +5,14 @@
package http_test package http_test
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"os/signal"
) )
func ExampleHijacker() { func ExampleHijacker() {
@ -109,3 +112,28 @@ func ExampleResponseWriter_trailers() {
w.Header().Set("AtEnd3", "value 3") // These will appear as 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
}