Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "net/http" "github.com/TsvetanMilanov/go-graceful-server-shutdown/gss" ) func main() { // Create server based on the provided handler. // The server will listen on port 8080 and there will be // no timeout for the connection draining. // StartServer will block the program execution until the server is closed. err := gss.StartServer(http.DefaultServeMux) fmt.Println(err) }
Output:
Index ¶
Examples ¶
Constants ¶
View Source
const ( // DefaultAddr The default server address. DefaultAddr = ":8080" // DefaultShutdownTimeoutSeconds the default timeout which the module will wait for // connections to drain. -1 means that the module will wait until all connections // are drained. DefaultShutdownTimeoutSeconds = -1 )
Variables ¶
Functions ¶
func StartCustomServerWithSettings ¶
StartCustomServerWithSettings starts the provided server and handles its shutdown based on the provided settings. This function is blocking.
Example ¶
package main import ( "fmt" "net/http" "os" "syscall" "github.com/TsvetanMilanov/go-graceful-server-shutdown/gss" ) func main() { settings := &gss.Settings{ // Set the signals which will trigger the graceful server shutdown. SignalsToWatch: []os.Signal{syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT}, } srv := &http.Server{Handler: http.DefaultServeMux, Addr: ":5678"} // StartServerWithSettings will block the program execution until the server is closed. err := gss.StartCustomServerWithSettings(srv, settings) fmt.Println(err) }
Output:
func StartServer ¶
StartServer creates server from the provided handler, starts it and handles its shutdown based on the default settings. This function is blocking.
Example ¶
package main import ( "fmt" "net/http" "github.com/TsvetanMilanov/go-graceful-server-shutdown/gss" ) func main() { // StartServer will block the program execution until the server is closed. err := gss.StartServer(http.DefaultServeMux) fmt.Println(err) }
Output:
func StartServerWithSettings ¶
StartServerWithSettings creates server from the provided handler, starts it and handles its shutdown based on the provided settings. This function is blocking.
Example ¶
package main import ( "fmt" "net/http" "os" "syscall" "github.com/TsvetanMilanov/go-graceful-server-shutdown/gss" ) func main() { var shutdownTimeoutSeconds int64 = 30 settings := &gss.Settings{ // Set the server address. Addr: ":5678", // Set the signals which will trigger the graceful server shutdown. SignalsToWatch: []os.Signal{syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT}, // Set shutdown timeout. ShutdownTimeoutSeconds: &shutdownTimeoutSeconds, } // StartServerWithSettings will block the program execution until the server is closed. err := gss.StartServerWithSettings(http.DefaultServeMux, settings) fmt.Println(err) }
Output:
Example (Shutdownchannel) ¶
package main import ( "fmt" "net/http" "time" "github.com/TsvetanMilanov/go-graceful-server-shutdown/gss" ) func main() { shutdownChannel := make(chan bool) settings := &gss.Settings{ ShutdownChannel: shutdownChannel, } serverChannel := make(chan error) go func() { err := gss.StartServerWithSettings(http.DefaultServeMux, settings) serverChannel <- err }() time.Sleep(500 * time.Millisecond) shutdownChannel <- true err := <-serverChannel fmt.Println(err) }
Output: <nil>
Types ¶
Click to show internal directories.
Click to hide internal directories.