Documentation
¶
Overview ¶
Package shutdown provides simple shutdown signals handler with callback
Take the following example: First file `main.go` simply gets the repository from the container and prints it we use **MustInvoke** method to simply present the way where we keep type safety
package main
import (
"context"
"net/http"
"os"
"time"
"log"
"github.com/vardius/shutdown"
)
func main() {
ctx := context.Background()
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello!\n")
})
stop := func() {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
log.Printf("shutting down...\n")
if err := srv.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v\n", err)
} else {
log.Printf("gracefully stopped\n")
}
}
go func() {
log.Printf("%v\n", http.ListenAndServe(":8080", nil))
stop()
os.Exit(1)
}()
}
Package shutdown provides simple shutdown signals handler with callback handler
Example ¶
package main
import (
"fmt"
"syscall"
"time"
"github.com/vardius/shutdown"
)
func main() {
// mock shutdown signall Ctrl + C
go func() {
time.Sleep(10 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
shutdown.GracefulStop(func() {
fmt.Println("shutdown")
})
}
Output: shutdown
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GracefulStop ¶
func GracefulStop(stop func())
GracefulStop handles signal and graceful shutdown
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.