diff --git a/go.mod b/go.mod index 6daa5c7237e..d4c2922bd12 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/bluenviron/mediacommon v1.13.0 github.com/datarhei/gosrt v0.7.0 github.com/fsnotify/fsnotify v1.7.0 + github.com/gin-contrib/pprof v1.5.0 github.com/gin-gonic/gin v1.10.0 github.com/go-git/go-git/v5 v5.12.0 github.com/golang-jwt/jwt/v5 v5.2.1 diff --git a/go.sum b/go.sum index 8ba558dc3a5..7876ae11d99 100644 --- a/go.sum +++ b/go.sum @@ -69,6 +69,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/pprof v1.5.0 h1:E/Oy7g+kNw94KfdCy3bZxQFtyDnAX2V7axRS7sNYVrU= +github.com/gin-contrib/pprof v1.5.0/go.mod h1:GqFL6LerKoCQ/RSWnkYczkTJ+tOAUVN/8sbnEtaqOKs= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= diff --git a/internal/core/api_test.go b/internal/core/api_test.go index a54ec12c45f..c868d9d3eb2 100644 --- a/internal/core/api_test.go +++ b/internal/core/api_test.go @@ -1071,7 +1071,7 @@ func TestAPIProtocolKick(t *testing.T) { } `json:"items"` } httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/"+pa+"/list", nil, &out2) - require.Equal(t, 0, len(out2.Items)) + require.Empty(t, out2.Items) }) } } diff --git a/internal/core/path_manager_test.go b/internal/core/path_manager_test.go index 6b506fced67..cadd5cd58fb 100644 --- a/internal/core/path_manager_test.go +++ b/internal/core/path_manager_test.go @@ -78,7 +78,7 @@ func TestPathAutoDeletion(t *testing.T) { data, err := p.pathManager.APIPathsList() require.NoError(t, err) - require.Equal(t, 0, len(data.Items)) + require.Empty(t, data.Items) }) } } diff --git a/internal/pprof/pprof.go b/internal/pprof/pprof.go index 90c492902e5..6575778974f 100644 --- a/internal/pprof/pprof.go +++ b/internal/pprof/pprof.go @@ -6,15 +6,14 @@ import ( "net/http" "time" - // start pprof - _ "net/http/pprof" + "github.com/gin-contrib/pprof" + "github.com/gin-gonic/gin" "github.com/bluenviron/mediamtx/internal/auth" "github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/protocols/httpp" "github.com/bluenviron/mediamtx/internal/restrictnetwork" - "github.com/gin-gonic/gin" ) type pprofAuthManager interface { @@ -48,7 +47,7 @@ func (pp *PPROF) Initialize() error { router.Use(pp.middlewareOrigin) router.Use(pp.middlewareAuth) - router.Use(pp.onRequest) + pprof.Register(router) network, address := restrictnetwork.Restrict("tcp", pp.Address) @@ -117,7 +116,3 @@ func (pp *PPROF) middlewareAuth(ctx *gin.Context) { return } } - -func (pp *PPROF) onRequest(ctx *gin.Context) { - http.DefaultServeMux.ServeHTTP(ctx.Writer, ctx.Request) -} diff --git a/internal/pprof/pprof_test.go b/internal/pprof/pprof_test.go index d26de96514b..16cb16f43d4 100644 --- a/internal/pprof/pprof_test.go +++ b/internal/pprof/pprof_test.go @@ -46,3 +46,33 @@ func TestPreflightRequest(t *testing.T) { require.Equal(t, "Authorization", res.Header.Get("Access-Control-Allow-Headers")) require.Equal(t, byts, []byte{}) } + +func TestPprof(t *testing.T) { + s := &PPROF{ + Address: "127.0.0.1:9999", + AllowOrigin: "*", + ReadTimeout: conf.StringDuration(10 * time.Second), + AuthManager: test.NilAuthManager, + Parent: test.NilLogger, + } + err := s.Initialize() + require.NoError(t, err) + defer s.Close() + + tr := &http.Transport{} + defer tr.CloseIdleConnections() + hc := &http.Client{Transport: tr} + + req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:9999/debug/pprof/heap", nil) + require.NoError(t, err) + + res, err := hc.Do(req) + require.NoError(t, err) + defer res.Body.Close() + + require.Equal(t, http.StatusOK, res.StatusCode) + + byts, err := io.ReadAll(res.Body) + require.NoError(t, err) + require.NotEmpty(t, byts) +} diff --git a/internal/servers/webrtc/server_test.go b/internal/servers/webrtc/server_test.go index 80fa556199c..4da99c46104 100644 --- a/internal/servers/webrtc/server_test.go +++ b/internal/servers/webrtc/server_test.go @@ -763,5 +763,5 @@ func TestICEServerClientOnly(t *testing.T) { require.Equal(t, len(s.ICEServers), len(clientICEServers)) serverICEServers, err := s.generateICEServers(false) require.NoError(t, err) - require.Equal(t, 0, len(serverICEServers)) + require.Empty(t, serverICEServers) }