do not panic if pprof initialization fails

This commit is contained in:
aler9 2020-08-30 14:10:05 +02:00
parent 833ce34838
commit 01b2e741b2
3 changed files with 55 additions and 23 deletions

29
main.go
View File

@ -5,8 +5,6 @@ import (
"io"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"time"
@ -17,10 +15,6 @@ import (
var Version = "v0.0.0"
const (
pprofAddress = ":9999"
)
type logDestination int
const (
@ -165,6 +159,7 @@ type program struct {
conf *conf
logFile *os.File
metrics *metrics
pprof *pprof
serverRtsp *serverTcp
serverRtp *serverUdp
serverRtcp *serverUdp
@ -238,14 +233,10 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
}
if conf.Pprof {
go func(mux *http.ServeMux) {
p.log("[pprof] opened on " + pprofAddress)
panic((&http.Server{
Addr: pprofAddress,
Handler: mux,
}).ListenAndServe())
}(http.DefaultServeMux)
http.DefaultServeMux = http.NewServeMux()
p.pprof, err = newPprof(p)
if err != nil {
return nil, err
}
}
if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok {
@ -269,17 +260,21 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
go p.metrics.run()
}
if p.pprof != nil {
go p.pprof.run()
}
if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok {
go p.serverRtp.run()
go p.serverRtcp.run()
}
go p.serverRtsp.run()
for _, s := range p.sources {
go s.run()
}
go p.serverRtsp.run()
for _, p := range p.paths {
p.onInit()
}
@ -629,7 +624,7 @@ func (p *program) forwardFrame(path string, trackId int, streamType gortsplib.St
func main() {
_, err := newProgram(os.Args[1:], os.Stdin)
if err != nil {
log.Fatal("ERR:", err)
log.Fatal("ERR: ", err)
}
select {}

View File

@ -21,9 +21,9 @@ type metricsData struct {
type metrics struct {
p *program
listener net.Listener
mux *http.ServeMux
server *http.Server
listener net.Listener
}
func newMetrics(p *program) (*metrics, error) {
@ -44,14 +44,10 @@ func newMetrics(p *program) (*metrics, error) {
Handler: m.mux,
}
m.log("opened on " + metricsAddress)
m.p.log("[metrics] opened on " + metricsAddress)
return m, nil
}
func (m *metrics) log(format string, args ...interface{}) {
m.p.log("[metrics] "+format, args...)
}
func (m *metrics) run() {
err := m.server.Serve(m.listener)
if err != http.ErrServerClosed {

41
pprof.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"net"
"net/http"
_ "net/http/pprof"
)
const (
pprofAddress = ":9998"
)
type pprof struct {
listener net.Listener
server *http.Server
}
func newPprof(p *program) (*pprof, error) {
listener, err := net.Listen("tcp", pprofAddress)
if err != nil {
return nil, err
}
pp := &pprof{
listener: listener,
}
pp.server = &http.Server{
Handler: http.DefaultServeMux,
}
p.log("[pprof] opened on " + pprofAddress)
return pp, nil
}
func (pp *pprof) run() {
err := pp.server.Serve(pp.listener)
if err != http.ErrServerClosed {
panic(err)
}
}