Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "fmt"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
- )
- var (
- simpleHTTPServer http.Server
- sigChan chan os.Signal
- simpleServiceShutdown chan bool
- )
- func simpleServiceStarter() {
- mux := http.NewServeMux()
- simpleHTTPServer := &http.Server{
- Addr: ":9000",
- Handler: mux,
- ReadTimeout: 10 * time.Second,
- WriteTimeout: 10 * time.Second,
- MaxHeaderBytes: 1 << 20,
- }
- fmt.Printf("nstarting http server on :9000 ")
- err := simpleHTTPServer.ListenAndServe()
- if err != http.ErrServerClosed {
- fmt.Printf("error starting simple service or closing listener - %vn", err)
- }
- fmt.Printf("simple service http server shutdown completed - %vn", err)
- // communicate with main thread
- simpleServiceShutdown <- true
- }
- func signalHandler() {
- // Handle SIGINT and SIGHUP.
- signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP)
- sig := <-sigChan
- fmt.Printf("nsignalHandler() received signal: %vn", sig)
- // gracefully shutdown http server
- err := simpleHTTPServer.Shutdown(context.Background())
- fmt.Printf("simple service shutdown on signal %v, error: %vn", sig, err)
- close(sigChan)
- }
- func main() {
- // block all async signals to this server. And we register only SIGINT and SIGHUP for now.
- signal.Ignore()
- sigChan = make(chan os.Signal, 1)
- simpleServiceShutdown = make(chan bool)
- go signalHandler()
- go simpleServiceStarter()
- <-simpleServiceShutdown // wait server to shutdown
- close(simpleServiceShutdown)
- }
- Run the program as:
- $ ./simpleHttp
- starting http server on :9000
- signalHandler() received signal: interrupt
- simple service shutdown on signal interrupt, error: <nil>
- $
- From another terminal send the signal as:
- tester 30202 4379 0 09:14 pts/8 00:00:00 ./simpleHttp
- kill -s SIGINT 30202
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement