Skip to content

Instantly share code, notes, and snippets.

@kirugan
Created February 5, 2019 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/f608ae7282bd8457514a47200916fc0f to your computer and use it in GitHub Desktop.
Save kirugan/f608ae7282bd8457514a47200916fc0f to your computer and use it in GitHub Desktop.
Test http.Shutdown and request.Context
package main
import (
"context"
"fmt"
"net/http"
"time"
)
func main() {
http.HandleFunc("/", func (_ http.ResponseWriter, req *http.Request) {
ctx := req.Context()
select {
case <- time.After(time.Minute):
case <- ctx.Done():
}
fmt.Println("After finish")
})
// check later whi
h := &http.Server{Addr:":8080"}
go h.ListenAndServe()
time.Sleep(time.Second * 10)
fmt.Println("Start shutdown")
h.Shutdown(context.Background())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment