Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UNIX socket support for gRPC and HTTP listeners #511

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
* `gate_duration_seconds`
* `kv_request_duration_seconds`
* `operation_duration_seconds`
* [ENHANCEMENT] Implement UNIX socket support for gRPC and HTTP listeners. #511
* [BUGFIX] spanlogger: Support multiple tenant IDs. #59
* [BUGFIX] Memberlist: fixed corrupted packets when sending compound messages with more than 255 messages or messages bigger than 64KB. #85
* [BUGFIX] Ring: `ring_member_ownership_percent` and `ring_tokens_owned` metrics are not updated on scale down. #109
Expand Down
22 changes: 20 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
DefaultNetwork = "tcp"
// NetworkTCPV4 for IPV4 only
NetworkTCPV4 = "tcp4"
// NetworkUnix for UNIX sockets
NetworkUnix = "unix"
)

// SignalHandler used by Server.
Expand Down Expand Up @@ -263,8 +265,16 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) {
if network == "" {
network = DefaultNetwork
}

// Generate a listen address depending on the configured network.
httpListenAddr := net.JoinHostPort(cfg.HTTPListenAddress, strconv.Itoa(cfg.HTTPListenPort))
if network == "unix" {
// If we're using unix sockets instead of a TCP socket, don't set a port.
httpListenAddr = cfg.HTTPListenAddress
}

// 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 := net.Listen(network, httpListenAddr)
if err != nil {
return nil, err
}
Expand All @@ -282,7 +292,15 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) {
if network == "" {
network = DefaultNetwork
}
grpcListener, err := net.Listen(network, net.JoinHostPort(cfg.GRPCListenAddress, strconv.Itoa(cfg.GRPCListenPort)))

// Generate a listen address depending on the configured network.
grpcListenAddr := net.JoinHostPort(cfg.GRPCListenAddress, strconv.Itoa(cfg.GRPCListenPort))
if network == "unix" {
// If we're using unix sockets instead of a TCP socket, don't set a port.
grpcListenAddr = cfg.GRPCListenAddress
}

grpcListener, err := net.Listen(network, grpcListenAddr)
if err != nil {
return nil, err
}
Expand Down
47 changes: 47 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,53 @@ func TestTCPv4Network(t *testing.T) {
})
}

func TestUnixNetwork(t *testing.T) {
testSockDir, err := os.MkdirTemp("", "sock")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(testSockDir))
})

var cfg Config
setAutoAssignedPorts(NetworkUnix, &cfg)
cfg.HTTPListenAddress = filepath.Join(testSockDir, "http.sock")
cfg.GRPCListenAddress = filepath.Join(testSockDir, "grpc.sock")

t.Run("unix_http", func(t *testing.T) {
var level log.Level
require.NoError(t, level.Set("info"))
cfg.LogLevel = level
cfg.MetricsNamespace = "testing_http_unix"
srv, err := New(cfg)
require.NoError(t, err)

errChan := make(chan error, 1)
go func() {
errChan <- srv.Run()
}()

require.NoError(t, srv.httpListener.Close())
require.NotNil(t, <-errChan)

// So that address is freed for further tests.
srv.GRPC.Stop()
})

t.Run("unix_http", func(t *testing.T) {
cfg.MetricsNamespace = "testing_grpc_unix"
srv, err := New(cfg)
require.NoError(t, err)

errChan := make(chan error, 1)
go func() {
errChan <- srv.Run()
}()

require.NoError(t, srv.grpcListener.Close())
require.NotNil(t, <-errChan)
})
}

// Ensure that http and grpc servers work with no overrides to config
// (except http port because an ordinary user can't bind to default port 80)
func TestDefaultAddresses(t *testing.T) {
Expand Down