call.go 3.0 KB

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