From 45db5e58df07b9672c024ee67e2b575a6a6f6043 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Sun, 26 Jun 2022 13:56:59 -0400 Subject: [PATCH] allow listening on multiple listeners Signed-off-by: Perry Naseck --- go.mod | 1 + go.sum | 1 + web/tls_config.go | 30 +++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index e2eebb5e..1f621d2f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/common v0.34.0 golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed + golang.org/x/sync v0.0.0-20201207232520-09787c993a3a gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index a2fbd6fb..5d7ca7ac 100644 --- a/go.sum +++ b/go.sum @@ -298,6 +298,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/web/tls_config.go b/web/tls_config.go index 455b7eb1..0719ad96 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -27,6 +27,7 @@ import ( "github.com/go-kit/log/level" "github.com/pkg/errors" config_util "github.com/prometheus/common/config" + "golang.org/x/sync/errgroup" "gopkg.in/yaml.v2" ) @@ -178,6 +179,20 @@ func ConfigToTLSConfig(c *TLSStruct) (*tls.Config, error) { return cfg, nil } +// ListenAndServe starts the server on the given listeners. Based on the file +// tlsConfigPath, TLS or basic auth could be enabled. +func ServeMultiple(listeners []net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { + errs := new(errgroup.Group) + for _, l := range listeners { + l := l + errs.Go(func() error { + level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String()) + return Serve(l, server, tlsConfigPath, logger) + }) + } + return errs.Wait() +} + // ListenAndServe starts the server on the given address. Based on the file // tlsConfigPath, TLS or basic auth could be enabled. func ListenAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { @@ -189,10 +204,10 @@ func ListenAndServe(server *http.Server, tlsConfigPath string, logger log.Logger return Serve(listener, server, tlsConfigPath, logger) } -// UseActivatedSocketAndServe starts the server on the systemd socket activated +// UseActivatedSocketsAndServe starts the server on the systemd socket activated // listener. Based on the file tlsConfigPath, TLS or basic auth could be // enabled. -func UseActivatedSocketAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { +func UseActivatedSocketsAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { listeners, err := activation.Listeners() if err != nil { return err @@ -200,17 +215,14 @@ func UseActivatedSocketAndServe(server *http.Server, tlsConfigPath string, logge if len(listeners) < 1 { return errors.New("No socket activation file descriptors found") } - if len(listeners) > 1 { - return errors.New("More than one socket activation file descriptor found") - } - return Serve(listeners[0], server, tlsConfigPath, logger) + return ServeMultiple(listeners, server, tlsConfigPath, logger) } // Server starts the server on the given listener. Based on the file // tlsConfigPath, TLS or basic auth could be enabled. func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { if tlsConfigPath == "" { - level.Info(logger).Log("msg", "TLS is disabled.", "http2", false) + level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String()) return server.Serve(l) } @@ -243,10 +255,10 @@ func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler)) } // Valid TLS config. - level.Info(logger).Log("msg", "TLS is enabled.", "http2", c.HTTPConfig.HTTP2) + level.Info(logger).Log("msg", "TLS is enabled.", "http2", c.HTTPConfig.HTTP2, "address", l.Addr().String()) case errNoTLSConfig: // No TLS config, back to plain HTTP. - level.Info(logger).Log("msg", "TLS is disabled.", "http2", false) + level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String()) return server.Serve(l) default: // Invalid TLS config.