Skip to content

Latest commit

 

History

History
44 lines (36 loc) · 1.02 KB

signals-exit.md

File metadata and controls

44 lines (36 loc) · 1.02 KB

Signals-Exit

Exit

defer fmt.Println("bye")
// code zero indicates success, non-zero an error.
// defer will not run
os.Exit(9)

Signals

  • Notify causes package signal to relay incoming signals to "sigs".
  • If no signals are provided, all incoming signals will be relayed to "sigs".
  • Otherwise, just the provided signals will.
{
	// channel that we will get signals on it
	sigs := make(chan os.Signal)
	// channel that we will use to shutdown the app
	done := make(chan bool)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		// blocking till we get os.Signal
		<-sigs
		// release resources
		// call shutdwon functions for HTTP server,  DB ...
		fmt.Println("\nDoing graceful shutdown")
		// send message to shutdown
		done <- true
	}()

	fmt.Println("waiting for os.Signal")
	// waiting internal shutdown message
	<-done
	fmt.Println("Exit")
}