Debian Codegraph @

vendor/google.golang.org/grpc/call.go

raw
1 /*
2 *
3 * Copyright 2014 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 package grpc
20
21 import (
22 "bytes"
23 "io"
24 "time"
25
26 "golang.org/x/net/context"
27 "golang.org/x/net/trace"
28 "google.golang.org/grpc/balancer"
29 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/peer"
31 "google.golang.org/grpc/stats"
32 "google.golang.org/grpc/status"
33 "google.golang.org/grpc/transport"
34 )
35
36 // recvResponse receives and parses an RPC response.
37 // On error, it returns the error and indicates whether the call should be retried.
38 //
39 // TODO(zhaoq): Check whether the received message sequence is valid.
40 // TODO ctx is used for stats collection and processing. It is the context passed from the application.
41 func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTransport, c *callInfo, stream *transport.Stream, reply interface{}) (err error) {
42 // Try to acquire header metadata from the server if there is any.
43 defer func() {
44 if err != nil {
45 if _, ok := err.(transport.ConnectionError); !ok {
46 t.CloseStream(stream, err)
47 }
48 }
49 }()
50 c.headerMD, err = stream.Header()
51 if err != nil {
52 return
53 }
54 p := &parser{r: stream}
55 var inPayload *stats.InPayload
56 if dopts.copts.StatsHandler != nil {
57 inPayload = &stats.InPayload{
58 Client: true,
59 }
60 }
61 for {
62 if c.maxReceiveMessageSize == nil {
63 return Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
64 }
65 if err = recv(p, dopts.codec, stream, dopts.dc, reply, *c.maxReceiveMessageSize, inPayload); err != nil {
66 if err == io.EOF {
67 break
68 }
69 return
70 }
71 }
72 if inPayload != nil && err == io.EOF && stream.Status().Code() == codes.OK {
73 // TODO in the current implementation, inTrailer may be handled before inPayload in some cases.
74 // Fix the order if necessary.
75 dopts.copts.StatsHandler.HandleRPC(ctx, inPayload)
76 }
77 c.trailerMD = stream.Trailer()
78 return nil
79 }
80
81 // sendRequest writes out various information of an RPC such as Context and Message.
82 func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, c *callInfo, callHdr *transport.CallHdr, stream *transport.Stream, t transport.ClientTransport, args interface{}, opts *transport.Options) (err error) {
83 defer func() {
84 if err != nil {
85 // If err is connection error, t will be closed, no need to close stream here.
86 if _, ok := err.(transport.ConnectionError); !ok {
87 t.CloseStream(stream, err)
88 }
89 }
90 }()
91 var (
92 cbuf *bytes.Buffer
93 outPayload *stats.OutPayload
94 )
95 if compressor != nil {
96 cbuf = new(bytes.Buffer)
97 }
98 if dopts.copts.StatsHandler != nil {
99 outPayload = &stats.OutPayload{
100 Client: true,
101 }
102 }
103 hdr, data, err := encode(dopts.codec, args, compressor, cbuf, outPayload)
104 if err != nil {
105 return err
106 }
107 if c.maxSendMessageSize == nil {
108 return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
109 }
110 if len(data) > *c.maxSendMessageSize {
111 return Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(data), *c.maxSendMessageSize)
112 }
113 err = t.Write(stream, hdr, data, opts)
114 if err == nil && outPayload != nil {
115 outPayload.SentTime = time.Now()
116 dopts.copts.StatsHandler.HandleRPC(ctx, outPayload)
117 }
118 // t.NewStream(...) could lead to an early rejection of the RPC (e.g., the service/method
119 // does not exist.) so that t.Write could get io.EOF from wait(...). Leave the following
120 // recvResponse to get the final status.
121 if err != nil && err != io.EOF {
122 return err
123 }
124 // Sent successfully.
125 return nil
126 }
127
128 // Invoke sends the RPC request on the wire and returns after response is received.
129 // Invoke is called by generated code. Also users can call Invoke directly when it
130 // is really needed in their use cases.
131 func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
132 if cc.dopts.unaryInt != nil {
133 return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
134 }
135 return invoke(ctx, method, args, reply, cc, opts...)
136 }
137
138 func invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (e error) {
139 c := defaultCallInfo()
140 mc := cc.GetMethodConfig(method)
141 if mc.WaitForReady != nil {
142 c.failFast = !*mc.WaitForReady
143 }
144
145 if mc.Timeout != nil && *mc.Timeout >= 0 {
146 var cancel context.CancelFunc
147 ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
148 defer cancel()
149 }
150
151 opts = append(cc.dopts.callOptions, opts...)
152 for _, o := range opts {
153 if err := o.before(c); err != nil {
154 return toRPCErr(err)
155 }
156 }
157 defer func() {
158 for _, o := range opts {
159 o.after(c)
160 }
161 }()
162
163 c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
164 c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
165
166 if EnableTracing {
167 c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
168 defer c.traceInfo.tr.Finish()
169 c.traceInfo.firstLine.client = true
170 if deadline, ok := ctx.Deadline(); ok {
171 c.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
172 }
173 c.traceInfo.tr.LazyLog(&c.traceInfo.firstLine, false)
174 // TODO(dsymonds): Arrange for c.traceInfo.firstLine.remoteAddr to be set.
175 defer func() {
176 if e != nil {
177 c.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{e}}, true)
178 c.traceInfo.tr.SetError()
179 }
180 }()
181 }
182 ctx = newContextWithRPCInfo(ctx, c.failFast)
183 sh := cc.dopts.copts.StatsHandler
184 if sh != nil {
185 ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
186 begin := &stats.Begin{
187 Client: true,
188 BeginTime: time.Now(),
189 FailFast: c.failFast,
190 }
191 sh.HandleRPC(ctx, begin)
192 defer func() {
193 end := &stats.End{
194 Client: true,
195 EndTime: time.Now(),
196 Error: e,
197 }
198 sh.HandleRPC(ctx, end)
199 }()
200 }
201 topts := &transport.Options{
202 Last: true,
203 Delay: false,
204 }
205 for {
206 var (
207 err error
208 t transport.ClientTransport
209 stream *transport.Stream
210 // Record the done handler from Balancer.Get(...). It is called once the
211 // RPC has completed or failed.
212 done func(balancer.DoneInfo)
213 )
214 // TODO(zhaoq): Need a formal spec of fail-fast.
215 callHdr := &transport.CallHdr{
216 Host: cc.authority,
217 Method: method,
218 }
219 if cc.dopts.cp != nil {
220 callHdr.SendCompress = cc.dopts.cp.Type()
221 }
222 if c.creds != nil {
223 callHdr.Creds = c.creds
224 }
225
226 t, done, err = cc.getTransport(ctx, c.failFast)
227 if err != nil {
228 // TODO(zhaoq): Probably revisit the error handling.
229 if _, ok := status.FromError(err); ok {
230 return err
231 }
232 if err == errConnClosing || err == errConnUnavailable {
233 if c.failFast {
234 return Errorf(codes.Unavailable, "%v", err)
235 }
236 continue
237 }
238 // All the other errors are treated as Internal errors.
239 return Errorf(codes.Internal, "%v", err)
240 }
241 if c.traceInfo.tr != nil {
242 c.traceInfo.tr.LazyLog(&payload{sent: true, msg: args}, true)
243 }
244 stream, err = t.NewStream(ctx, callHdr)
245 if err != nil {
246 if done != nil {
247 if _, ok := err.(transport.ConnectionError); ok {
248 // If error is connection error, transport was sending data on wire,
249 // and we are not sure if anything has been sent on wire.
250 // If error is not connection error, we are sure nothing has been sent.
251 updateRPCInfoInContext(ctx, rpcInfo{bytesSent: true, bytesReceived: false})
252 }
253 done(balancer.DoneInfo{Err: err})
254 }
255 if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
256 continue
257 }
258 return toRPCErr(err)
259 }
260 if peer, ok := peer.FromContext(stream.Context()); ok {
261 c.peer = peer
262 }
263 err = sendRequest(ctx, cc.dopts, cc.dopts.cp, c, callHdr, stream, t, args, topts)
264 if err != nil {
265 if done != nil {
266 updateRPCInfoInContext(ctx, rpcInfo{
267 bytesSent: stream.BytesSent(),
268 bytesReceived: stream.BytesReceived(),
269 })
270 done(balancer.DoneInfo{Err: err})
271 }
272 // Retry a non-failfast RPC when
273 // i) there is a connection error; or
274 // ii) the server started to drain before this RPC was initiated.
275 if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
276 continue
277 }
278 return toRPCErr(err)
279 }
280 err = recvResponse(ctx, cc.dopts, t, c, stream, reply)
281 if err != nil {
282 if done != nil {
283 updateRPCInfoInContext(ctx, rpcInfo{
284 bytesSent: stream.BytesSent(),
285 bytesReceived: stream.BytesReceived(),
286 })
287 done(balancer.DoneInfo{Err: err})
288 }
289 if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
290 continue
291 }
292 return toRPCErr(err)
293 }
294 if c.traceInfo.tr != nil {
295 c.traceInfo.tr.LazyLog(&payload{sent: false, msg: reply}, true)
296 }
297 t.CloseStream(stream, nil)
298 if done != nil {
299 updateRPCInfoInContext(ctx, rpcInfo{
300 bytesSent: stream.BytesSent(),
301 bytesReceived: stream.BytesReceived(),
302 })
303 done(balancer.DoneInfo{Err: err})
304 }
305 return stream.Status().Err()
306 }
307 }