-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_apply.go
57 lines (45 loc) · 1.37 KB
/
node_apply.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
package rafting
import (
"context"
"fmt"
"time"
pb "github.com/danielgatis/go-rafting/protobuf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func applyOnLeader(n *Node, payload []byte, timeout time.Duration) (interface{}, error) {
if n.raft.Leader() == "" {
return nil, ErrUnknownleader
}
var opt grpc.DialOption = grpc.EmptyDialOption{}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
conn, err := grpc.DialContext(ctx, string(n.raft.Leader()), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock(), opt)
if err != nil {
return nil, fmt.Errorf(`grpc.Dial(...): %w`, err)
}
defer conn.Close()
client := pb.NewRaftingServiceClient(conn)
response, err := client.Apply(context.Background(), &pb.ApplyRequest{Payload: payload})
if err != nil {
return nil, fmt.Errorf(`client.Apply(...): %w`, err)
}
var result interface{}
err = n.unmarshalFn(response.Payload, &result)
if err != nil {
return nil, fmt.Errorf("n.unmarshalFn(...): %w", err)
}
return result, nil
}
func apply(n *Node, payload []byte, timeout time.Duration) (interface{}, error) {
result := n.raft.Apply(payload, timeout)
if result.Error() != nil {
return nil, result.Error()
}
switch result.Response().(type) {
case error:
return nil, result.Response().(error)
default:
return result.Response(), nil
}
}