call.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. package grpc
  19. import (
  20. "context"
  21. )
  22. // Invoke sends the RPC request on the wire and returns after response is
  23. // received. This is typically called by generated code.
  24. //
  25. // All errors returned by Invoke are compatible with the status package.
  26. func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error {
  27. if err := cc.idlenessMgr.onCallBegin(); err != nil {
  28. return err
  29. }
  30. defer cc.idlenessMgr.onCallEnd()
  31. // allow interceptor to see all applicable call options, which means those
  32. // configured as defaults from dial option as well as per-call options
  33. opts = combine(cc.dopts.callOptions, opts)
  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. func combine(o1 []CallOption, o2 []CallOption) []CallOption {
  40. // we don't use append because o1 could have extra capacity whose
  41. // elements would be overwritten, which could cause inadvertent
  42. // sharing (and race conditions) between concurrent calls
  43. if len(o1) == 0 {
  44. return o2
  45. } else if len(o2) == 0 {
  46. return o1
  47. }
  48. ret := make([]CallOption, len(o1)+len(o2))
  49. copy(ret, o1)
  50. copy(ret[len(o1):], o2)
  51. return ret
  52. }
  53. // Invoke sends the RPC request on the wire and returns after response is
  54. // received. This is typically called by generated code.
  55. //
  56. // DEPRECATED: Use ClientConn.Invoke instead.
  57. func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
  58. return cc.Invoke(ctx, method, args, reply, opts...)
  59. }
  60. var unaryStreamDesc = &StreamDesc{ServerStreams: false, ClientStreams: false}
  61. func invoke(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, opts ...CallOption) error {
  62. cs, err := newClientStream(ctx, unaryStreamDesc, cc, method, opts...)
  63. if err != nil {
  64. return err
  65. }
  66. if err := cs.SendMsg(req); err != nil {
  67. return err
  68. }
  69. return cs.RecvMsg(reply)
  70. }