mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-16 11:44:50 +00:00
65 lines
1014 B
Go
65 lines
1014 B
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
|
|
// start pprof
|
|
_ "net/http/pprof"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
)
|
|
|
|
type pprofParent interface {
|
|
Log(logger.Level, string, ...interface{})
|
|
}
|
|
|
|
type pprof struct {
|
|
parent pprofParent
|
|
|
|
ln net.Listener
|
|
server *http.Server
|
|
}
|
|
|
|
func newPPROF(
|
|
address string,
|
|
parent pprofParent,
|
|
) (*pprof, error) {
|
|
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)
|
|
|
|
go pp.run()
|
|
|
|
return pp, nil
|
|
}
|
|
|
|
func (pp *pprof) close() {
|
|
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...)
|
|
}
|
|
|
|
func (pp *pprof) run() {
|
|
err := pp.server.Serve(pp.ln)
|
|
if err != http.ErrServerClosed {
|
|
panic(err)
|
|
}
|
|
}
|