etcdserver: Peers.Pick returns just an addr

This commit is contained in:
Brian Waldon 2014-09-23 10:09:34 -07:00
parent 27813599a1
commit fb7968d704
2 changed files with 15 additions and 15 deletions

View File

@ -23,14 +23,14 @@ func addScheme(addr string) string {
return fmt.Sprintf("http://%s", addr) return fmt.Sprintf("http://%s", addr)
} }
// Pick chooses a random address from a given Peer's addresses, and returns it as // Pick returns a random address from a given Peer's addresses. If the
// an addressible URI. If the given peer does not exist, an empty string is returned. // given peer does not exist, an empty string is returned.
func (ps Peers) Pick(id int64) string { func (ps Peers) Pick(id int64) string {
addrs := ps[id] addrs := ps[id]
if len(addrs) == 0 { if len(addrs) == 0 {
return "" 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: // 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) { func send(c *http.Client, p Peers, m raftpb.Message) {
// TODO (xiangli): reasonable retry logic // TODO (xiangli): reasonable retry logic
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
url := p.Pick(m.To) addr := p.Pick(m.To)
if url == "" { if addr == "" {
// TODO: unknown peer id.. what do we do? I // TODO: unknown peer id.. what do we do? I
// don't think his should ever happen, need to // don't think his should ever happen, need to
// look into this further. // look into this further.
@ -108,7 +108,7 @@ func send(c *http.Client, p Peers, m raftpb.Message) {
return return
} }
url += raftPrefix url := fmt.Sprintf("http://%s%s", addr, raftPrefix)
// TODO: don't block. we should be able to have 1000s // TODO: don't block. we should be able to have 1000s
// of messages out at a time. // of messages out at a time.

View File

@ -95,13 +95,13 @@ func TestPeersPick(t *testing.T) {
3: []string{}, 3: []string{},
} }
ids := map[string]bool{ ids := map[string]bool{
"http://abc": true, "abc": true,
"http://def": true, "def": true,
"http://ghi": true, "ghi": true,
"http://jkl": true, "jkl": true,
"http://mno": true, "mno": true,
"http://pqr": true, "pqr": true,
"http://stu": true, "stu": true,
} }
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
a := ps.Pick(1) a := ps.Pick(1)
@ -110,8 +110,8 @@ func TestPeersPick(t *testing.T) {
break break
} }
} }
if b := ps.Pick(2); b != "http://xyz" { if b := ps.Pick(2); b != "xyz" {
t.Errorf("id=%q, want %q", b, "http://xyz") t.Errorf("id=%q, want %q", b, "xyz")
} }
if c := ps.Pick(3); c != "" { if c := ps.Pick(3); c != "" {
t.Errorf("id=%q, want \"\"", c) t.Errorf("id=%q, want \"\"", c)