Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/TV4/graceful
Graceful shutdown of Go 1.8+ servers using Server.Shutdown
https://github.com/TV4/graceful
go graceful package shutdown
Last synced: 29 days ago
JSON representation
Graceful shutdown of Go 1.8+ servers using Server.Shutdown
- Host: GitHub
- URL: https://github.com/TV4/graceful
- Owner: TV4
- License: mit
- Archived: true
- Created: 2017-06-19T11:54:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-06T13:30:13.000Z (almost 2 years ago)
- Last Synced: 2024-08-03T23:28:41.814Z (4 months ago)
- Topics: go, graceful, package, shutdown
- Language: Go
- Size: 26.4 KB
- Stars: 136
- Watchers: 27
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - graceful
README
**Deprecated; this package is no longer maintained.**
# graceful
[![Build Status](https://travis-ci.org/TV4/graceful.svg?branch=master)](https://travis-ci.org/TV4/graceful)
[![Go Report Card](https://goreportcard.com/badge/github.com/TV4/graceful)](https://goreportcard.com/report/github.com/TV4/graceful)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/TV4/graceful)
[![License MIT](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/TV4/graceful#license-mit)## Installation
go get -u github.com/TV4/graceful
## Usage
```go
package mainimport (
"log"
"net/http"
"time""github.com/TV4/graceful"
)type server struct{}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.Write([]byte("Hello!"))
}func main() {
addr := ":2017"log.Printf("Listening on http://0.0.0.0%s\n", addr)
graceful.ListenAndServe(&http.Server{
Addr: addr,
Handler: &server{},
})
}
``````
$ go run main.go
2017/06/19 16:35:28 Listening on http://0.0.0.0:2017
^C
```### You can also use `graceful.LogListenAndServe`
```go
package mainimport (
"net/http"
"time""github.com/TV4/graceful"
)type server struct{}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.Write([]byte("Hello!"))
}func main() {
graceful.LogListenAndServe(&http.Server{
Addr: ":2017",
Handler: &server{},
})
}
``````
$ go run main.go
Listening on http://0.0.0.0:2017
^C
Server shutdown with timeout: 15s
Finished all in-flight HTTP requests
Shutdown finished 14s before deadline
```### And optionally your handler can implement the Shutdowner interface
```go
package mainimport (
"context"
"fmt"
"net/http"graceful "github.com/TV4/graceful"
)func main() {
graceful.LogListenAndServe(&http.Server{
Addr: ":8080",
Handler: &server{},
})
}type server struct{}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}func (s *server) Shutdown(ctx context.Context) error {
// Here you can do anything that you need to do
// after the *http.Server has stopped accepting
// new connections and finished its Shutdown// The ctx is the same as the context used to
// perform *https.Server.Shutdown and thus
// shares the timeout (15 seconds by default)fmt.Println("Finished *server.Shutdown")
return nil
}
``````
$ go run main.go
Listening on http://0.0.0.0:8080
^C
Server shutdown with timeout: 15s
Finished all in-flight HTTP requests
Shutting down handler with timeout: 15s
Finished *server.Shutdown
Shutdown finished 15s before deadline
```## License (MIT)
Copyright (c) 2017-2018 TV4
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.