chore(graceful): support custom timeout value (#466)

fixed: https://github.com/appleboy/gorush/issues/465
This commit is contained in:
Bo-Yi Wu 2020-02-05 23:23:16 +08:00 committed by GitHub
parent eba8c2d4a6
commit c379630c29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 1 deletions

View File

@ -92,6 +92,7 @@ See the default [YAML config example](config/config.yml):
core: core:
enabled: true # enabale httpd server enabled: true # enabale httpd server
address: "" # ip address to bind (default: any) address: "" # ip address to bind (default: any)
shutdown_timeout: 30 # default is 30 second
port: "8088" # ignore this port number if auto_tls is enabled (listen 443). port: "8088" # ignore this port number if auto_tls is enabled (listen 443).
worker_num: 0 # default worker number is runtime.NumCPU() worker_num: 0 # default worker number is runtime.NumCPU()
queue_num: 0 # default queue number is 8192 queue_num: 0 # default queue number is 8192

View File

@ -14,6 +14,7 @@ var defaultConf = []byte(`
core: core:
enabled: true # enabale httpd server enabled: true # enabale httpd server
address: "" # ip address to bind (default: any) address: "" # ip address to bind (default: any)
shutdown_timeout: 30 # default is 30 second
port: "8088" # ignore this port number if auto_tls is enabled (listen 443). port: "8088" # ignore this port number if auto_tls is enabled (listen 443).
worker_num: 0 # default worker number is runtime.NumCPU() worker_num: 0 # default worker number is runtime.NumCPU()
queue_num: 0 # default queue number is 8192 queue_num: 0 # default queue number is 8192
@ -103,6 +104,7 @@ type ConfYaml struct {
type SectionCore struct { type SectionCore struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
Address string `yaml:"address"` Address string `yaml:"address"`
ShutdownTimeout int64 `yaml:"shutdown_timeout"`
Port string `yaml:"port"` Port string `yaml:"port"`
MaxNotification int64 `yaml:"max_notification"` MaxNotification int64 `yaml:"max_notification"`
WorkerNum int64 `yaml:"worker_num"` WorkerNum int64 `yaml:"worker_num"`
@ -253,6 +255,7 @@ func LoadConf(confPath string) (ConfYaml, error) {
// Core // Core
conf.Core.Address = viper.GetString("core.address") conf.Core.Address = viper.GetString("core.address")
conf.Core.Port = viper.GetString("core.port") conf.Core.Port = viper.GetString("core.port")
conf.Core.ShutdownTimeout = int64(viper.GetInt("core.shutdown_timeout"))
conf.Core.Enabled = viper.GetBool("core.enabled") conf.Core.Enabled = viper.GetBool("core.enabled")
conf.Core.WorkerNum = int64(viper.GetInt("core.worker_num")) conf.Core.WorkerNum = int64(viper.GetInt("core.worker_num"))
conf.Core.QueueNum = int64(viper.GetInt("core.queue_num")) conf.Core.QueueNum = int64(viper.GetInt("core.queue_num"))

View File

@ -39,6 +39,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
// Core // Core
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.Address) assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.Address)
assert.Equal(suite.T(), "8088", suite.ConfGorushDefault.Core.Port) assert.Equal(suite.T(), "8088", suite.ConfGorushDefault.Core.Port)
assert.Equal(suite.T(), int64(30), suite.ConfGorushDefault.Core.ShutdownTimeout)
assert.Equal(suite.T(), true, suite.ConfGorushDefault.Core.Enabled) assert.Equal(suite.T(), true, suite.ConfGorushDefault.Core.Enabled)
assert.Equal(suite.T(), int64(runtime.NumCPU()), suite.ConfGorushDefault.Core.WorkerNum) assert.Equal(suite.T(), int64(runtime.NumCPU()), suite.ConfGorushDefault.Core.WorkerNum)
assert.Equal(suite.T(), int64(8192), suite.ConfGorushDefault.Core.QueueNum) assert.Equal(suite.T(), int64(8192), suite.ConfGorushDefault.Core.QueueNum)
@ -112,6 +113,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
func (suite *ConfigTestSuite) TestValidateConf() { func (suite *ConfigTestSuite) TestValidateConf() {
// Core // Core
assert.Equal(suite.T(), "8088", suite.ConfGorush.Core.Port) assert.Equal(suite.T(), "8088", suite.ConfGorush.Core.Port)
assert.Equal(suite.T(), int64(30), suite.ConfGorush.Core.ShutdownTimeout)
assert.Equal(suite.T(), true, suite.ConfGorush.Core.Enabled) assert.Equal(suite.T(), true, suite.ConfGorush.Core.Enabled)
assert.Equal(suite.T(), int64(runtime.NumCPU()), suite.ConfGorush.Core.WorkerNum) assert.Equal(suite.T(), int64(runtime.NumCPU()), suite.ConfGorush.Core.WorkerNum)
assert.Equal(suite.T(), int64(8192), suite.ConfGorush.Core.QueueNum) assert.Equal(suite.T(), int64(8192), suite.ConfGorush.Core.QueueNum)

View File

@ -1,6 +1,7 @@
core: core:
enabled: true # enabale httpd server enabled: true # enabale httpd server
address: "" # ip address to bind (default: any) address: "" # ip address to bind (default: any)
shutdown_timeout: 30 # default is 30 second
port: "8088" # ignore this port number if auto_tls is enabled (listen 443). port: "8088" # ignore this port number if auto_tls is enabled (listen 443).
worker_num: 0 # default worker number is runtime.NumCPU() worker_num: 0 # default worker number is runtime.NumCPU()
queue_num: 0 # default queue number is 8192 queue_num: 0 # default queue number is 8192

View File

@ -8,6 +8,7 @@ import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"net/http" "net/http"
"time"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
@ -73,6 +74,9 @@ func listenAndServe(ctx context.Context, s *http.Server) error {
g.Go(func() error { g.Go(func() error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
timeout := time.Duration(PushConf.Core.ShutdownTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return s.Shutdown(ctx) return s.Shutdown(ctx)
} }
}) })
@ -87,6 +91,9 @@ func listenAndServeTLS(ctx context.Context, s *http.Server) error {
g.Go(func() error { g.Go(func() error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
timeout := time.Duration(PushConf.Core.ShutdownTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return s.Shutdown(ctx) return s.Shutdown(ctx)
} }
}) })

View File

@ -252,7 +252,7 @@ func main() {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(int(gorush.PushConf.Core.WorkerNum)) wg.Add(int(gorush.PushConf.Core.WorkerNum))
ctx := withContextFunc(context.Background(), func() { ctx := withContextFunc(context.Background(), func() {
gorush.LogAccess.Info("close the notification queue channel") gorush.LogAccess.Info("close the notification queue channel, current queue len: ", len(gorush.QueueNotification))
close(gorush.QueueNotification) close(gorush.QueueNotification)
wg.Wait() wg.Wait()
close(finished) close(finished)

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"net/http" "net/http"
"sync" "sync"
"time"
"github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/rpc/proto" "github.com/appleboy/gorush/rpc/proto"
@ -122,6 +123,9 @@ func RunGRPCServer(ctx context.Context) error {
g.Go(func() error { g.Go(func() error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
timeout := time.Duration(gorush.PushConf.Core.ShutdownTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return srv.Shutdown(ctx) return srv.Shutdown(ctx)
} }
}) })