Skip to content

Commit

Permalink
core: Support Unix domain socket HTTP server listener
Browse files Browse the repository at this point in the history
  • Loading branch information
LINKIWI committed Feb 3, 2024
1 parent 4a05b20 commit 7d0db48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions core/internal/helpers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func ValidateHostList(hosts []string) bool {
// ValidateHostPort returns true if the provided string is of the form "hostname:port", where hostname is a valid
// hostname or IP address (as parsed by ValidateIP or ValidateHostname), and port is a valid integer.
func ValidateHostPort(host string, allowBlankHost bool) bool {
if strings.HasPrefix(host, "unix:") {
return true
}

// Must be hostname:port, ipv4:port, or [ipv6]:port. Optionally allow blank hostname
hostname, portString, err := net.SplitHostPort(host)
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions core/internal/httpserver/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ func (hc *Coordinator) Start() error {
listeners := make(map[string]net.Listener)

for name, server := range hc.servers {
ln, err := net.Listen("tcp", hc.servers[name].Addr)
network := "tcp"
if strings.HasPrefix(hc.servers[name].Addr, "unix:") {
network = "unix"
hc.servers[name].Addr = hc.servers[name].Addr[len("unix:"):]
}

ln, err := net.Listen(network, hc.servers[name].Addr)
if err != nil {
hc.Log.Error("failed to listen", zap.String("listener", hc.servers[name].Addr), zap.Error(err))
for _, listenerToClose := range listeners {
Expand All @@ -184,10 +190,14 @@ func (hc *Coordinator) Start() error {
}
return err
}

hc.Log.Info("started listener", zap.String("listener", ln.Addr().String()))
listeners[name] = tcpKeepAliveListener{
Keepalive: server.IdleTimeout,
TCPListener: ln.(*net.TCPListener),
listeners[name] = ln
if network == "tcp" {
listeners[name] = tcpKeepAliveListener{
Keepalive: server.IdleTimeout,
TCPListener: ln.(*net.TCPListener),
}
}
}

Expand Down

0 comments on commit 7d0db48

Please sign in to comment.