You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

162 lines
5.1 KiB

  1. // Copyright 2016, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. package gax
  30. import (
  31. "math/rand"
  32. "time"
  33. "google.golang.org/grpc"
  34. "google.golang.org/grpc/codes"
  35. "google.golang.org/grpc/status"
  36. )
  37. // CallOption is an option used by Invoke to control behaviors of RPC calls.
  38. // CallOption works by modifying relevant fields of CallSettings.
  39. type CallOption interface {
  40. // Resolve applies the option by modifying cs.
  41. Resolve(cs *CallSettings)
  42. }
  43. // Retryer is used by Invoke to determine retry behavior.
  44. type Retryer interface {
  45. // Retry reports whether a request should be retriedand how long to pause before retrying
  46. // if the previous attempt returned with err. Invoke never calls Retry with nil error.
  47. Retry(err error) (pause time.Duration, shouldRetry bool)
  48. }
  49. type retryerOption func() Retryer
  50. func (o retryerOption) Resolve(s *CallSettings) {
  51. s.Retry = o
  52. }
  53. // WithRetry sets CallSettings.Retry to fn.
  54. func WithRetry(fn func() Retryer) CallOption {
  55. return retryerOption(fn)
  56. }
  57. // OnCodes returns a Retryer that retries if and only if
  58. // the previous attempt returns a GRPC error whose error code is stored in cc.
  59. // Pause times between retries are specified by bo.
  60. //
  61. // bo is only used for its parameters; each Retryer has its own copy.
  62. func OnCodes(cc []codes.Code, bo Backoff) Retryer {
  63. return &boRetryer{
  64. backoff: bo,
  65. codes: append([]codes.Code(nil), cc...),
  66. }
  67. }
  68. type boRetryer struct {
  69. backoff Backoff
  70. codes []codes.Code
  71. }
  72. func (r *boRetryer) Retry(err error) (time.Duration, bool) {
  73. st, ok := status.FromError(err)
  74. if !ok {
  75. return 0, false
  76. }
  77. c := st.Code()
  78. for _, rc := range r.codes {
  79. if c == rc {
  80. return r.backoff.Pause(), true
  81. }
  82. }
  83. return 0, false
  84. }
  85. // Backoff implements exponential backoff.
  86. // The wait time between retries is a random value between 0 and the "retry envelope".
  87. // The envelope starts at Initial and increases by the factor of Multiplier every retry,
  88. // but is capped at Max.
  89. type Backoff struct {
  90. // Initial is the initial value of the retry envelope, defaults to 1 second.
  91. Initial time.Duration
  92. // Max is the maximum value of the retry envelope, defaults to 30 seconds.
  93. Max time.Duration
  94. // Multiplier is the factor by which the retry envelope increases.
  95. // It should be greater than 1 and defaults to 2.
  96. Multiplier float64
  97. // cur is the current retry envelope
  98. cur time.Duration
  99. }
  100. // Pause returns the next time.Duration that the caller should use to backoff.
  101. func (bo *Backoff) Pause() time.Duration {
  102. if bo.Initial == 0 {
  103. bo.Initial = time.Second
  104. }
  105. if bo.cur == 0 {
  106. bo.cur = bo.Initial
  107. }
  108. if bo.Max == 0 {
  109. bo.Max = 30 * time.Second
  110. }
  111. if bo.Multiplier < 1 {
  112. bo.Multiplier = 2
  113. }
  114. // Select a duration between 1ns and the current max. It might seem
  115. // counterintuitive to have so much jitter, but
  116. // https://www.awsarchitectureblog.com/2015/03/backoff.html argues that
  117. // that is the best strategy.
  118. d := time.Duration(1 + rand.Int63n(int64(bo.cur)))
  119. bo.cur = time.Duration(float64(bo.cur) * bo.Multiplier)
  120. if bo.cur > bo.Max {
  121. bo.cur = bo.Max
  122. }
  123. return d
  124. }
  125. type grpcOpt []grpc.CallOption
  126. func (o grpcOpt) Resolve(s *CallSettings) {
  127. s.GRPC = o
  128. }
  129. // WithGRPCOptions allows passing gRPC call options during client creation.
  130. func WithGRPCOptions(opt ...grpc.CallOption) CallOption {
  131. return grpcOpt(append([]grpc.CallOption(nil), opt...))
  132. }
  133. // CallSettings allow fine-grained control over how calls are made.
  134. type CallSettings struct {
  135. // Retry returns a Retryer to be used to control retry logic of a method call.
  136. // If Retry is nil or the returned Retryer is nil, the call will not be retried.
  137. Retry func() Retryer
  138. // CallOptions to be forwarded to GRPC.
  139. GRPC []grpc.CallOption
  140. }