khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 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 | "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. |
| 29 | func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error { |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 30 | if err := cc.idlenessMgr.onCallBegin(); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | defer cc.idlenessMgr.onCallEnd() |
| 34 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 35 | // allow interceptor to see all applicable call options, which means those |
| 36 | // configured as defaults from dial option as well as per-call options |
| 37 | opts = combine(cc.dopts.callOptions, opts) |
| 38 | |
| 39 | if cc.dopts.unaryInt != nil { |
| 40 | return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...) |
| 41 | } |
| 42 | return invoke(ctx, method, args, reply, cc, opts...) |
| 43 | } |
| 44 | |
| 45 | func combine(o1 []CallOption, o2 []CallOption) []CallOption { |
| 46 | // we don't use append because o1 could have extra capacity whose |
| 47 | // elements would be overwritten, which could cause inadvertent |
| 48 | // sharing (and race conditions) between concurrent calls |
| 49 | if len(o1) == 0 { |
| 50 | return o2 |
| 51 | } else if len(o2) == 0 { |
| 52 | return o1 |
| 53 | } |
| 54 | ret := make([]CallOption, len(o1)+len(o2)) |
| 55 | copy(ret, o1) |
| 56 | copy(ret[len(o1):], o2) |
| 57 | return ret |
| 58 | } |
| 59 | |
| 60 | // Invoke sends the RPC request on the wire and returns after response is |
| 61 | // received. This is typically called by generated code. |
| 62 | // |
| 63 | // DEPRECATED: Use ClientConn.Invoke instead. |
| 64 | func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error { |
| 65 | return cc.Invoke(ctx, method, args, reply, opts...) |
| 66 | } |
| 67 | |
| 68 | var unaryStreamDesc = &StreamDesc{ServerStreams: false, ClientStreams: false} |
| 69 | |
| 70 | func invoke(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, opts ...CallOption) error { |
| 71 | cs, err := newClientStream(ctx, unaryStreamDesc, cc, method, opts...) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | if err := cs.SendMsg(req); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | return cs.RecvMsg(reply) |
| 79 | } |