ShiningRush commented on a change in pull request #947:
URL: https://github.com/apache/apisix-dashboard/pull/947#discussion_r533945810



##########
File path: api/internal/utils/utils.go
##########
@@ -37,35 +38,50 @@ func init() {
                }
                salt = uint16(i)
        }
-
+       ips, err := getLocalIPs()
+       if err != nil {
+               panic(err)
+       }
        _sf = sonyflake.NewSonyflake(sonyflake.Settings{
                MachineID: func() (u uint16, e error) {
-                       return sumIP(GetOutboundIP()) + salt, nil
+                       return sumIPs(ips) + salt, nil
                },
        })
        if _sf == nil {
                panic("sonyflake init failed")
        }
 }
 
-func sumIP(ip net.IP) uint16 {
+func sumIPs(ips []net.IP) uint16 {
        total := 0
-       for i := range ip {
-               total += int(ip[i])
+       for _, ip := range ips {
+               for i := range ip {
+                       total += int(ip[i])
+               }
        }
        return uint16(total)
 }
 
-// Get preferred outbound ip of this machine
-func GetOutboundIP() net.IP {
-       conn, err := net.Dial("udp", "8.8.8.8:80")
+func getLocalIPs() ([]net.IP, error) {
+       var ips []net.IP
+       addrs, err := net.InterfaceAddrs()
        if err != nil {
-               panic(err)
+               return ips, err
+       }
+       for _, a := range addrs {
+               if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() && 
ipNet.IP.To4() != nil {
+                       ips = append(ips, ipNet.IP)
+               }
        }
-       defer conn.Close()
+       return ips, nil
+}
 
-       localAddr := conn.LocalAddr().(*net.UDPAddr)
-       return localAddr.IP
+func randomIP(xs []net.IP) net.IP {

Review comment:
       So we can remove unused code and test case.
   BTW: this is not a really `random`, because here use the default random 
seed,  so it will always output a certain int sequence.If we need to pick a ip, 
pick the first one would be better, it help to debug program.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to