https://github.com/bsm/shutdown
https://github.com/bsm/shutdown
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/bsm/shutdown
- Owner: bsm
- License: apache-2.0
- Created: 2016-04-25T17:28:11.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2021-05-25T15:43:01.000Z (about 5 years ago)
- Last Synced: 2025-03-22T19:02:36.774Z (about 1 year ago)
- Language: Go
- Size: 10.7 KB
- Stars: 2
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Shutdown
[](http://godoc.org/github.com/bsm/shutdown)
[](https://opensource.org/licenses/Apache-2.0)
Wait for servers to terminate gracefully.
## Example:
```go
import (
"context"
"log"
"net/http"
"time"
"github.com/bsm/shutdown"
)
func main() {
srv := &http.Server{
Addr: ":8080",
Handler: http.FileServer(http.Dir("/usr/share/doc")),
}
// Wait for either SIGINT/SIGTERM or ListenAndServe to exit.
// Handle errors.
err := shutdown.Wait(srv.ListenAndServe)
if err != nil && err != http.ErrServerClosed {
log.Fatalln("Server error", err)
}
// Perform a graceful server shutdown.
log.Println("Shutting down ...")
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(timeoutCtx); err != nil {
log.Println("Shutdown error", err)
}
}
```