etcdhttp: non-blocking sender

This commit is contained in:
Xiang Li 2014-09-05 13:53:49 -07:00
parent 769c043537
commit cbec48e8f6

View File

@ -73,16 +73,23 @@ const DefaultTimeout = 500 * time.Millisecond
func Sender(p Peers) func(msgs []raftpb.Message) { func Sender(p Peers) func(msgs []raftpb.Message) {
return func(msgs []raftpb.Message) { return func(msgs []raftpb.Message) {
for _, m := range msgs { for _, m := range msgs {
// TODO: create workers that deal with message sending // TODO: reuse go routines
// concurrently as to not block progress // limit the number of outgoing connections for the same receiver
for { go send(p, m)
}
}
}
func send(p Peers, m raftpb.Message) {
// TODO (xiangli): reasonable retry logic
for i := 0; i < 3; i++ {
url := p.Pick(m.To) url := p.Pick(m.To)
if url == "" { if url == "" {
// 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.
log.Println("etcdhttp: no addr for %d", m.To) log.Println("etcdhttp: no addr for %d", m.To)
break return
} }
url += "/raft" url += "/raft"
@ -92,16 +99,13 @@ func Sender(p Peers) func(msgs []raftpb.Message) {
data, err := m.Marshal() data, err := m.Marshal()
if err != nil { if err != nil {
log.Println("etcdhttp: dropping message:", err) log.Println("etcdhttp: dropping message:", err)
break // drop bad message return // drop bad message
} }
if httpPost(url, data) { if httpPost(url, data) {
break // success return // success
} }
// TODO: backoff // TODO: backoff
} }
}
}
} }
func httpPost(url string, data []byte) bool { func httpPost(url string, data []byte) bool {
@ -111,6 +115,7 @@ func httpPost(url string, data []byte) bool {
elog.TODO() elog.TODO()
return false return false
} }
resp.Body.Close()
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
elog.TODO() elog.TODO()
return false return false