Skip to content

Commit

Permalink
util: FormatServers
Browse files Browse the repository at this point in the history
  • Loading branch information
pmazzini authored and nemith committed Jun 21, 2020
1 parent da5f835 commit 50daf81
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
11 changes: 1 addition & 10 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fmt"
"io"
"net"
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -180,15 +179,7 @@ func Connect(servers []string, sessionTimeout time.Duration, options ...connOpti
return nil, nil, errors.New("zk: server list must not be empty")
}

srvs := make([]string, len(servers))

for i, addr := range servers {
if strings.Contains(addr, ":") {
srvs[i] = addr
} else {
srvs[i] = addr + ":" + strconv.Itoa(DefaultPort)
}
}
srvs := FormatServers(servers)

// Randomize the order of the servers to avoid creating hotspots
stringShuffle(srvs)
Expand Down
11 changes: 7 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ func DigestACL(perms int32, user, password string) []ACL {
// that resembles <addr>:<port>. If the server has no port provided, the
// DefaultPort constant is added to the end.
func FormatServers(servers []string) []string {
for i := range servers {
if !strings.Contains(servers[i], ":") {
servers[i] = servers[i] + ":" + strconv.Itoa(DefaultPort)
srvs := make([]string, len(servers))
for i, addr := range servers {
if strings.Contains(addr, ":") {
srvs[i] = addr
} else {
srvs[i] = addr + ":" + strconv.Itoa(DefaultPort)
}
}
return servers
return srvs
}

// stringShuffle performs a Fisher-Yates shuffle on a slice of strings
Expand Down

0 comments on commit 50daf81

Please sign in to comment.