mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdserver: Peers.Pick returns just an addr
This commit is contained in:
parent
27813599a1
commit
fb7968d704
@ -23,14 +23,14 @@ func addScheme(addr string) string {
|
||||
return fmt.Sprintf("http://%s", addr)
|
||||
}
|
||||
|
||||
// Pick chooses a random address from a given Peer's addresses, and returns it as
|
||||
// an addressible URI. If the given peer does not exist, an empty string is returned.
|
||||
// Pick returns a random address from a given Peer's addresses. If the
|
||||
// given peer does not exist, an empty string is returned.
|
||||
func (ps Peers) Pick(id int64) string {
|
||||
addrs := ps[id]
|
||||
if len(addrs) == 0 {
|
||||
return ""
|
||||
}
|
||||
return addScheme(addrs[rand.Intn(len(addrs))])
|
||||
return addrs[rand.Intn(len(addrs))]
|
||||
}
|
||||
|
||||
// Set parses command line sets of names to IPs formatted like:
|
||||
@ -99,8 +99,8 @@ func Sender(t *http.Transport, p Peers) func(msgs []raftpb.Message) {
|
||||
func send(c *http.Client, p Peers, m raftpb.Message) {
|
||||
// TODO (xiangli): reasonable retry logic
|
||||
for i := 0; i < 3; i++ {
|
||||
url := p.Pick(m.To)
|
||||
if url == "" {
|
||||
addr := p.Pick(m.To)
|
||||
if addr == "" {
|
||||
// TODO: unknown peer id.. what do we do? I
|
||||
// don't think his should ever happen, need to
|
||||
// look into this further.
|
||||
@ -108,7 +108,7 @@ func send(c *http.Client, p Peers, m raftpb.Message) {
|
||||
return
|
||||
}
|
||||
|
||||
url += raftPrefix
|
||||
url := fmt.Sprintf("http://%s%s", addr, raftPrefix)
|
||||
|
||||
// TODO: don't block. we should be able to have 1000s
|
||||
// of messages out at a time.
|
||||
|
@ -95,13 +95,13 @@ func TestPeersPick(t *testing.T) {
|
||||
3: []string{},
|
||||
}
|
||||
ids := map[string]bool{
|
||||
"http://abc": true,
|
||||
"http://def": true,
|
||||
"http://ghi": true,
|
||||
"http://jkl": true,
|
||||
"http://mno": true,
|
||||
"http://pqr": true,
|
||||
"http://stu": true,
|
||||
"abc": true,
|
||||
"def": true,
|
||||
"ghi": true,
|
||||
"jkl": true,
|
||||
"mno": true,
|
||||
"pqr": true,
|
||||
"stu": true,
|
||||
}
|
||||
for i := 0; i < 1000; i++ {
|
||||
a := ps.Pick(1)
|
||||
@ -110,8 +110,8 @@ func TestPeersPick(t *testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if b := ps.Pick(2); b != "http://xyz" {
|
||||
t.Errorf("id=%q, want %q", b, "http://xyz")
|
||||
if b := ps.Pick(2); b != "xyz" {
|
||||
t.Errorf("id=%q, want %q", b, "xyz")
|
||||
}
|
||||
if c := ps.Pick(3); c != "" {
|
||||
t.Errorf("id=%q, want \"\"", c)
|
||||
|
Loading…
x
Reference in New Issue
Block a user