signals on davidchua https://dchua.com/tags/signals/ Recent content in signals on davidchua Hugo -- gohugo.io en-us Wed, 28 Jun 2017 00:00:00 +0000 The Signal Pattern https://dchua.com/posts/2017-06-28-the-signal-pattern/ Wed, 28 Jun 2017 00:00:00 +0000 https://dchua.com/posts/2017-06-28-the-signal-pattern/ The signal pattern can be used to ensure your long-lived processes (ie. servers) are able to handle signals being sent to the process. import ... type Handler struct { } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{}`)) } func main() { h := &Handler{} sigCh := make(chan os.Signal, 1) errCh := make(chan error, 1) signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP) go func() { err := http.ListenAndServe(":3000", h) errCh <- err }() for { select { case sig := <-sigCh: switch sig { case syscall.