Skip to content

Commit

Permalink
feat(server): support listener passed by systemd for socket activation
Browse files Browse the repository at this point in the history
This patch adds support for server to be used with systemd's
socket activation by expecting an open file descriptor to be
announced using environment variables.

To make use of this set either HTTPListenNetwork or
GRPCListenNetwork to "sd_listen_fd".
  • Loading branch information
networkException committed Sep 30, 2024
1 parent f52de24 commit e379e75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/armon/go-metrics v0.3.10
github.com/aws/aws-sdk-go v1.44.321
github.com/cespare/xxhash/v2 v2.3.0
github.com/coreos/go-systemd/v22 v22.5.0
github.com/cristalhq/hedgedhttp v0.9.1
github.com/davecgh/go-spew v1.1.1
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb
Expand Down Expand Up @@ -61,7 +62,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
39 changes: 30 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/grafana/dskit/log"
"github.com/grafana/dskit/middleware"
"github.com/grafana/dskit/signals"

"github.com/coreos/go-systemd/v22/activation"
)

// Listen on the named network
Expand All @@ -47,6 +49,8 @@ const (
DefaultNetwork = "tcp"
// NetworkTCPV4 for IPV4 only
NetworkTCPV4 = "tcp4"
// NetworkSystemdListenFd for using a passed open file descriptor
NetworkSystemdListenFd = "sd_listen_fd"
)

// SignalHandler used by Server.
Expand Down Expand Up @@ -248,6 +252,25 @@ func NewWithMetrics(cfg Config, metrics *Metrics) (*Server, error) {
return newServer(cfg, metrics)
}

func listen(network string, address string, port int, systemdListenFiles map[string][]net.Listener) (net.Listener, error) {
if network == "" {
network = DefaultNetwork
}

switch network {
case DefaultNetwork, NetworkTCPV4:
return net.Listen(network, net.JoinHostPort(address, strconv.Itoa(port)))
case NetworkSystemdListenFd:
if len(systemdListenFiles[address]) < 1 {
return nil, fmt.Errorf("expected at least one listener file descriptor named '%s'", address)
}

return systemdListenFiles[address][0], nil
default:
return nil, fmt.Errorf("cannot listen on unknown network '%s'", network)
}
}

func newServer(cfg Config, metrics *Metrics) (*Server, error) {
// If user doesn't supply a logging implementation, by default instantiate go-kit.
logger := cfg.Log
Expand All @@ -260,15 +283,17 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) {
gatherer = prometheus.DefaultGatherer
}

network := cfg.HTTPListenNetwork
if network == "" {
network = DefaultNetwork
systemdListenFiles, err := activation.ListenersWithNames()
if err != nil {
return nil, err
}

// Setup listeners first, so we can fail early if the port is in use.
httpListener, err := net.Listen(network, net.JoinHostPort(cfg.HTTPListenAddress, strconv.Itoa(cfg.HTTPListenPort)))
httpListener, err := listen(cfg.HTTPListenNetwork, cfg.HTTPListenAddress, cfg.HTTPListenPort, systemdListenFiles)
if err != nil {
return nil, err
}

httpListener = middleware.CountingListener(httpListener, metrics.TCPConnections.WithLabelValues("http"))
if cfg.HTTPLogClosedConnectionsWithoutResponse {
httpListener = middleware.NewZeroResponseListener(httpListener, level.Warn(logger))
Expand All @@ -279,11 +304,7 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) {
httpListener = netutil.LimitListener(httpListener, cfg.HTTPConnLimit)
}

network = cfg.GRPCListenNetwork
if network == "" {
network = DefaultNetwork
}
grpcListener, err := net.Listen(network, net.JoinHostPort(cfg.GRPCListenAddress, strconv.Itoa(cfg.GRPCListenPort)))
grpcListener, err := listen(cfg.GRPCListenNetwork, cfg.GRPCListenAddress, cfg.GRPCListenPort, systemdListenFiles)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e379e75

Please sign in to comment.