Esempio n. 1
0
// Sends an AppendEntries RPC to a peer.
func (t *HTTPTransporter) SendAppendEntriesRequest(server raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) *raft.AppendEntriesResponse {
	var b bytes.Buffer
	if _, err := req.Encode(&b); err != nil {
		//log.Println("transporter.ae.encoding.error:", err)
		return nil
	}

	url := joinPath(peer.ConnectionString, t.AppendEntriesPath())
	//log.Println(server.Name(), "POST", url)

	//oldTimeout := t.Transport.ResponseHeaderTimeout
	t.Transport.ResponseHeaderTimeout = server.ElectionTimeout()
	httpResp, err := t.httpClient.Post(url, "application/protobuf", &b)
	if httpResp == nil || err != nil {
		//log.Println("transporter.ae.response.error:", err)
		return nil
	}
	//t.Transport.ResponseHeaderTimeout = oldTimeout
	defer httpResp.Body.Close()

	resp := &raft.AppendEntriesResponse{}
	if _, err = resp.Decode(httpResp.Body); err != nil && err != io.EOF {
		//log.Println("transporter.ae.decoding.error:", err)
		return nil
	}

	return resp
}