Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: snowflake remove network dependency #947

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ github.com/shiningrush/droplet v0.2.1 h1:p2utttTbCfgiL+x0Zrb2jFeWspB7/o+v3e+R94G
github.com/shiningrush/droplet v0.2.1/go.mod h1:akW2vIeamvMD6zj6wIBfzYn6StGXBxwlW3gA+hcHu5M=
github.com/shiningrush/droplet v0.2.2 h1:jEqSGoJXlybt1yQdauu+waE+l7KYlguNs8VayMfQ96Q=
github.com/shiningrush/droplet v0.2.2/go.mod h1:akW2vIeamvMD6zj6wIBfzYn6StGXBxwlW3gA+hcHu5M=
github.com/shiningrush/droplet v0.2.3/go.mod h1:akW2vIeamvMD6zj6wIBfzYn6StGXBxwlW3gA+hcHu5M=
github.com/shiningrush/droplet/wrapper/gin v0.2.0 h1:LHkU+TbSkpePgXrTg3hJoSZlCMS03GeWMl0t+oLkd44=
github.com/shiningrush/droplet/wrapper/gin v0.2.0/go.mod h1:ZJu+sCRrVXn5Pg618c1KK3Ob2UiXGuPM1ROx5uMM9YQ=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
Expand Down
40 changes: 28 additions & 12 deletions api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package utils

import (
"fmt"
"math/rand"
"net"
"os"
"strconv"
Expand All @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

if xs != nil && len(xs) > 0 {
return xs[rand.Intn(len(xs))]
} else {
return nil
}
}

func GetFlakeUid() uint64 {
Expand Down
22 changes: 21 additions & 1 deletion api/internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package utils

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetFlakeUid(t *testing.T) {
Expand All @@ -29,4 +30,23 @@ func TestGetFlakeUid(t *testing.T) {
func TestGetFlakeUidStr(t *testing.T) {
id := GetFlakeUidStr()
assert.NotEqual(t, "", id)
assert.Equal(t, 18, len(id))
}

func TestGetLocalIPs(t *testing.T) {
_, err := getLocalIPs()
assert.Equal(t, nil, err)
}

func TestSumIPs_with_nil(t *testing.T) {
total := sumIPs(nil)
assert.Equal(t, uint16(0), total)
}

func TestRandomIP(t *testing.T) {
ips, err := getLocalIPs()
assert.Equal(t, nil, err)
ip := randomIP(ips)
t.Log(ip)
assert.Contains(t, ips, ip)
}