-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_rafting.go
41 lines (33 loc) · 970 Bytes
/
node_rafting.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
package rafting
import (
context "context"
"fmt"
"time"
pb "github.com/danielgatis/go-rafting/protobuf"
)
type raftingServiceServer struct {
pb.UnimplementedRaftingServiceServer
node *Node
}
func newRaftingServerImpl(node *Node) *raftingServiceServer {
return &raftingServiceServer{
node: node,
}
}
func (s *raftingServiceServer) Apply(ctx context.Context, request *pb.ApplyRequest) (*pb.ApplyResponse, error) {
result := s.node.raft.Apply(request.Payload, time.Second)
if result.Error() != nil {
return nil, fmt.Errorf("raft.Apply(...): %w", result.Error())
}
payload, err := s.node.marshalFn(result.Response())
if err != nil {
return nil, fmt.Errorf("node.marshalFn(...): %w", err)
}
return &pb.ApplyResponse{Payload: payload}, nil
}
func (s *raftingServiceServer) GetDetails(context.Context, *pb.GetDetailsRequest) (*pb.GetDetailsResponse, error) {
return &pb.GetDetailsResponse{
Id: s.node.id,
Port: int32(s.node.port),
}, nil
}