-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
50 lines (42 loc) · 1.19 KB
/
client.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 easyraft
import (
"context"
"errors"
"github.com/ksrichard/easyraft/grpc"
ggrpc "google.golang.org/grpc"
)
func ApplyOnLeader(node *Node, payload []byte) (interface{}, error) {
if node.Raft.Leader() == "" {
return nil, errors.New("unknown leader")
}
var opt ggrpc.DialOption = ggrpc.EmptyDialOption{}
conn, err := ggrpc.Dial(string(node.Raft.Leader()), ggrpc.WithInsecure(), ggrpc.WithBlock(), opt)
if err != nil {
return nil, err
}
defer conn.Close()
client := grpc.NewRaftClient(conn)
response, err := client.ApplyLog(context.Background(), &grpc.ApplyRequest{Request: payload})
if err != nil {
return nil, err
}
result, err := node.Serializer.Deserialize(response.Response)
if err != nil {
return nil, err
}
return result, nil
}
func GetPeerDetails(address string) (*grpc.GetDetailsResponse, error) {
var opt ggrpc.DialOption = ggrpc.EmptyDialOption{}
conn, err := ggrpc.Dial(address, ggrpc.WithInsecure(), ggrpc.WithBlock(), opt)
if err != nil {
return nil, err
}
defer conn.Close()
client := grpc.NewRaftClient(conn)
response, err := client.GetDetails(context.Background(), &grpc.GetDetailsRequest{})
if err != nil {
return nil, err
}
return response, nil
}