2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-08-30 12:10:05 +00:00
|
|
|
|
|
|
|
import (
|
2020-08-30 12:27:26 +00:00
|
|
|
"context"
|
2020-08-30 12:10:05 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-11-05 11:30:25 +00:00
|
|
|
|
|
|
|
// start pprof
|
2020-08-30 12:10:05 +00:00
|
|
|
_ "net/http/pprof"
|
2020-12-08 11:21:06 +00:00
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2020-08-30 12:10:05 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type pprofParent interface {
|
2020-12-08 11:21:06 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type pprof struct {
|
2021-11-15 19:13:54 +00:00
|
|
|
parent pprofParent
|
|
|
|
|
2021-11-03 15:37:08 +00:00
|
|
|
ln net.Listener
|
|
|
|
server *http.Server
|
2020-08-30 12:10:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newPPROF(
|
2021-04-24 16:25:19 +00:00
|
|
|
address string,
|
2021-07-24 13:55:42 +00:00
|
|
|
parent pprofParent,
|
|
|
|
) (*pprof, error) {
|
2021-11-03 15:37:08 +00:00
|
|
|
ln, err := net.Listen("tcp", address)
|
2020-08-30 12:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
pp := &pprof{
|
2021-11-15 19:13:54 +00:00
|
|
|
parent: parent,
|
|
|
|
ln: ln,
|
2020-08-30 12:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pp.server = &http.Server{
|
|
|
|
Handler: http.DefaultServeMux,
|
|
|
|
}
|
|
|
|
|
2021-11-15 19:13:54 +00:00
|
|
|
pp.log(logger.Info, "listener opened on "+address)
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
go pp.run()
|
2021-07-24 13:55:42 +00:00
|
|
|
|
2020-08-30 12:10:05 +00:00
|
|
|
return pp, nil
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (pp *pprof) close() {
|
2020-10-19 20:17:48 +00:00
|
|
|
pp.server.Shutdown(context.Background())
|
2021-11-15 19:13:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (pp *pprof) run() {
|
2021-11-03 15:37:08 +00:00
|
|
|
err := pp.server.Serve(pp.ln)
|
2020-08-30 12:10:05 +00:00
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|