Documentation
¶
Overview ¶
Package finish adds a graceful shutdown to Go's HTTP server.
It utilizes http.Server's built-in Shutdown() method.
Example ¶
package main import ( "fmt" "log" "net/http" "time" "github.com/pseidemann/finish" ) func main() { http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { time.Sleep(5 * time.Second) fmt.Fprintln(w, "world") }) srv := &http.Server{Addr: "localhost:8080"} fin := finish.New() fin.Add(srv) go func() { err := srv.ListenAndServe() if err != http.ErrServerClosed { log.Fatal(err) } }() fin.Wait() }
Output:
Index ¶
Examples ¶
Constants ¶
const DefaultTimeout = 10 * time.Second
DefaultTimeout is used if Finisher.Timeout is not set.
Variables ¶
var ( // DefaultLogger is used if Finisher.Logger is not set. It uses the Go standard log package. DefaultLogger = &defaultLogger{} // StdoutLogger can be used as a simple logger which writes to stdout via the fmt standard package. StdoutLogger = &stdoutLogger{} // DefaultSignals is used if Finisher.Signals is not set. // The default shutdown signals are: // - SIGINT (triggered by pressing Control-C) // - SIGTERM (sent by `kill $pid` or e.g. systemd stop) DefaultSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM} )
Functions ¶
This section is empty.
Types ¶
type Finisher ¶
type Finisher struct { // Timeout is the maximum amount of time to wait for still running server // requests to finish when the shutdown signal was received for each server. // It defaults to DefaultTimeout which is 10 seconds. // // The timeout can be overridden on a per server basis with passing the // WithTimeout() option to Add() while adding the server. Timeout time.Duration // Log can be set to change where finish logs to. // It defaults to DefaultLogger which uses the standard Go log package. Log Logger // Signals can be used to change which signals finish catches to initiate // the shutdown. // It defaults to DefaultSignals which contains SIGINT and SIGTERM. Signals []os.Signal // contains filtered or unexported fields }
Finisher implements graceful shutdown of servers.
func New ¶
func New() *Finisher
New creates a Finisher. This is a convenience constructor if no changes to the default configuration are needed.
func (*Finisher) Add ¶
Add a server for graceful shutdown.
Options can be passed as the second argument to change the behavior for this server:
To give the server a specific name instead of just "server #<num>":
fin.Add(srv, finish.WithName("internal server"))
To override the timeout, configured in Finisher, for this specific server:
fin.Add(srv, finish.WithTimeout(5*time.Second))
To do both at the same time:
fin.Add(srv, finish.WithName("internal server"), finish.WithTimeout(5*time.Second))
type Logger ¶
Logger is the interface expected by Finisher.Log. It allows using any logger which implements the Infof() and Errorf() methods.
type Option ¶
type Option func(keeper *serverKeeper) error
Option is what a functional option returns. The function which is returned applies the actual values when invoked later by the Option recipient.
func WithName ¶
WithName sets a custom name for the server to register.
The default name is "server" if there will be only one server registered, otherwise the names default to "server #<num>".
func WithTimeout ¶
WithTimeout overrides the global Finisher.Timeout for this specific server.