-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
64 lines (54 loc) · 1.41 KB
/
util.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package proton
import (
"errors"
"time"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
)
const (
// MaxRetryTime is the number of time we try to initiate
// a grpc connection to a remote raft member
MaxRetryTime = 3
)
// Raft represents a connection to a raft member
type Raft struct {
RaftClient
Conn *grpc.ClientConn
}
// GetRaftClient returns a raft client object to communicate
// with other raft members
func GetRaftClient(addr string, timeout time.Duration) (*Raft, error) {
conn, err := getClientConn(addr, "tcp", timeout)
if err != nil {
return nil, err
}
return &Raft{
RaftClient: NewRaftClient(conn),
Conn: conn,
}, nil
}
// getClientConn returns a grpc client connection
func getClientConn(addr string, protocol string, timeout time.Duration) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithTimeout(timeout))
if err != nil {
return nil, err
}
return conn, nil
}
// EncodePair returns a protobuf encoded key/value pair to be sent through raft
func EncodePair(key string, value []byte) ([]byte, error) {
k := proto.String(key)
pair := &Pair{
Key: *k,
Value: value,
}
data, err := proto.Marshal(pair)
if err != nil {
return nil, errors.New("Can't encode key/value using protobuf")
}
return data, nil
}
// Register registers the node raft server
func Register(server *grpc.Server, node *Node) {
RegisterRaftServer(server, node)
}