Skip to content

Commit

Permalink
allow listening on multiple listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAwesomeP committed Jun 26, 2022
1 parent 07c7e71 commit 785aea2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -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=
Expand Down
30 changes: 21 additions & 9 deletions web/tls_config.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -189,28 +204,25 @@ 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
}
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)
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 785aea2

Please sign in to comment.