Skip to content

Commit

Permalink
Merge pull request #9 from myENA/feature/interface-priority
Browse files Browse the repository at this point in the history
fixed logic when searching interfaces, priority is now: 172.16/12, 10…
  • Loading branch information
dcarbone committed Jan 11, 2018
2 parents f39cbcd + 8076056 commit 4000c28
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
5 changes: 1 addition & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
4 changes: 1 addition & 3 deletions candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/renstrom/shortuuid"
"math/rand"
"net"
"regexp"
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/renstrom/shortuuid"
"math/rand"
"net/url"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 1 addition & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
version: v1.1.*
31 changes: 15 additions & 16 deletions my_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4000c28

Please sign in to comment.