You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by do...@apache.org on 2025/05/19 07:45:25 UTC

(inlong) branch master updated: [INLONG-11869][SDK]Obtain a valid ip during initialization (#11870)

This is an automated email from the ASF dual-hosted git repository.

dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new f2f9cb5780 [INLONG-11869][SDK]Obtain a valid ip during initialization (#11870)
f2f9cb5780 is described below

commit f2f9cb57802fdbfcf5e5aee9bd961ab35c34b3f1
Author: lvanneo <38...@users.noreply.github.com>
AuthorDate: Mon May 19 15:45:19 2025 +0800

    [INLONG-11869][SDK]Obtain a valid ip during initialization (#11870)
---
 .../dataproxy-sdk-golang/dataproxy/options.go        |  7 ++-----
 .../dataproxy-sdk-golang/util/id.go                  |  2 +-
 .../dataproxy-sdk-golang/util/ip.go                  | 20 ++++++++++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/dataproxy/options.go b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/dataproxy/options.go
index 5d4692f077..985802ed6d 100755
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/dataproxy/options.go
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/dataproxy/options.go
@@ -41,12 +41,9 @@ var (
 
 func init() {
 	var err error
-	localIP, err = util.GetFirstPrivateIP()
+	localIP, err = util.GetOneIP()
 	if err != nil {
-		localIP, err = util.GetFirstIP()
-		if err != nil {
-			localIP = "noIP"
-		}
+		localIP = "noIP"
 	}
 }
 
diff --git a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/id.go b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/id.go
index 268a907da0..443a84548a 100755
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/id.go
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/id.go
@@ -29,7 +29,7 @@ var (
 )
 
 func init() {
-	ip, err := GetFirstPrivateIP()
+	ip, err := GetOneIP()
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/ip.go b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/ip.go
index 2ca85d429f..3128fcd864 100755
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/ip.go
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/util/ip.go
@@ -154,3 +154,23 @@ func GetFirstIP() (string, error) {
 
 	return "", fmt.Errorf("no ip")
 }
+
+// GetOneIP obtain a valid ip address of the current host, with private ip preferred
+func GetOneIP() (string, error) {
+	ips, err := GetIPv4List()
+	if err != nil {
+		return "", fmt.Errorf("failed to obtain ip. %w", err)
+	}
+
+	if len(ips) == 0 {
+		return "", fmt.Errorf("no ip")
+	}
+
+	for _, ip := range ips {
+		if IsPrivateIP(ip) {
+			return ip, nil
+		}
+	}
+
+	return ips[0], nil
+}