-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.go
50 lines (43 loc) · 1.11 KB
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package raft
import (
"context"
"fmt"
"github.com/Mathew-Estafanous/raft/pb"
"google.golang.org/grpc"
"log"
)
func (r *Raft) sendRPC(req interface{}, target Node) rpcResp {
conn, err := grpc.Dial(target.Addr, grpc.WithInsecure())
if err != nil {
return toRPCResponse(nil, err)
}
defer func() {
if err = conn.Close(); err != nil {
r.logger.Print("Encountered an issue when closing connection: %v", err)
}
}()
c := pb.NewRaftClient(conn)
var res interface{}
switch req := req.(type) {
case *pb.VoteRequest:
res, err = c.RequestVote(context.Background(), req)
case *pb.AppendEntriesRequest:
res, err = c.AppendEntry(context.Background(), req)
default:
log.Fatalf("[BUG] Could not determine RPC request of %v", req)
}
return toRPCResponse(res, err)
}
func (r *Raft) handleRPC(req interface{}) rpcResp {
var rpcErr error
var resp interface{}
switch req := req.(type) {
case *pb.VoteRequest:
resp = r.onRequestVote(req)
case *pb.AppendEntriesRequest:
resp = r.onAppendEntry(req)
default:
rpcErr = fmt.Errorf("unable to response to rpcResp request")
}
return toRPCResponse(resp, rpcErr)
}