Browse Source

Enhancement for shutdown example

The current example for graceful-shutdown is already very complete, but
can be improved for situations where the server cannot start correctly
from the beginning.
pull/658/head
Paul FREAKN Baker 4 years ago
parent
commit
ff9bbbfd50
  1. 31
      README.md

31
README.md

@ -472,31 +472,40 @@ func main() {
} }
// Run our server in a goroutine so that it doesn't block. // Run our server in a goroutine so that it doesn't block.
serverErrorChannel := make(chan error, 1)
defer close(serverErrorChannel)
go func() { go func() {
if err := srv.ListenAndServe(); err != nil { log.Printf("Running server: %s\n", server.Addr)
log.Println(err) serverErrorChannel <- server.ListenAndServe()
}
}() }()
c := make(chan os.Signal, 1) osSignalChannel := make(chan os.Signal, 1)
defer close(osSignalChannel)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C) // We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught. // SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt) signal.Notify(osSignalChannel, os.Interrupt)
// Block until we receive our signal.
<-c
// Block until we receive from one of our channels
select {
case capturedSignal := <-osSignalChannel:
log.Printf("Signal caught: %s", capturedSignal)
// Create a deadline to wait for. // Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait) ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel() defer cancel()
// Doesn't block if no connections, but will otherwise wait // Doesn't block if no connections, but will otherwise wait
// until the timeout deadline. // until the timeout deadline.
srv.Shutdown(ctx) server.Shutdown(ctx)
<-serverErrorChannel
// Optionally, you could run srv.Shutdown in a goroutine and block on // Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services // <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation. // to finalize based on context cancellation.
log.Println("shutting down") log.Println("Gracefully Shutdown")
os.Exit(0) case serverError := <-serverErrorChannel:
// Fatally exit should the server run into an irrecoverable error.
// An example of this would be when attempting to reserve a port
// that is already reserved.
log.Fatalln(serverError)
}
} }
``` ```

Loading…
Cancel
Save