From dbed8117ba0b58530c110a28507ef8ccc713329f Mon Sep 17 00:00:00 2001 From: Stefan Engstrom Date: Wed, 10 Jan 2018 12:52:25 -0600 Subject: [PATCH 1/3] fixed logic when searching interfaces, priority is now: 172.16/12, 10/8, 192.168/16 --- my_address.go | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/my_address.go b/my_address.go index 943fa72..20739bb 100644 --- a/my_address.go +++ b/my_address.go @@ -48,27 +48,26 @@ func GetMyAddress() (string, error) { continue } + // We looked for an interface name and we found it addrs, err := iface.Addrs() if err != nil { return "", err } - for _, addr := range addrs { - // bit kludgey to go via the CIDR but see no other way - cidr := addr.String() - ip, _, err := net.ParseCIDR(cidr) - if err != nil { - return "", err - } - // don't report loopback or ipv6 addresses - if !ip.IsLoopback() && ip.To4() != nil { - switch { - case block192.Contains(ip): - return ip.String(), nil - case block172.Contains(ip): - return ip.String(), nil - case block10.Contains(ip): - return ip.String(), nil + // Look for interfaces in a list of prioritized netblocks + for _,bl := range []*net.IPNet{block172,block10,block192} { + for _, addr := range addrs { + // bit kludgy to go via the CIDR but see no other way + cidr := addr.String() + ip, _, err := net.ParseCIDR(cidr) + if err != nil { + return "", err + } + // don't report loopback or ipv6 addresses + if !ip.IsLoopback() && ip.To4() != nil { + if bl.Contains(ip) { + return ip.String(),nil + } } } } From 006688e7b534415c934e02bf909cf88d005999c6 Mon Sep 17 00:00:00 2001 From: Daniel Paul Carbone Date: Wed, 10 Jan 2018 16:05:53 -0600 Subject: [PATCH 2/3] Removing renstrom/shortuuid and satori/go.uuid as dependencies. --- Gopkg.toml | 5 +---- candidate.go | 4 +--- client.go | 3 +-- glide.yaml | 4 +--- util.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 util.go diff --git a/Gopkg.toml b/Gopkg.toml index 28e2ccd..029363f 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -3,7 +3,4 @@ version = ">=v1.0.1" [[constraint]] name = "github.com/stretchr/testify" - version = "v1.1.*" -[[constraint]] - name = "github.com/renstrom/shortuuid" - version = "v2.0.*" + version = "v1.1.*" \ No newline at end of file diff --git a/candidate.go b/candidate.go index a9826d9..b25c444 100644 --- a/candidate.go +++ b/candidate.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "github.com/hashicorp/consul/api" - "github.com/renstrom/shortuuid" "math/rand" "net" "regexp" @@ -76,7 +75,7 @@ func NewCandidate(client *Client, id, key, ttl string) (*Candidate, error) { // begin session entry construction c.sessionEntry = &api.SessionEntry{ - Name: fmt.Sprintf("leader-%s-%s-%s", id, c.client.MyNode(), shortuuid.New()), + Name: fmt.Sprintf("leader-%s-%s-%s", id, c.client.MyNode(), randstr(12)), Behavior: api.SessionBehaviorDelete, } @@ -541,7 +540,6 @@ type CandidateSessionParts struct { // ParseCandidateSessionName is provided so you don't have to parse it yourself :) func ParseCandidateSessionName(name string) (*CandidateSessionParts, error) { - // fmt.Sprintf("leader-%s-%s-%s", id, c.client.MyNode(), shortuuid.New()), split := strings.Split(name, "-") if 4 != len(split) { return nil, fmt.Errorf("expected four parts in session name \"%s\", saw only \"%d\"", name, len(split)) diff --git a/client.go b/client.go index 8d111ee..e89cb32 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "github.com/hashicorp/consul/api" - "github.com/renstrom/shortuuid" "math/rand" "net/url" "os" @@ -308,7 +307,7 @@ func (c *Client) SimpleServiceRegister(reg *SimpleServiceRegistration) (string, // Form a unique service id var tail string if reg.RandomID { - tail = shortuuid.New() + tail = randstr(12) } else { tail = strings.ToLower(c.myHost) } diff --git a/glide.yaml b/glide.yaml index 8ff1208..fa5df4d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -3,6 +3,4 @@ import: - package: github.com/hashicorp/consul version: ">=v1.0.1" - package: github.com/stretchr/testify - version: v1.1.* - - package: "github.com/renstrom/shortuuid" - version: "v2.0.*" \ No newline at end of file + version: v1.1.* \ No newline at end of file diff --git a/util.go b/util.go new file mode 100644 index 0000000..358d3ed --- /dev/null +++ b/util.go @@ -0,0 +1,31 @@ +package consultant + +import ( + "math/rand" +) + +const ( + rnb = "0123456789" + rlb = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +) + +var ( + rnbl = int64(len(rnb)) + rlbl = int64(len(rlb)) +) + +func randstr(n int) string { + if n <= 0 { + n = 12 + } + buff := make([]byte, n) + for i := 0; i < n; i++ { + switch rand.Intn(1) { + case 0: + buff[i] = rnb[rand.Int63()%rnbl] + default: + buff[i] = rlb[rand.Int63()&rlbl] + } + } + return string(buff) +} From 8076056a7474757dc306b8cb432f91c94e5636f2 Mon Sep 17 00:00:00 2001 From: Daniel Paul Carbone Date: Thu, 11 Jan 2018 11:06:13 -0600 Subject: [PATCH 3/3] fat fingered that one... --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 358d3ed..cfa8e99 100644 --- a/util.go +++ b/util.go @@ -24,7 +24,7 @@ func randstr(n int) string { case 0: buff[i] = rnb[rand.Int63()%rnbl] default: - buff[i] = rlb[rand.Int63()&rlbl] + buff[i] = rlb[rand.Int63()%rlbl] } } return string(buff)