From c844dc82992f5001d86d0c2eb0f4b8cd97fce372 Mon Sep 17 00:00:00 2001 From: Hooman Mohammadi Date: Tue, 12 Jan 2021 19:09:42 -0500 Subject: [PATCH] graceful shutdown run function initial setup Change-Id: I7dca9f7117e54954ad12f5c04f4dd81315008d0e --- gin.go | 7 +++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 10 insertions(+) diff --git a/gin.go b/gin.go index 1e126179..c9d8fe73 100644 --- a/gin.go +++ b/gin.go @@ -15,6 +15,7 @@ import ( "github.com/gin-gonic/gin/internal/bytesconv" "github.com/gin-gonic/gin/render" + "github.com/kuangchanglang/graceful" ) const defaultMultipartMemory = 32 << 20 // 32 MB @@ -114,6 +115,7 @@ type Engine struct { pool sync.Pool trees methodTrees maxParams uint16 + server *graceful.Server } var _ IRouter = &Engine{} @@ -311,6 +313,11 @@ func (engine *Engine) Run(addr ...string) (err error) { return } +// RunGracefully supports graceful shutdown using graceful 3rd party library +func (engine *Engine) RunGracefully(addr ...string) error { + return engine.server.Run() +} + // RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router) // Note: this method will block the calling goroutine indefinitely unless an error happens. diff --git a/go.mod b/go.mod index 884ff851..79f07c1e 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.3.3 github.com/json-iterator/go v1.1.9 + github.com/kuangchanglang/graceful v1.0.2 github.com/mattn/go-isatty v0.0.12 github.com/stretchr/testify v1.4.0 github.com/ugorji/go/codec v1.1.7 diff --git a/go.sum b/go.sum index a64b3319..7f7dd53f 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kuangchanglang/graceful v1.0.2 h1:67kR+XSl2iR5pnCIuI2bH3Ke1aT9uL2h7cZIiqRtlj0= +github.com/kuangchanglang/graceful v1.0.2/go.mod h1:fQkb+p3PRjvdiAsa65Qv78lm9CsYc4M+yhiuU1rOVtg= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=