diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 5daa99d4d..219b1f4ca 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -388,7 +388,12 @@ func (s *Server) SetConfig(c ServerConfig) error { } // SetAddr sets the listening address for the server. -// The address is like ':80', '0.0.0.0:80', '127.0.0.1:80', '180.18.99.10:80', etc. +// The address is like: +// SetAddr(":80") +// SetAddr("0.0.0.0:80") +// SetAddr("127.0.0.1:80") +// SetAddr("180.18.99.10:80") +// etc. func (s *Server) SetAddr(address string) { s.config.Address = address } diff --git a/net/ghttp/ghttp_server_pprof.go b/net/ghttp/ghttp_server_pprof.go index eb37f4626..3477d68a6 100644 --- a/net/ghttp/ghttp_server_pprof.go +++ b/net/ghttp/ghttp_server_pprof.go @@ -23,12 +23,13 @@ const ( defaultPProfPattern = "/debug/pprof" ) -// StartPProfServer starts and runs a new server for pprof. -func StartPProfServer(port int, pattern ...string) { - s := GetServer(defaultPProfServerName) +// StartPProfServer starts and runs a new server for pprof in another goroutine. +func StartPProfServer(address string, pattern ...string) (s *Server, err error) { + s = GetServer(defaultPProfServerName) s.EnablePProf(pattern...) - s.SetPort(port) - s.Run() + s.SetAddr(address) + err = s.Start() + return } // EnablePProf enables PProf feature for server. diff --git a/net/ghttp/ghttp_z_unit_feature_pprof_test.go b/net/ghttp/ghttp_z_unit_feature_pprof_test.go index 75ad0bc8f..d6b355e7e 100644 --- a/net/ghttp/ghttp_z_unit_feature_pprof_test.go +++ b/net/ghttp/ghttp_z_unit_feature_pprof_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" . "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/util/guid" ) @@ -29,30 +30,37 @@ func TestServer_EnablePProf(t *testing.T) { client := g.Client() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) - r, err := client.Get(ctx, "/pprof/index") - Assert(err, nil) - Assert(r.StatusCode, 200) - r.Close() - - r, err = client.Get(ctx, "/pprof/cmdline") - Assert(err, nil) - Assert(r.StatusCode, 200) - r.Close() - - //r, err = client.Get(ctx, "/pprof/profile") - //Assert(err, nil) - //Assert(r.StatusCode, 200) - //r.Close() - - r, err = client.Get(ctx, "/pprof/symbol") - Assert(err, nil) - Assert(r.StatusCode, 200) - r.Close() - - r, err = client.Get(ctx, "/pprof/trace") - Assert(err, nil) - Assert(r.StatusCode, 200) - r.Close() + urlPaths := []string{ + "/pprof/index", "/pprof/cmdline", "/pprof/symbol", "/pprof/trace", + } + for _, urlPath := range urlPaths { + r, err := client.Get(ctx, urlPath) + AssertNil(err) + Assert(r.StatusCode, 200) + AssertNil(r.Close()) + } + }) +} + +func TestServer_StartPProfServer(t *testing.T) { + C(t, func(t *T) { + s, err := ghttp.StartPProfServer(":0") + t.AssertNil(err) + + defer ghttp.ShutdownAllServer(ctx) + + time.Sleep(100 * time.Millisecond) + client := g.Client() + client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d/debug", s.GetListenedPort())) + + urlPaths := []string{ + "/pprof/index", "/pprof/cmdline", "/pprof/symbol", "/pprof/trace", + } + for _, urlPath := range urlPaths { + r, err := client.Get(ctx, urlPath) + AssertNil(err) + Assert(r.StatusCode, 200) + AssertNil(r.Close()) + } }) - }