Created
February 5, 2019 20:54
-
-
Save kirugan/f608ae7282bd8457514a47200916fc0f to your computer and use it in GitHub Desktop.
Test http.Shutdown and request.Context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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