From 69e41f87ef603d05e0c98675f29d409d19900f5a Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 19 Jun 2024 07:34:00 -0700 Subject: [PATCH] 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. --- cmd/net.go | 39 ++++++++++++++++++++++++++++----------- cmd/server-main.go | 6 ++---- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/cmd/net.go b/cmd/net.go index 2181cbaeb..082f2fb42 100644 --- a/cmd/net.go +++ b/cmd/net.go @@ -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 diff --git a/cmd/server-main.go b/cmd/server-main.go index b8c5808f7..075c9c482 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -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 != "" {