https://github.com/vardius/shutdown
Simple go signals handler for performing graceful shutdown by executing callback function
https://github.com/vardius/shutdown
graceful graceful-shutdown gracefully shutdown signal signals
Last synced: 6 months ago
JSON representation
Simple go signals handler for performing graceful shutdown by executing callback function
- Host: GitHub
- URL: https://github.com/vardius/shutdown
- Owner: vardius
- License: mit
- Created: 2019-06-18T22:23:32.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-22T06:43:30.000Z (over 5 years ago)
- Last Synced: 2025-03-24T16:56:05.117Z (7 months ago)
- Topics: graceful, graceful-shutdown, gracefully, shutdown, signal, signals
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 20
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
β²οΈ shutdown
================
[](https://travis-ci.org/vardius/shutdown)
[](https://goreportcard.com/report/github.com/vardius/shutdown)
[](https://codecov.io/gh/vardius/shutdown)
[](https://pkg.go.dev/github.com/vardius/shutdown)
[](https://github.com/vardius/shutdown/blob/master/LICENSE.md)
shutdown - Simple go signals handler for performing graceful shutdown by executing callback function
π ABOUT
==================================================
Contributors:* [RafaΕ Lorenz](http://rafallorenz.com)
Want to contribute ? Feel free to send pull requests!
Have problems, bugs, feature ideas?
We are using the github [issue tracker](https://github.com/vardius/shutdown/issues) to manage them.## π Documentation
For __examples__ **visit [godoc#pkg-examples](http://godoc.org/github.com/vardius/shutdown#pkg-examples)**
For **GoDoc** reference, **visit [pkg.go.dev](https://pkg.go.dev/github.com/vardius/shutdown)**
π HOW TO USE
==================================================For detailed breakdown of example [How to handle signals with Go to graceful shutdown HTTP server](https://rafallorenz.com/go/handle-signals-to-graceful-shutdown-http-server/)
## π« Basic example
```go
package mainimport (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"syscall"
"time""github.com/vardius/shutdown"
)func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello!")
})httpServer := &http.Server{
Addr: ":8080",
Handler: mux,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}stop := func() {
gracefulCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()if err := httpServer.Shutdown(gracefulCtx); err != nil {
log.Printf("shutdown error: %v\n", err)
} else {
log.Printf("gracefully stopped\n")
}
}// Run server
go func() {
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
log.Printf("HTTP server ListenAndServe: %v", err)
os.Exit(1)
}
}()shutdown.GracefulStop(stop) // will block until shutdown signal is received
}
```π [License](LICENSE.md)
-------This package is released under the MIT license. See the complete license in the package