Created
October 4, 2017 19:20
-
-
Save akutz/2cb1fdaa4d5cd19555400adb3f38386e to your computer and use it in GitHub Desktop.
An example program using the Goodbye library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
"time" | |
"github.com/thecodeteam/goodbye" | |
) | |
func main() { | |
// Create a context to use with the Goodbye library's functions. | |
ctx := context.Background() | |
// Always defer `goodbye.Exit` as early as possible since it is | |
// safe to execute no matter what. The -1 argument indicates to | |
// the Exit function to use the default exit code specified by | |
// the package field goodbye.ExitCode. | |
defer goodbye.Exit(ctx, -1) | |
// Invoke `goodbye.Notify` to begin trapping signals this process | |
// might receive. The Notify function can specify which signals are | |
// trapped, but if none are specified then a default list is used. | |
// The default set is platform dependent. See the files | |
// "goodbye_GOOS.go" for more information. | |
goodbye.Notify(ctx) | |
// Define an HTTP server | |
var s *http.Server | |
// Register three functions that are executed exactly once when the | |
// process exits, either normally or as a result of a signal. | |
// This function removes the UNIX socket file used by the HTTP server. | |
// Because the HTTP server should be shutdown before its socket file is | |
// removed, this function is registered with a low priority. | |
goodbye.RegisterWithPriority(func(ctx context.Context, sig os.Signal) { | |
// Remove the UNIX socket file. | |
os.RemoveAll("http.sock") | |
log.Println("http: removed socket file: http.sock") | |
}, 500) | |
// This function detects whether or not the program is exiting as | |
// a result of a signal or not. This should be the first handler | |
// to execute. | |
goodbye.Register(func(ctx context.Context, sig os.Signal) { | |
// If the process is shutting down because a signal was | |
// received then log that signal. | |
if !goodbye.IsNormalExit(sig) { | |
log.Printf("http: received signal: %v\n", sig) | |
} | |
}) | |
// This function shuts down the HTTP server. Because no priority | |
// is specified this function will have a priority of 0, the default | |
// priority. Because the previous handler is also registered with | |
// a priority of 0, it will execute first. Handlers registered at | |
// the same priority level are executed in order of registration. | |
goodbye.Register(func(ctx context.Context, sig os.Signal) { | |
s.Shutdown(ctx) | |
log.Println("http: shutdown") | |
}) | |
l, err := net.Listen("unix", "http.sock") | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
// Start an HTTP server. | |
s = &http.Server{Handler: http.FileServer(http.Dir("."))} | |
go s.Serve(l) | |
log.Println("http: serving") | |
// Wait 5 seconds then let the process complete. | |
time.Sleep(time.Duration(5) * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment