MCPcopy
hub / github.com/cloudflare/tableflip / Example_httpShutdown

Function Example_httpShutdown

http_example_test.go:19–81  ·  view source on GitHub ↗

This shows how to use the upgrader with the graceful shutdown facilities of net/http.

()

Source from the content-addressed store, hash-verified

17// This shows how to use the upgrader
18// with the graceful shutdown facilities of net/http.
19func Example_httpShutdown() {
20 var (
21 listenAddr = flag.String("listen", "localhost:8080", "`Address` to listen on")
22 pidFile = flag.String("pid-file", "", "`Path` to pid file")
23 )
24
25 flag.Parse()
26 log.SetPrefix(fmt.Sprintf("%d ", os.Getpid()))
27
28 upg, err := tableflip.New(tableflip.Options{
29 PIDFile: *pidFile,
30 })
31 if err != nil {
32 panic(err)
33 }
34 defer upg.Stop()
35
36 // Do an upgrade on SIGHUP
37 go func() {
38 sig := make(chan os.Signal, 1)
39 signal.Notify(sig, syscall.SIGHUP)
40 for range sig {
41 err := upg.Upgrade()
42 if err != nil {
43 log.Println("Upgrade failed:", err)
44 }
45 }
46 }()
47
48 // Listen must be called before Ready
49 ln, err := upg.Listen("tcp", *listenAddr)
50 if err != nil {
51 log.Fatalln("Can't listen:", err)
52 }
53
54 server := http.Server{
55 // Set timeouts, etc.
56 }
57
58 go func() {
59 err := server.Serve(ln)
60 if err != http.ErrServerClosed {
61 log.Println("HTTP server:", err)
62 }
63 }()
64
65 log.Printf("ready")
66 if err := upg.Ready(); err != nil {
67 panic(err)
68 }
69 <-upg.Exit()
70
71 // Make sure to set a deadline on exiting the process
72 // after upg.Exit() is closed. No new upgrades can be
73 // performed if the parent doesn't exit.
74 time.AfterFunc(30*time.Second, func() {
75 log.Println("Graceful shutdown timed out")
76 os.Exit(1)

Callers

nothing calls this directly

Calls 7

NewFunction · 0.92
StopMethod · 0.65
UpgradeMethod · 0.65
ListenMethod · 0.65
ReadyMethod · 0.65
ExitMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…