mirror of
https://github.com/bluenviron/mediamtx
synced 2025-01-23 15:34:28 +00:00
66 lines
1.0 KiB
Go
66 lines
1.0 KiB
Go
package pprof
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
// start pprof
|
|
_ "net/http/pprof"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
)
|
|
|
|
const (
|
|
port = 9999
|
|
)
|
|
|
|
// Parent is implemented by program.
|
|
type Parent interface {
|
|
Log(logger.Level, string, ...interface{})
|
|
}
|
|
|
|
// Pprof is a performance metrics exporter.
|
|
type Pprof struct {
|
|
listener net.Listener
|
|
server *http.Server
|
|
}
|
|
|
|
// New allocates a Pprof.
|
|
func New(
|
|
listenIP string,
|
|
parent Parent,
|
|
) (*Pprof, error) {
|
|
address := listenIP + ":" + strconv.FormatInt(port, 10)
|
|
listener, err := net.Listen("tcp", address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pp := &Pprof{
|
|
listener: listener,
|
|
}
|
|
|
|
pp.server = &http.Server{
|
|
Handler: http.DefaultServeMux,
|
|
}
|
|
|
|
parent.Log(logger.Info, "[pprof] opened on "+address)
|
|
|
|
go pp.run()
|
|
return pp, nil
|
|
}
|
|
|
|
// Close closes a Pprof.
|
|
func (pp *Pprof) Close() {
|
|
pp.server.Shutdown(context.Background())
|
|
}
|
|
|
|
func (pp *Pprof) run() {
|
|
err := pp.server.Serve(pp.listener)
|
|
if err != http.ErrServerClosed {
|
|
panic(err)
|
|
}
|
|
}
|