Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net/ghttp): move plugin remove logic to Shutdown() && call Shutdown() when Run() exits #4072

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci-main.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env bash

# Define the latest Go version requirement
LATEST_GO_VERSION="1.23"

coverage=$1

# find all path that contains go.mod.
Expand Down
21 changes: 14 additions & 7 deletions .github/workflows/ci-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ concurrency:

env:
TZ: "Asia/Shanghai"

# for unit testing cases of some components that only execute on the latest go version.
LATEST_GO_VERSION: "1.23"

jobs:
code-test:
strategy:
matrix:
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
# When adding new go version to the list, make sure:
# 1. Update the `LATEST_GO_VERSION` env variable.
# 2. Update the `Report Coverage` action.
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ]

runs-on: ubuntu-20.04

# Service containers to run with `code-test`
Expand Down Expand Up @@ -185,11 +196,6 @@ jobs:
ports:
- 2181:2181

strategy:
matrix:
go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ]

steps:
# TODO: szenius/set-timezone update to node16
# sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Expand Down Expand Up @@ -256,7 +262,8 @@ jobs:

- name: Report Coverage
uses: codecov/codecov-action@v4
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
# Only report coverage on the latest go version and amd64 arch
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && matrix.go-version == '1.23' && matrix.goarch == 'amd64' }}
with:
flags: go-${{ matrix.go-version }}-${{ matrix.goarch }}
token: ${{ secrets.CODECOV_TOKEN }}
11 changes: 8 additions & 3 deletions .github/workflows/ci-sub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ concurrency:

env:
TZ: "Asia/Shanghai"

# for unit testing cases of some components that only execute on the latest go version.
LATEST_GO_VERSION: "1.23"

jobs:
code-test:
runs-on: ubuntu-latest

strategy:
matrix:
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
# When adding new go version to the list, make sure:
# 1. Update the `LATEST_GO_VERSION` env variable.
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ]

runs-on: ubuntu-latest

steps:
- name: Setup Timezone
uses: szenius/[email protected]
Expand Down
1 change: 0 additions & 1 deletion cmd/gf/go.work
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ replace (
github.com/gogf/gf/contrib/drivers/oracle/v2 => ../../contrib/drivers/oracle
github.com/gogf/gf/contrib/drivers/pgsql/v2 => ../../contrib/drivers/pgsql
github.com/gogf/gf/contrib/drivers/sqlite/v2 => ../../contrib/drivers/sqlite
github.com/gogf/gf/contrib/drivers/dm/v2 => ../../contrib/drivers/dm
github.com/gogf/gf/v2 => ../../
)
2 changes: 1 addition & 1 deletion database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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&...&paramN=valueN`
)

Expand Down
34 changes: 34 additions & 0 deletions database/gdb/gdb_z_mysql_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 3 additions & 11 deletions net/ghttp/ghttp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion net/ghttp/ghttp_server_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
gqcn marked this conversation as resolved.
Show resolved Hide resolved
// 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
}
9 changes: 7 additions & 2 deletions net/ghttp/ghttp_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_server_config_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_server_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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