blob: f73b7d5528f54b5929406addd1b8988a9edc486e [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
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
19package grpc
20
21import (
22 "golang.org/x/net/context"
23)
24
25// Invoke sends the RPC request on the wire and returns after response is
26// received. This is typically called by generated code.
27//
28// All errors returned by Invoke are compatible with the status package.
29func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error {
30 // allow interceptor to see all applicable call options, which means those
31 // configured as defaults from dial option as well as per-call options
32 opts = combine(cc.dopts.callOptions, opts)
33
34 if cc.dopts.unaryInt != nil {
35 return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
36 }
37 return invoke(ctx, method, args, reply, cc, opts...)
38}
39
40func combine(o1 []CallOption, o2 []CallOption) []CallOption {
41 // we don't use append because o1 could have extra capacity whose
42 // elements would be overwritten, which could cause inadvertent
43 // sharing (and race connditions) between concurrent calls
44 if len(o1) == 0 {
45 return o2
46 } else if len(o2) == 0 {
47 return o1
48 }
49 ret := make([]CallOption, len(o1)+len(o2))
50 copy(ret, o1)
51 copy(ret[len(o1):], o2)
52 return ret
53}
54
55// Invoke sends the RPC request on the wire and returns after response is
56// received. This is typically called by generated code.
57//
58// DEPRECATED: Use ClientConn.Invoke instead.
59func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
60 return cc.Invoke(ctx, method, args, reply, opts...)
61}
62
63var unaryStreamDesc = &StreamDesc{ServerStreams: false, ClientStreams: false}
64
65func invoke(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, opts ...CallOption) error {
66 // TODO: implement retries in clientStream and make this simply
67 // newClientStream, SendMsg, RecvMsg.
68 firstAttempt := true
69 for {
70 csInt, err := newClientStream(ctx, unaryStreamDesc, cc, method, opts...)
71 if err != nil {
72 return err
73 }
74 cs := csInt.(*clientStream)
75 if err := cs.SendMsg(req); err != nil {
76 if !cs.c.failFast && cs.attempt.s.Unprocessed() && firstAttempt {
77 // TODO: Add a field to header for grpc-transparent-retry-attempts
78 firstAttempt = false
79 continue
80 }
81 return err
82 }
83 if err := cs.RecvMsg(reply); err != nil {
84 if !cs.c.failFast && cs.attempt.s.Unprocessed() && firstAttempt {
85 // TODO: Add a field to header for grpc-transparent-retry-attempts
86 firstAttempt = false
87 continue
88 }
89 return err
90 }
91 return nil
92 }
93}