-
Notifications
You must be signed in to change notification settings - Fork 1
/
clientconn.go
212 lines (189 loc) · 5.3 KB
/
clientconn.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
package totem
import (
"context"
"fmt"
"log/slog"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
)
type ClientConn struct {
controller *StreamController
interceptor grpc.UnaryClientInterceptor
logger *slog.Logger
metrics *MetricsExporter
}
var _ grpc.ClientConnInterface = (*ClientConn)(nil)
// Method placeholder to distinguish forwarded raw RPC messages.
const Forward = "(forward)"
func (cc *ClientConn) Invoke(
ctx context.Context,
method string,
req any,
reply any,
callOpts ...grpc.CallOption,
) error {
if cc.interceptor != nil {
// Important: the interceptor is called with a nil *grpc.ClientConn.
return cc.interceptor(ctx, method, req, reply, nil, cc.invoke, callOpts...)
}
return cc.invoke(ctx, method, req, reply, nil, callOpts...)
}
func (cc *ClientConn) invoke(
ctx context.Context,
method string,
req any,
reply any,
_ *grpc.ClientConn,
callOpts ...grpc.CallOption,
) error {
var serviceName, methodName string
forwarding := method == Forward
if !forwarding {
var err error
serviceName, methodName, err = parseQualifiedMethod(method)
if err != nil {
return err
}
} else {
if _, ok := req.(*RPC); !ok {
return fmt.Errorf("[totem] invalid forwarding request type: %T (expected *totem.RPC)", req)
}
if _, ok := reply.(*RPC); !ok {
return fmt.Errorf("[totem] invalid forwarding response type: %T (expected *totem.RPC)", reply)
}
}
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.New(nil)
}
mdSupplier := metadataSupplier{&md}
lg := cc.logger.With(
"requestType", fmt.Sprintf("%T", req),
"replyType", fmt.Sprintf("%T", reply),
)
var reqMsg []byte
switch req := req.(type) {
case *RPC:
serviceName = req.ServiceName
methodName = req.MethodName
lg.Debug("forwarding rpc", "method", method)
reqMsg = req.GetRequest()
if req.Metadata != nil {
md = metadata.Join(md, req.Metadata.ToMD())
}
case protoadapt.MessageV2:
lg.Debug("invoking method", "method", method)
var err error
reqMsg, err = proto.Marshal(req)
if err != nil {
return err
}
case protoadapt.MessageV1:
reqv2 := protoadapt.MessageV2Of(req)
lg.Debug("invoking method", "method", method)
var err error
reqMsg, err = proto.Marshal(reqv2)
if err != nil {
return err
}
default:
panic(fmt.Sprintf("[totem] unsupported request type: %T", req))
}
var span trace.Span
if TracingEnabled {
name, attr := spanInfo(method, peerFromCtx(ctx))
attr = append(attr, attribute.String("func", "clientConn.Invoke"))
ctx, span = cc.controller.tracer.Start(ctx, name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attr...),
)
defer span.End()
otel.GetTextMapPropagator().Inject(ctx, &mdSupplier)
}
cc.metrics.TrackTxBytes(serviceName, methodName, int64(len(reqMsg)))
rpc := &RPC{
ServiceName: serviceName,
MethodName: methodName,
Content: &RPC_Request{
Request: reqMsg,
},
Metadata: FromMD(md),
}
startTime := time.Now()
future := cc.controller.Request(ctx, rpc)
select {
case rpc := <-future:
resp := rpc.GetResponse()
if !forwarding {
// don't unpack response status from replies to forwarded messages
stat := resp.GetStatus()
if err := stat.Err(); err != nil {
cc.logger.Debug("received reply with error", "tag", rpc.Tag,
"method", method,
"error", err)
recordErrorStatus(span, stat)
return err
}
}
cc.logger.Debug("received reply", "tag", rpc.Tag, "method", method)
recordSuccess(span)
cc.metrics.TrackSvcTxLatency(serviceName, methodName, time.Since(startTime))
cc.metrics.TrackRxBytes(serviceName, methodName, int64(len(resp.Response)))
for _, callOpt := range callOpts {
switch opt := callOpt.(type) {
case grpc.HeaderCallOption:
*opt.HeaderAddr = rpc.Metadata.ToMD()
case grpc.TrailerCallOption:
*opt.TrailerAddr = rpc.Metadata.ToMD()
}
}
switch reply := reply.(type) {
case *RPC:
reply.Content = &RPC_Response{
Response: resp,
}
reply.Metadata = FromMD(rpc.Metadata.ToMD())
case protoadapt.MessageV2:
if err := proto.Unmarshal(resp.GetResponse(), reply); err != nil {
cc.logger.Error("received malformed response message", "tag", rpc.Tag,
"method", method,
"error", err)
return fmt.Errorf("[totem] malformed response: %w", err)
}
case protoadapt.MessageV1:
replyv2 := protoadapt.MessageV2Of(reply)
if err := proto.Unmarshal(resp.GetResponse(), replyv2); err != nil {
cc.logger.Error("received malformed response message", "tag", rpc.Tag,
"method", method,
"error", err)
return fmt.Errorf("[totem] malformed response: %w", err)
}
default:
panic(fmt.Sprintf("[totem] unsupported request type: %T", req))
}
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (cc *ClientConn) NewStream(
ctx context.Context,
desc *grpc.StreamDesc,
method string,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
if desc.ClientStreams {
panic("[totem] client streaming not implemented")
}
serviceName, methodName, err := parseQualifiedMethod(method)
if err != nil {
return nil, err
}
return newServerStreamClientWrapper(ctx, cc.controller, serviceName, methodName, opts...), nil
}