From 903842484e764a805abebce2e8a2fad718d14211 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 15 Jan 2021 18:56:35 +0100 Subject: [PATCH] apply listenIP to metrics and pprof too --- README.md | 4 ++-- internal/metrics/metrics.go | 10 ++++++++-- internal/pprof/pprof.go | 9 +++++++-- main.go | 15 +++++++++++---- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ce1486be..ad293cc6 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Download and launch the image: docker run --rm -it --network=host aler9/rtsp-simple-server ``` -The `--network=host` flag is mandatory since Docker can change the source port of UDP packets for routing reasons, and this makes impossible finding out the publisher of the packets. This issue can be avoided by disabling UDP and exposing the RTSP port: +The `--network=host` flag is mandatory since Docker can change the source port of UDP packets for routing reasons, and this doesn't allow to find out the publisher of the packets. This issue can be avoided by disabling UDP and exposing the RTSP port: ``` docker run --rm -it -e RTSP_PROTOCOLS=tcp -p 8554:8554 aler9/rtsp-simple-server @@ -131,7 +131,7 @@ There are two ways to change the configuration: RTSP_PATHS_TEST_SOURCE=rtsp://myurl ./rtsp-simple-server ``` - This method is particularly useful when running rtsp-simple-server with Docker; any configuration parameter can be changed by passing environment variables with the `-e` flag: + This method is particularly useful when using Docker; any configuration parameter can be changed by passing environment variables with the `-e` flag: ``` docker run --rm -it --network=host -e RTSP_PATHS_TEST_SOURCE=rtsp://myurl aler9/rtsp-simple-server diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index e5186482..130b8eaa 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -14,7 +14,7 @@ import ( ) const ( - address = ":9998" + port = 9998 ) func formatMetric(key string, value int64, nowUnix int64) string { @@ -37,7 +37,13 @@ type Metrics struct { } // New allocates a metrics. -func New(stats *stats.Stats, parent Parent) (*Metrics, error) { +func New( + listenIP string, + stats *stats.Stats, + parent Parent, +) (*Metrics, error) { + + address := listenIP + ":" + strconv.FormatInt(port, 10) listener, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/pprof/pprof.go b/internal/pprof/pprof.go index 7f923eda..29ee6eba 100644 --- a/internal/pprof/pprof.go +++ b/internal/pprof/pprof.go @@ -4,6 +4,7 @@ import ( "context" "net" "net/http" + "strconv" // start pprof _ "net/http/pprof" @@ -12,7 +13,7 @@ import ( ) const ( - address = ":9999" + port = 9999 ) // Parent is implemented by program. @@ -27,7 +28,11 @@ type Pprof struct { } // New allocates a Pprof. -func New(parent Parent) (*Pprof, error) { +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 diff --git a/main.go b/main.go index a7b50dcf..a56fdebc 100644 --- a/main.go +++ b/main.go @@ -164,7 +164,10 @@ func (p *program) createResources(initial bool) error { if p.conf.Metrics { if p.metrics == nil { - p.metrics, err = metrics.New(p.stats, p) + p.metrics, err = metrics.New( + p.conf.ListenIP, + p.stats, + p) if err != nil { return err } @@ -173,7 +176,9 @@ func (p *program) createResources(initial bool) error { if p.conf.Pprof { if p.pprof == nil { - p.pprof, err = pprof.New(p) + p.pprof, err = pprof.New( + p.conf.ListenIP, + p) if err != nil { return err } @@ -277,13 +282,15 @@ func (p *program) closeResources(newConf *conf.Conf) { closeMetrics := false if newConf == nil || - newConf.Metrics != p.conf.Metrics { + newConf.Metrics != p.conf.Metrics || + newConf.ListenIP != p.conf.ListenIP { closeMetrics = true } closePprof := false if newConf == nil || - newConf.Pprof != p.conf.Pprof { + newConf.Pprof != p.conf.Pprof || + newConf.ListenIP != p.conf.ListenIP { closePprof = true }