mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-11-03 01:43:48 +08:00
do not panic if pprof initialization fails
This commit is contained in:
29
main.go
29
main.go
@@ -5,8 +5,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
_ "net/http/pprof"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -17,10 +15,6 @@ import (
|
|||||||
|
|
||||||
var Version = "v0.0.0"
|
var Version = "v0.0.0"
|
||||||
|
|
||||||
const (
|
|
||||||
pprofAddress = ":9999"
|
|
||||||
)
|
|
||||||
|
|
||||||
type logDestination int
|
type logDestination int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -165,6 +159,7 @@ type program struct {
|
|||||||
conf *conf
|
conf *conf
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
metrics *metrics
|
metrics *metrics
|
||||||
|
pprof *pprof
|
||||||
serverRtsp *serverTcp
|
serverRtsp *serverTcp
|
||||||
serverRtp *serverUdp
|
serverRtp *serverUdp
|
||||||
serverRtcp *serverUdp
|
serverRtcp *serverUdp
|
||||||
@@ -238,14 +233,10 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if conf.Pprof {
|
if conf.Pprof {
|
||||||
go func(mux *http.ServeMux) {
|
p.pprof, err = newPprof(p)
|
||||||
p.log("[pprof] opened on " + pprofAddress)
|
if err != nil {
|
||||||
panic((&http.Server{
|
return nil, err
|
||||||
Addr: pprofAddress,
|
}
|
||||||
Handler: mux,
|
|
||||||
}).ListenAndServe())
|
|
||||||
}(http.DefaultServeMux)
|
|
||||||
http.DefaultServeMux = http.NewServeMux()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok {
|
if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok {
|
||||||
@@ -269,17 +260,21 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
|
|||||||
go p.metrics.run()
|
go p.metrics.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.pprof != nil {
|
||||||
|
go p.pprof.run()
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok {
|
if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok {
|
||||||
go p.serverRtp.run()
|
go p.serverRtp.run()
|
||||||
go p.serverRtcp.run()
|
go p.serverRtcp.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go p.serverRtsp.run()
|
||||||
|
|
||||||
for _, s := range p.sources {
|
for _, s := range p.sources {
|
||||||
go s.run()
|
go s.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
go p.serverRtsp.run()
|
|
||||||
|
|
||||||
for _, p := range p.paths {
|
for _, p := range p.paths {
|
||||||
p.onInit()
|
p.onInit()
|
||||||
}
|
}
|
||||||
@@ -629,7 +624,7 @@ func (p *program) forwardFrame(path string, trackId int, streamType gortsplib.St
|
|||||||
func main() {
|
func main() {
|
||||||
_, err := newProgram(os.Args[1:], os.Stdin)
|
_, err := newProgram(os.Args[1:], os.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("ERR:", err)
|
log.Fatal("ERR: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {}
|
select {}
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ type metricsData struct {
|
|||||||
|
|
||||||
type metrics struct {
|
type metrics struct {
|
||||||
p *program
|
p *program
|
||||||
|
listener net.Listener
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
server *http.Server
|
server *http.Server
|
||||||
listener net.Listener
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMetrics(p *program) (*metrics, error) {
|
func newMetrics(p *program) (*metrics, error) {
|
||||||
@@ -44,14 +44,10 @@ func newMetrics(p *program) (*metrics, error) {
|
|||||||
Handler: m.mux,
|
Handler: m.mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log("opened on " + metricsAddress)
|
m.p.log("[metrics] opened on " + metricsAddress)
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) log(format string, args ...interface{}) {
|
|
||||||
m.p.log("[metrics] "+format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *metrics) run() {
|
func (m *metrics) run() {
|
||||||
err := m.server.Serve(m.listener)
|
err := m.server.Serve(m.listener)
|
||||||
if err != http.ErrServerClosed {
|
if err != http.ErrServerClosed {
|
||||||
|
|||||||
41
pprof.go
Normal file
41
pprof.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user