restore pprof service (#3848) (#3851)

This commit is contained in:
Alessandro Ros 2024-10-08 17:06:47 +02:00 committed by GitHub
parent 457a47116a
commit 1a6547f13b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 39 additions and 11 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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)
})
}
}

View File

@ -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)
})
}
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}