mediamtx/internal/core/pprof.go

65 lines
1014 B
Go
Raw Normal View History

package core
import (
2020-08-30 12:27:26 +00:00
"context"
"net"
"net/http"
2020-11-05 11:30:25 +00:00
// start pprof
_ "net/http/pprof"
"github.com/aler9/rtsp-simple-server/internal/logger"
)
type pprofParent interface {
Log(logger.Level, string, ...interface{})
2020-10-19 20:17:48 +00:00
}
type pprof struct {
parent pprofParent
2021-11-03 15:37:08 +00:00
ln net.Listener
server *http.Server
}
func newPPROF(
address string,
parent pprofParent,
) (*pprof, error) {
2021-11-03 15:37:08 +00:00
ln, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
pp := &pprof{
parent: parent,
ln: ln,
}
pp.server = &http.Server{
Handler: http.DefaultServeMux,
}
pp.log(logger.Info, "listener opened on "+address)
2020-10-19 20:17:48 +00:00
go pp.run()
return pp, nil
}
func (pp *pprof) close() {
2020-10-19 20:17:48 +00:00
pp.server.Shutdown(context.Background())
pp.log(logger.Info, "listener closed")
}
func (pp *pprof) log(level logger.Level, format string, args ...interface{}) {
pp.parent.Log(level, "[pprof] "+format, args...)
2020-10-19 20:17:48 +00:00
}
func (pp *pprof) run() {
2021-11-03 15:37:08 +00:00
err := pp.server.Serve(pp.ln)
if err != http.ErrServerClosed {
panic(err)
}
}