This shows how to use the upgrader with the graceful shutdown facilities of net/http and using the stub implementation if on an unsupported platform.
()
| 29 | // with the graceful shutdown facilities of net/http |
| 30 | // and using the stub implementation if on an unsupported platform. |
| 31 | func Example_httpShutdown() { |
| 32 | var ( |
| 33 | listenAddr = flag.String("listen", "localhost:8080", "`Address` to listen on") |
| 34 | pidFile = flag.String("pid-file", "", "`Path` to pid file") |
| 35 | ) |
| 36 | |
| 37 | flag.Parse() |
| 38 | log.SetPrefix(fmt.Sprintf("%d ", os.Getpid())) |
| 39 | |
| 40 | var upg upgrader |
| 41 | upg, err := tableflip.New(tableflip.Options{ |
| 42 | PIDFile: *pidFile, |
| 43 | }) |
| 44 | if errors.Is(err, tableflip.ErrNotSupported) { |
| 45 | upg, _ = testing.New() |
| 46 | } else if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | defer upg.Stop() |
| 50 | |
| 51 | // Do an upgrade on SIGHUP |
| 52 | // NOTE: With `testing.Upgrader` this goroutine is useless |
| 53 | // You may choose to enclose it inside an `if` statement block. |
| 54 | go func() { |
| 55 | sig := make(chan os.Signal, 1) |
| 56 | signal.Notify(sig, syscall.SIGHUP) |
| 57 | for range sig { |
| 58 | err := upg.Upgrade() |
| 59 | if err != nil { |
| 60 | log.Println("Upgrade failed:", err) |
| 61 | } |
| 62 | } |
| 63 | }() |
| 64 | |
| 65 | // Listen must be called before Ready |
| 66 | ln, err := upg.Listen("tcp", *listenAddr) |
| 67 | if err != nil { |
| 68 | log.Fatalln("Can't listen:", err) |
| 69 | } |
| 70 | |
| 71 | server := http.Server{ |
| 72 | // Set timeouts, etc. |
| 73 | } |
| 74 | |
| 75 | go func() { |
| 76 | err := server.Serve(ln) |
| 77 | if err != http.ErrServerClosed { |
| 78 | log.Println("HTTP server:", err) |
| 79 | } |
| 80 | }() |
| 81 | |
| 82 | log.Printf("ready") |
| 83 | if err := upg.Ready(); err != nil { |
| 84 | panic(err) |
| 85 | } |
| 86 | <-upg.Exit() |
| 87 | |
| 88 | // Make sure to set a deadline on exiting the process |
nothing calls this directly
no test coverage detected
searching dependent graphs…