Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

expose protobuf encoding/decoing API

  • Loading branch information...
commit f92654b00ea7218f37f317292333d767cff21bd0 1 parent dbedc98
@xiang90 xiang90 authored
View
4 append_entries_request.go
@@ -31,7 +31,7 @@ func newAppendEntriesRequest(term uint64, prevLogIndex uint64, prevLogTerm uint6
// Encodes the AppendEntriesRequest to a buffer. Returns the number of bytes
// written and any error that may have occurred.
-func (req *AppendEntriesRequest) encode(w io.Writer) (int, error) {
+func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error) {
protoEntries := make([]*protobuf.ProtoAppendEntriesRequest_ProtoLogEntry, len(req.Entries))
@@ -63,7 +63,7 @@ func (req *AppendEntriesRequest) encode(w io.Writer) (int, error) {
// Decodes the AppendEntriesRequest from a buffer. Returns the number of bytes read and
// any error that occurs.
-func (req *AppendEntriesRequest) decode(r io.Reader) (int, error) {
+func (req *AppendEntriesRequest) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
View
6 append_entries_request_test.go
@@ -10,7 +10,7 @@ func BenchmarkAppendEntriesRequestEncoding(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
- req.encode(&buf)
+ req.Encode(&buf)
}
b.SetBytes(int64(len(tmp)))
}
@@ -19,7 +19,7 @@ func BenchmarkAppendEntriesRequestDecoding(b *testing.B) {
req, buf := createTestAppendEntriesRequest(2000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
- req.decode(bytes.NewReader(buf))
+ req.Decode(bytes.NewReader(buf))
}
b.SetBytes(int64(len(buf)))
}
@@ -34,7 +34,7 @@ func createTestAppendEntriesRequest(entryCount int) (*AppendEntriesRequest, []by
req := newAppendEntriesRequest(1, 1, 1, 1, "leader", entries)
var buf bytes.Buffer
- req.encode(&buf)
+ req.Encode(&buf)
return req, buf.Bytes()
}
View
4 append_entries_response.go
@@ -30,7 +30,7 @@ func newAppendEntriesResponse(term uint64, success bool, index uint64, commitInd
// Encodes the AppendEntriesResponse to a buffer. Returns the number of bytes
// written and any error that may have occurred.
-func (resp *AppendEntriesResponse) encode(w io.Writer) (int, error) {
+func (resp *AppendEntriesResponse) Encode(w io.Writer) (int, error) {
pb := &protobuf.ProtoAppendEntriesResponse{
Term: proto.Uint64(resp.Term),
Index: proto.Uint64(resp.Index),
@@ -47,7 +47,7 @@ func (resp *AppendEntriesResponse) encode(w io.Writer) (int, error) {
// Decodes the AppendEntriesResponse from a buffer. Returns the number of bytes read and
// any error that occurs.
-func (resp *AppendEntriesResponse) decode(r io.Reader) (int, error) {
+func (resp *AppendEntriesResponse) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
View
6 append_entries_response_test.go
@@ -10,7 +10,7 @@ func BenchmarkAppendEntriesResponseEncoding(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
- req.encode(&buf)
+ req.Encode(&buf)
}
b.SetBytes(int64(len(tmp)))
}
@@ -19,7 +19,7 @@ func BenchmarkAppendEntriesResponseDecoding(b *testing.B) {
req, buf := createTestAppendEntriesResponse(2000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
- req.decode(bytes.NewReader(buf))
+ req.Decode(bytes.NewReader(buf))
}
b.SetBytes(int64(len(buf)))
}
@@ -28,7 +28,7 @@ func createTestAppendEntriesResponse(entryCount int) (*AppendEntriesResponse, []
resp := newAppendEntriesResponse(1, true, 1, 1)
var buf bytes.Buffer
- resp.encode(&buf)
+ resp.Encode(&buf)
return resp, buf.Bytes()
}
View
16 http_transporter.go
@@ -89,7 +89,7 @@ func (t *HTTPTransporter) Install(server *Server, mux HTTPMuxer) {
// Sends an AppendEntries RPC to a peer.
func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse {
var b bytes.Buffer
- if _, err := req.encode(&b); err != nil {
+ if _, err := req.Encode(&b); err != nil {
traceln("transporter.ae.encoding.error:", err)
return nil
}
@@ -106,7 +106,7 @@ func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, r
defer httpResp.Body.Close()
resp := &AppendEntriesResponse{}
- if _, err = resp.decode(httpResp.Body); err != nil && err != io.EOF {
+ if _, err = resp.Decode(httpResp.Body); err != nil && err != io.EOF {
traceln("transporter.ae.decoding.error:", err)
return nil
}
@@ -117,7 +117,7 @@ func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, r
// Sends a RequestVote RPC to a peer.
func (t *HTTPTransporter) SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse {
var b bytes.Buffer
- if _, err := req.encode(&b); err != nil {
+ if _, err := req.Encode(&b); err != nil {
traceln("transporter.rv.encoding.error:", err)
return nil
}
@@ -134,7 +134,7 @@ func (t *HTTPTransporter) SendVoteRequest(server *Server, peer *Peer, req *Reque
defer httpResp.Body.Close()
resp := &RequestVoteResponse{}
- if _, err = resp.decode(httpResp.Body); err != nil && err != io.EOF {
+ if _, err = resp.Decode(httpResp.Body); err != nil && err != io.EOF {
traceln("transporter.rv.decoding.error:", err)
return nil
}
@@ -162,13 +162,13 @@ func (t *HTTPTransporter) appendEntriesHandler(server *Server) http.HandlerFunc
traceln(server.Name(), "RECV /appendEntries")
req := &AppendEntriesRequest{}
- if _, err := req.decode(r.Body); err != nil {
+ if _, err := req.Decode(r.Body); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
resp := server.AppendEntries(req)
- if _, err := resp.encode(w); err != nil {
+ if _, err := resp.Encode(w); err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
@@ -181,13 +181,13 @@ func (t *HTTPTransporter) requestVoteHandler(server *Server) http.HandlerFunc {
traceln(server.Name(), "RECV /requestVote")
req := &RequestVoteRequest{}
- if _, err := req.decode(r.Body); err != nil {
+ if _, err := req.Decode(r.Body); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
resp := server.RequestVote(req)
- if _, err := resp.encode(w); err != nil {
+ if _, err := resp.Encode(w); err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
View
4 request_vote_request.go
@@ -28,7 +28,7 @@ func newRequestVoteRequest(term uint64, candidateName string, lastLogIndex uint6
// Encodes the RequestVoteRequest to a buffer. Returns the number of bytes
// written and any error that may have occurred.
-func (req *RequestVoteRequest) encode(w io.Writer) (int, error) {
+func (req *RequestVoteRequest) Encode(w io.Writer) (int, error) {
pb := &protobuf.ProtoRequestVoteRequest{
Term: proto.Uint64(req.Term),
LastLogIndex: proto.Uint64(req.LastLogIndex),
@@ -45,7 +45,7 @@ func (req *RequestVoteRequest) encode(w io.Writer) (int, error) {
// Decodes the RequestVoteRequest from a buffer. Returns the number of bytes read and
// any error that occurs.
-func (req *RequestVoteRequest) decode(r io.Reader) (int, error) {
+func (req *RequestVoteRequest) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
View
4 request_vote_response.go
@@ -24,7 +24,7 @@ func newRequestVoteResponse(term uint64, voteGranted bool) *RequestVoteResponse
// Encodes the RequestVoteResponse to a buffer. Returns the number of bytes
// written and any error that may have occurred.
-func (resp *RequestVoteResponse) encode(w io.Writer) (int, error) {
+func (resp *RequestVoteResponse) Encode(w io.Writer) (int, error) {
pb := &protobuf.ProtoRequestVoteResponse{
Term: proto.Uint64(resp.Term),
VoteGranted: proto.Bool(resp.VoteGranted),
@@ -40,7 +40,7 @@ func (resp *RequestVoteResponse) encode(w io.Writer) (int, error) {
// Decodes the RequestVoteResponse from a buffer. Returns the number of bytes read and
// any error that occurs.
-func (resp *RequestVoteResponse) decode(r io.Reader) (int, error) {
+func (resp *RequestVoteResponse) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
Please sign in to comment.
Something went wrong with that request. Please try again.