forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add graceful-shutdown example for go 1.8 (gin-gonic#835)
* docs: add graceful-shutdown example for go 1.8 * fix testing Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
- Loading branch information
Showing
7 changed files
with
193 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// +build go1.8 | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.GET("/", func(c *gin.Context) { | ||
c.String(http.StatusOK, "Welcome Gin Server") | ||
}) | ||
|
||
server := &http.Server{ | ||
Addr: ":8080", | ||
Handler: router, | ||
} | ||
|
||
quit := make(chan os.Signal) | ||
signal.Notify(quit, os.Interrupt) | ||
|
||
go func() { | ||
<-quit | ||
log.Println("receive interrupt signal") | ||
if err := server.Close(); err != nil { | ||
log.Fatal("Server Close:", err) | ||
} | ||
}() | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
if err == http.ErrServerClosed { | ||
log.Println("Server closed under request") | ||
} else { | ||
log.Fatal("Server closed unexpect") | ||
} | ||
} | ||
|
||
log.Println("Server exist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// +build go1.8 | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.GET("/", func(c *gin.Context) { | ||
time.Sleep(5 * time.Second) | ||
c.String(http.StatusOK, "Welcome Gin Server") | ||
}) | ||
|
||
srv := &http.Server{ | ||
Addr: ":8080", | ||
Handler: router, | ||
} | ||
|
||
go func() { | ||
// service connections | ||
if err := srv.ListenAndServe(); err != nil { | ||
log.Printf("listen: %s\n", err) | ||
} | ||
}() | ||
|
||
// Wait for interrupt signal to gracefully shutdown the server with | ||
// a timeout of 5 seconds. | ||
quit := make(chan os.Signal) | ||
signal.Notify(quit, os.Interrupt) | ||
<-quit | ||
log.Println("Shutdown Server ...") | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if err := srv.Shutdown(ctx); err != nil { | ||
log.Fatal("Server Shutdown:", err) | ||
} | ||
log.Println("Server exist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters