mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 03:05:05 +08:00
feat(net/ghttp): move plugin remove logic to Shutdown() && call Shutdown() when Run() exits (#4072)
This commit is contained in:
parent
e6bee78be4
commit
89e5285d95
@ -708,7 +708,7 @@ const (
|
||||
ctxKeyCatchSQL gctx.StrKey = `CtxKeyCatchSQL`
|
||||
ctxKeyInternalProducedSQL gctx.StrKey = `CtxKeyInternalProducedSQL`
|
||||
|
||||
linkPattern = `(\w+):([\w\-\$]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([^\?]*)\?{0,1}(.*)`
|
||||
linkPattern = `^(\w+):(.*?):(.*?)@(\w+?)\((.+?)\)/{0,1}([^\?]*)\?{0,1}(.*?)$`
|
||||
linkPatternDescription = `type:username:password@protocol(host:port)/dbname?param1=value1&...¶mN=valueN`
|
||||
)
|
||||
|
||||
|
@ -295,6 +295,40 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
|
||||
t.Assert(newNode.Charset, `utf8`)
|
||||
t.Assert(newNode.Protocol, `unix`)
|
||||
})
|
||||
// https://github.com/gogf/gf/issues/4059
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
node := &ConfigNode{
|
||||
Link: "tidb:2hcmRccccxxx9Fizz.root:wP3xxxxPIDc@tcp(xxxx.tidbcloud.com:4000)/db_name?tls=true",
|
||||
}
|
||||
newNode, err := parseConfigNodeLink(node)
|
||||
t.AssertNil(err)
|
||||
t.Assert(newNode.Type, `tidb`)
|
||||
t.Assert(newNode.User, `2hcmRccccxxx9Fizz.root`)
|
||||
t.Assert(newNode.Pass, `wP3xxxxPIDc`)
|
||||
t.Assert(newNode.Host, `xxxx.tidbcloud.com`)
|
||||
t.Assert(newNode.Port, `4000`)
|
||||
t.Assert(newNode.Name, `db_name`)
|
||||
t.Assert(newNode.Extra, `tls=true`)
|
||||
t.Assert(newNode.Charset, `utf8`)
|
||||
t.Assert(newNode.Protocol, `tcp`)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
node := &ConfigNode{
|
||||
Type: "tidb",
|
||||
Link: "2hcmRccccxxx9Fizz.root:wP3xxxxPIDc@tcp(xxxx.tidbcloud.com:4000)/db_name?tls=true",
|
||||
}
|
||||
newNode, err := parseConfigNodeLink(node)
|
||||
t.AssertNil(err)
|
||||
t.Assert(newNode.Type, `tidb`)
|
||||
t.Assert(newNode.User, `2hcmRccccxxx9Fizz.root`)
|
||||
t.Assert(newNode.Pass, `wP3xxxxPIDc`)
|
||||
t.Assert(newNode.Host, `xxxx.tidbcloud.com`)
|
||||
t.Assert(newNode.Port, `4000`)
|
||||
t.Assert(newNode.Name, `db_name`)
|
||||
t.Assert(newNode.Extra, `tls=true`)
|
||||
t.Assert(newNode.Charset, `utf8`)
|
||||
t.Assert(newNode.Protocol, `tcp`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Func_doQuoteWord(t *testing.T) {
|
||||
|
@ -454,17 +454,9 @@ func (s *Server) Run() {
|
||||
|
||||
// Blocking using the channel for graceful restart.
|
||||
<-s.closeChan
|
||||
// Remove plugins.
|
||||
if len(s.plugins) > 0 {
|
||||
for _, p := range s.plugins {
|
||||
intlog.Printf(ctx, `remove plugin: %s`, p.Name())
|
||||
if err := p.Remove(); err != nil {
|
||||
intlog.Errorf(ctx, "%+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
s.doServiceDeregister()
|
||||
s.Logger().Infof(ctx, "pid[%d]: all servers shutdown", gproc.Pid())
|
||||
|
||||
// Shutdown the server
|
||||
_ = s.Shutdown()
|
||||
}
|
||||
|
||||
// Wait blocks to wait for all servers done.
|
||||
|
@ -88,14 +88,25 @@ func (s *Server) EnableAdmin(pattern ...string) {
|
||||
s.BindObject(p, &utilAdmin{})
|
||||
}
|
||||
|
||||
// Shutdown shuts down current server.
|
||||
// Shutdown shuts the current server.
|
||||
func (s *Server) Shutdown() error {
|
||||
var ctx = context.TODO()
|
||||
// Remove plugins.
|
||||
if len(s.plugins) > 0 {
|
||||
for _, p := range s.plugins {
|
||||
s.Logger().Printf(ctx, `remove plugin: %s`, p.Name())
|
||||
if err := p.Remove(); err != nil {
|
||||
s.Logger().Errorf(ctx, "%+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.doServiceDeregister()
|
||||
// Only shut down current servers.
|
||||
// It may have multiple underlying http servers.
|
||||
for _, v := range s.servers {
|
||||
v.Shutdown(ctx)
|
||||
}
|
||||
s.Logger().Infof(ctx, "pid[%d]: all servers shutdown", gproc.Pid())
|
||||
return nil
|
||||
}
|
||||
|
@ -343,6 +343,9 @@ func (s *Server) SetConfigWithMap(m map[string]interface{}) error {
|
||||
if k, v := gutil.MapPossibleItemByKey(m, "FormParsingMemory"); k != "" {
|
||||
m[k] = gfile.StrToSize(gconv.String(v))
|
||||
}
|
||||
if _, v := gutil.MapPossibleItemByKey(m, "Logger"); v == nil {
|
||||
intlog.Printf(context.TODO(), "SetConfigWithMap: set Logger nil")
|
||||
}
|
||||
// Update the current configuration object.
|
||||
// It only updates the configured keys not all the object.
|
||||
if err := gconv.Struct(m, &s.config); err != nil {
|
||||
@ -379,8 +382,10 @@ func (s *Server) SetConfig(c ServerConfig) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := s.config.Logger.SetLevelStr(s.config.LogLevel); err != nil {
|
||||
intlog.Errorf(context.TODO(), `%+v`, err)
|
||||
if s.config.Logger != nil {
|
||||
if err := s.config.Logger.SetLevelStr(s.config.LogLevel); err != nil {
|
||||
intlog.Errorf(context.TODO(), `%+v`, err)
|
||||
}
|
||||
}
|
||||
gracefulEnabled = c.Graceful
|
||||
intlog.Printf(context.TODO(), "SetConfig: %+v", s.config)
|
||||
|
@ -68,10 +68,10 @@ func (s *Server) GetLogPath() string {
|
||||
|
||||
// IsAccessLogEnabled checks whether the access log enabled.
|
||||
func (s *Server) IsAccessLogEnabled() bool {
|
||||
return s.config.AccessLogEnabled
|
||||
return s.config.AccessLogEnabled && s.config.Logger != nil
|
||||
}
|
||||
|
||||
// IsErrorLogEnabled checks whether the error log enabled.
|
||||
func (s *Server) IsErrorLogEnabled() bool {
|
||||
return s.config.ErrorLogEnabled
|
||||
return s.config.ErrorLogEnabled && s.config.Logger != nil
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ func (s *Server) handleAccessLog(r *Request) {
|
||||
r.GetClientIp(), r.Referer(), r.UserAgent(),
|
||||
)
|
||||
logger := instance.GetOrSetFuncLock(loggerInstanceKey, func() interface{} {
|
||||
l := s.Logger().Clone()
|
||||
l := s.Logger()
|
||||
l.SetFile(s.config.AccessLogPattern)
|
||||
l.SetStdoutPrint(s.config.LogStdout)
|
||||
l.SetLevelPrint(false)
|
||||
@ -73,7 +73,7 @@ func (s *Server) handleErrorLog(err error, r *Request) {
|
||||
content += ", " + err.Error()
|
||||
}
|
||||
logger := instance.GetOrSetFuncLock(loggerInstanceKey, func() interface{} {
|
||||
l := s.Logger().Clone()
|
||||
l := s.Logger()
|
||||
l.SetStack(false)
|
||||
l.SetFile(s.config.ErrorLogPattern)
|
||||
l.SetStdoutPrint(s.config.LogStdout)
|
||||
|
@ -618,3 +618,15 @@ func Test_Issue3789(t *testing.T) {
|
||||
t.Assert(c.GetContent(ctx, "/hello?id=&secondId=2&thirdId=3"), expect)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/4047
|
||||
func Test_Issue4047(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := g.Server(guid.S())
|
||||
err := s.SetConfigWithMap(g.Map{
|
||||
"logger": nil,
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(s.Logger(), nil)
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user