compute localIPs only once per server startup() (#19951)

repeatedly calling this function is not necessary,
on systems with lots of interfaces, including virtual
ones can make this reasonably delayed.
This commit is contained in:
Harshavardhana 2024-06-19 07:34:00 -07:00 committed by GitHub
parent ee48f9f206
commit 69e41f87ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 15 deletions

View file

@ -19,7 +19,6 @@ package cmd
import (
"errors"
"fmt"
"net"
"net/url"
"runtime"
@ -32,8 +31,16 @@ import (
xnet "github.com/minio/pkg/v3/net"
)
// IPv4 addresses of local host.
var localIP4 = mustGetLocalIP4()
var (
// IPv4 addresses of localhost.
localIP4 = mustGetLocalIP4()
// IPv6 addresses of localhost.
localIP6 = mustGetLocalIP6()
// List of all local loopback addresses.
localLoopbacks = mustGetLocalLoopbacks()
)
// mustSplitHostPort is a wrapper to net.SplitHostPort() where error is assumed to be a fatal.
func mustSplitHostPort(hostPort string) (host, port string) {
@ -74,6 +81,16 @@ func mustGetLocalIPs() (ipList []net.IP) {
return ipList
}
func mustGetLocalLoopbacks() (ipList set.StringSet) {
ipList = set.NewStringSet()
for _, ip := range mustGetLocalIPs() {
if ip != nil && ip.IsLoopback() {
ipList.Add(ip.String())
}
}
return
}
// mustGetLocalIP4 returns IPv4 addresses of localhost. It panics on error.
func mustGetLocalIP4() (ipList set.StringSet) {
ipList = set.NewStringSet()
@ -160,15 +177,15 @@ func getConsoleEndpoints() (consoleEndpoints []string) {
}
var ipList []string
if globalMinioConsoleHost == "" {
ipList = sortIPs(mustGetLocalIP4().ToSlice())
ipList = append(ipList, mustGetLocalIP6().ToSlice()...)
ipList = sortIPs(localIP4.ToSlice())
ipList = append(ipList, localIP6.ToSlice()...)
} else {
ipList = []string{globalMinioConsoleHost}
}
consoleEndpoints = make([]string, 0, len(ipList))
for _, ip := range ipList {
endpoint := fmt.Sprintf("%s://%s", getURLScheme(globalIsTLS), net.JoinHostPort(ip, globalMinioConsolePort))
consoleEndpoints = append(consoleEndpoints, endpoint)
consoleEndpoints = append(consoleEndpoints, getURLScheme(globalIsTLS)+"://"+net.JoinHostPort(ip, globalMinioConsolePort))
}
return consoleEndpoints
@ -180,15 +197,15 @@ func getAPIEndpoints() (apiEndpoints []string) {
}
var ipList []string
if globalMinioHost == "" {
ipList = sortIPs(mustGetLocalIP4().ToSlice())
ipList = append(ipList, mustGetLocalIP6().ToSlice()...)
ipList = sortIPs(localIP4.ToSlice())
ipList = append(ipList, localIP6.ToSlice()...)
} else {
ipList = []string{globalMinioHost}
}
apiEndpoints = make([]string, 0, len(ipList))
for _, ip := range ipList {
endpoint := fmt.Sprintf("%s://%s", getURLScheme(globalIsTLS), net.JoinHostPort(ip, globalMinioPort))
apiEndpoints = append(apiEndpoints, endpoint)
apiEndpoints = append(apiEndpoints, getURLScheme(globalIsTLS)+"://"+net.JoinHostPort(ip, globalMinioPort))
}
return apiEndpoints

View file

@ -680,10 +680,8 @@ func getServerListenAddrs() []string {
// Use a string set to avoid duplication
addrs := set.NewStringSet()
// Listen on local interface to receive requests from Console
for _, ip := range mustGetLocalIPs() {
if ip != nil && ip.IsLoopback() {
addrs.Add(net.JoinHostPort(ip.String(), globalMinioPort))
}
for _, ip := range localLoopbacks.ToSlice() {
addrs.Add(net.JoinHostPort(ip, globalMinioPort))
}
host, _ := mustSplitHostPort(globalMinioAddr)
if host != "" {