/
hw_simple.go
132 lines (116 loc) · 3.04 KB
/
hw_simple.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
package server
import (
"context"
"log"
"net"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/t10471/go-examples/grpc-examples/go/proto"
)
func HWSimpleMain(myAddress, otherAddress string, unwrapError bool) error {
zapLogger, err := zap.NewDevelopment()
if err != nil {
log.Printf("failed to zap.NewDevelopment: %v", err)
return err
}
lis, err := net.Listen("tcp", myAddress)
if err != nil {
log.Printf("failed to listen: %v", err)
return err
}
s := grpc.NewServer(
grpc.UnaryInterceptor(grpc_zap.UnaryServerInterceptor(zapLogger)),
)
pb.RegisterGreeterServer(s, &hwSimple{otherAddress: otherAddress, unwrapError: unwrapError})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Printf("failed to serve: %v", err)
return err
}
return nil
}
type hwSimple struct {
pb.UnimplementedGreeterServer
otherAddress string
unwrapError bool
}
func (s *hwSimple) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
msg, err := s.callOther(ctx, in.Name)
if err != nil {
if s.unwrapError {
return nil, toGRPCError(err)
}
return nil, err
}
return &pb.HelloReply{Message: msg}, nil
}
func (s *hwSimple) callOther(ctx context.Context, name string) (string, error) {
// Set up a connection to the server.
conn, err := grpc.Dial(s.otherAddress, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Printf("did not connect: %v", err)
return "", err
}
defer conn.Close()
c := pb.NewOtherClient(conn)
var retStr string
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() (err error) {
r, err := c.CallOther(ctx, &pb.OtherRequest{Name: name})
if err != nil {
if !s.unwrapError {
s, ok := status.FromError(err)
if ok && s.Code() == codes.Canceled {
log.Printf("CallOther canceled")
return nil
}
}
log.Printf("could not call other: %v", err)
return xerrors.Errorf("failed to CallOther :%w", err)
// return err
}
log.Printf("CallOther: %s", r.GetMessage())
retStr = r.GetMessage()
return nil
})
eg.Go(func() (err error) {
r, err := c.CallOtherV2(ctx, &pb.OtherRequest{Name: name})
if err != nil {
if !s.unwrapError {
s, ok := status.FromError(err)
if ok && s.Code() == codes.Canceled {
log.Printf("CallOtherV2 canceled")
return nil
}
}
log.Printf("could not call other: %v", err)
return xerrors.Errorf("failed to CallOtherV2 :%w", err)
}
log.Printf("CallOtherV2: %s", r.GetMessage())
return nil
})
if err := eg.Wait(); err != nil {
return "", err
}
return retStr, nil
}
func toGRPCError(err error) error {
if err == nil {
return nil
}
s, ok := status.FromError(err)
if ok {
return s.Err()
}
unwrappedErr := xerrors.Unwrap(err)
if unwrappedErr == nil {
return status.Error(codes.Internal, err.Error())
}
return toGRPCError(unwrappedErr)
}