Improved the graceful shutdown and restart section and removed… (#2288)
This commit is contained in:
		
							
								
								
									
										32
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								README.md
									
									
									
									
									
								
							@ -67,7 +67,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
 | 
			
		||||
    - [Custom HTTP configuration](#custom-http-configuration)
 | 
			
		||||
    - [Support Let's Encrypt](#support-lets-encrypt)
 | 
			
		||||
    - [Run multiple service using Gin](#run-multiple-service-using-gin)
 | 
			
		||||
    - [Graceful restart or stop](#graceful-restart-or-stop)
 | 
			
		||||
    - [Graceful shutdown or restart](#graceful-shutdown-or-restart)
 | 
			
		||||
    - [Build a single binary with templates](#build-a-single-binary-with-templates)
 | 
			
		||||
    - [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)
 | 
			
		||||
    - [Try to bind body into different structs](#try-to-bind-body-into-different-structs)
 | 
			
		||||
@ -1687,12 +1687,13 @@ func main() {
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Graceful restart or stop
 | 
			
		||||
### Graceful shutdown or restart
 | 
			
		||||
 | 
			
		||||
Do you want to graceful restart or stop your web server?
 | 
			
		||||
There are some ways this can be done.
 | 
			
		||||
There are a few approaches you can use to perform a graceful shutdown or restart. You can make use of third-party packages specifically built for that, or you can manually do the same with the functions and methods from the built-in packages.
 | 
			
		||||
 | 
			
		||||
We can use [fvbock/endless](https://github.com/fvbock/endless) to replace the default `ListenAndServe`. Refer issue [#296](https://github.com/gin-gonic/gin/issues/296) for more details.
 | 
			
		||||
#### Third-party packages
 | 
			
		||||
 | 
			
		||||
We can use [fvbock/endless](https://github.com/fvbock/endless) to replace the default `ListenAndServe`. Refer to issue [#296](https://github.com/gin-gonic/gin/issues/296) for more details.
 | 
			
		||||
 | 
			
		||||
```go
 | 
			
		||||
router := gin.Default()
 | 
			
		||||
@ -1701,13 +1702,15 @@ router.GET("/", handler)
 | 
			
		||||
endless.ListenAndServe(":4242", router)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
An alternative to endless:
 | 
			
		||||
Alternatives:
 | 
			
		||||
 | 
			
		||||
* [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully.
 | 
			
		||||
* [graceful](https://github.com/tylerb/graceful): Graceful is a Go package enabling graceful shutdown of an http.Handler server.
 | 
			
		||||
* [grace](https://github.com/facebookgo/grace): Graceful restart & zero downtime deploy for Go servers.
 | 
			
		||||
 | 
			
		||||
If you are using Go 1.8, you may not need to use this library! Consider using http.Server's built-in [Shutdown()](https://golang.org/pkg/net/http/#Server.Shutdown) method for graceful shutdowns. See the full [graceful-shutdown](https://github.com/gin-gonic/examples/tree/master/graceful-shutdown) example with gin.
 | 
			
		||||
#### Manually
 | 
			
		||||
 | 
			
		||||
In case you are using Go 1.8 or a later version, you may not need to use those libraries. Consider using `http.Server`'s built-in [Shutdown()](https://golang.org/pkg/net/http/#Server.Shutdown) method for graceful shutdowns. The example below describes its usage, and we've got more examples using gin [here](https://github.com/gin-gonic/examples/tree/master/graceful-shutdown).
 | 
			
		||||
 | 
			
		||||
```go
 | 
			
		||||
// +build go1.8
 | 
			
		||||
@ -1738,8 +1741,9 @@ func main() {
 | 
			
		||||
		Handler: router,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Initializing the server in a goroutine so that
 | 
			
		||||
	// it won't block the graceful shutdown handling below
 | 
			
		||||
	go func() {
 | 
			
		||||
		// service connections
 | 
			
		||||
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
 | 
			
		||||
			log.Fatalf("listen: %s\n", err)
 | 
			
		||||
		}
 | 
			
		||||
@ -1753,18 +1757,16 @@ func main() {
 | 
			
		||||
	// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
 | 
			
		||||
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
 | 
			
		||||
	<-quit
 | 
			
		||||
	log.Println("Shutdown Server ...")
 | 
			
		||||
	log.Println("Shuting down server...")
 | 
			
		||||
 | 
			
		||||
	// The context is used to inform the server it has 5 seconds to finish
 | 
			
		||||
	// the request it is currently handling
 | 
			
		||||
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
 | 
			
		||||
	defer cancel()
 | 
			
		||||
	if err := srv.Shutdown(ctx); err != nil {
 | 
			
		||||
		log.Fatal("Server Shutdown:", err)
 | 
			
		||||
	}
 | 
			
		||||
	// catching ctx.Done(). timeout of 5 seconds.
 | 
			
		||||
	select {
 | 
			
		||||
	case <-ctx.Done():
 | 
			
		||||
		log.Println("timeout of 5 seconds.")
 | 
			
		||||
		log.Fatal("Server forced to shutdown:", err)
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	log.Println("Server exiting")
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user