-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
220 lines (178 loc) · 4.98 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
//"google.golang.org/grpc/encoding/gzip"
pb "github.com/PacktPublishing/gRPC-Go-for-Professionals/proto/todo/v2"
)
// addTask calls the AddTask unary endpoint with a AddTaskRequest
// generated from description and dueDate.
func addTask(c pb.TodoServiceClient, description string, dueDate time.Time) uint64 {
req := &pb.AddTaskRequest{
Description: description,
DueDate: timestamppb.New(dueDate),
}
res, err := c.AddTask(context.Background(), req /*, grpc.UseCompressor(gzip.Name)*/)
if err != nil {
if s, ok := status.FromError(err); ok {
switch s.Code() {
case codes.InvalidArgument, codes.Internal:
log.Fatalf("%s: %s", s.Code(), s.Message())
default:
log.Fatal(s)
}
} else {
panic(err)
}
}
fmt.Printf("added task: %d\n", res.Id)
return res.Id
}
// printTasks calls the ListTasks server streaming endpoint
// and displays the Tasks on stdout.
func printTasks(c pb.TodoServiceClient, fm *fieldmaskpb.FieldMask) {
// ctx, cancel := context.WithCancel(context.Background())
// ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
// defer cancel()
ctx := context.Background()
req := &pb.ListTasksRequest{
Mask: fm,
}
stream, err := c.ListTasks(ctx, req)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
for {
res, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
// if res.Overdue {
// log.Printf("CANCEL called")
// cancel()
// }
fmt.Println(res.Task.String(), "overdue: ", res.Overdue)
}
}
// updateTasks calls the UpdateTasks client streaming endpoint
// with the provided reqs.
func updateTasks(c pb.TodoServiceClient, reqs ...*pb.UpdateTasksRequest) {
ctx := context.Background()
stream, err := c.UpdateTasks(ctx)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
for _, req := range reqs {
if err := stream.Send(req); err != nil {
log.Fatalf("unexpected error: %v", err)
}
if req != nil {
fmt.Printf("updated task with id: %d\n", req.Id)
}
}
if _, err = stream.CloseAndRecv(); err != nil {
log.Fatalf("unexpected error: %v", err)
}
}
// deleteTasks calls the DeleteTasks bidi streaming endpoint
// with the provided reqs.
func deleteTasks(c pb.TodoServiceClient, reqs ...*pb.DeleteTasksRequest) {
stream, err := c.DeleteTasks(context.Background())
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
waitc := make(chan struct{})
go func() {
for {
_, err := stream.Recv()
if err == io.EOF {
close(waitc)
break
}
if err != nil {
log.Fatalf("error while receiving: %v\n", err)
}
log.Println("deleted tasks")
}
}()
for _, req := range reqs {
if err := stream.Send(req); err != nil {
return
}
}
if err := stream.CloseSend(); err != nil {
return
}
<-waitc
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
log.Fatalln("usage: client [IP_ADDR]")
}
addr := args[0]
creds, err := credentials.NewClientTLSFromFile("./certs/ca_cert.pem", "x.test.example.com")
if err != nil {
log.Fatalf("failed to load credentials: %v", err)
}
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
//grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(unaryAuthInterceptor),
grpc.WithStreamInterceptor(streamAuthInterceptor),
//grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)),
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
}
conn, err := grpc.Dial(addr, opts...)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer func(conn *grpc.ClientConn) {
if err := conn.Close(); err != nil {
log.Fatalf("unexpected error: %v", err)
}
}(conn)
c := pb.NewTodoServiceClient(conn)
fmt.Println("--------ADD--------")
dueDate := time.Now().Add(5 * time.Second)
id1 := addTask(c, "This is a task", dueDate)
id2 := addTask(c, "This is another task", dueDate)
id3 := addTask(c, "And yet another task", dueDate)
fmt.Println("-------------------")
fmt.Println("--------LIST-------")
printTasks(c, nil)
fmt.Println("-------------------")
fmt.Println("-------UPDATE------")
updateTasks(c, []*pb.UpdateTasksRequest{
{Id: id1, Description: "A better name for the task"},
{Id: id2, DueDate: timestamppb.New(dueDate.Add(5 * time.Hour))},
{Id: id3, Done: true},
}...)
printTasks(c, nil)
fmt.Println("-------------------")
fmt.Println("-------DELETE------")
deleteTasks(c, []*pb.DeleteTasksRequest{
{Id: id1},
{Id: id2},
{Id: id3},
}...)
printTasks(c, nil)
fmt.Println("-------------------")
fmt.Println("-------ERROR-------")
// addTask(c, "", dueDate)
// addTask(c, "not empty", time.Now().Add(-5*time.Second))
fmt.Println("-------------------")
}