Skip to content

Commit

Permalink
restore pprof service (#3848) (#3851)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Oct 8, 2024
1 parent 457a471 commit 1a6547f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/path_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
11 changes: 3 additions & 8 deletions internal/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
30 changes: 30 additions & 0 deletions internal/pprof/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/servers/webrtc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 1a6547f

Please sign in to comment.