Commit b29bb78a authored by Stephen McQuay (smcquay)'s avatar Stephen McQuay (smcquay) Committed by Brad Fitzpatrick

net/http: add example to Server.Shutdown

Fixes #19579

Change-Id: Id99ca6de94d8d895dfaed1ed507e9d36c7f60670
Reviewed-on: https://go-review.googlesource.com/48869Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent f7df55d1
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment