-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient_services.go
36 lines (31 loc) · 912 Bytes
/
client_services.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
package easyraft
import (
"context"
rgrpc "github.com/ksrichard/easyraft/grpc"
)
func NewClientGrpcService(node *Node) *ClientGrpcServices {
return &ClientGrpcServices{
Node: node,
}
}
type ClientGrpcServices struct {
Node *Node
rgrpc.UnimplementedRaftServer
}
func (s *ClientGrpcServices) ApplyLog(ctx context.Context, request *rgrpc.ApplyRequest) (*rgrpc.ApplyResponse, error) {
result := s.Node.Raft.Apply(request.GetRequest(), 0)
if result.Error() != nil {
return nil, result.Error()
}
respPayload, err := s.Node.Serializer.Serialize(result.Response())
if err != nil {
return nil, err
}
return &rgrpc.ApplyResponse{Response: respPayload}, nil
}
func (s *ClientGrpcServices) GetDetails(context.Context, *rgrpc.GetDetailsRequest) (*rgrpc.GetDetailsResponse, error) {
return &rgrpc.GetDetailsResponse{
ServerId: s.Node.ID,
DiscoveryPort: int32(s.Node.DiscoveryPort),
}, nil
}