call_option.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. )
  36. // CallOption is an option used by Invoke to control behaviors of RPC calls.
  37. // CallOption works by modifying relevant fields of CallSettings.
  38. type CallOption interface {
  39. // Resolve applies the option by modifying cs.
  40. Resolve(cs *CallSettings)
  41. }
  42. // Retryer is used by Invoke to determine retry behavior.
  43. type Retryer interface {
  44. // Retry reports whether a request should be retriedand how long to pause before retrying
  45. // if the previous attempt returned with err. Invoke never calls Retry with nil error.
  46. Retry(err error) (pause time.Duration, shouldRetry bool)
  47. }
  48. type retryerOption func() Retryer
  49. func (o retryerOption) Resolve(s *CallSettings) {
  50. s.Retry = o
  51. }
  52. // WithRetry sets CallSettings.Retry to fn.
  53. func WithRetry(fn func() Retryer) CallOption {
  54. return retryerOption(fn)
  55. }
  56. // OnCodes returns a Retryer that retries if and only if
  57. // the previous attempt returns a GRPC error whose error code is stored in cc.
  58. // Pause times between retries are specified by bo.
  59. //
  60. // bo is only used for its parameters; each Retryer has its own copy.
  61. func OnCodes(cc []codes.Code, bo Backoff) Retryer {
  62. return &boRetryer{
  63. backoff: bo,
  64. codes: append([]codes.Code(nil), cc...),
  65. }
  66. }
  67. type boRetryer struct {
  68. backoff Backoff
  69. codes []codes.Code
  70. }
  71. func (r *boRetryer) Retry(err error) (time.Duration, bool) {
  72. c := grpc.Code(err)
  73. for _, rc := range r.codes {
  74. if c == rc {
  75. return r.backoff.Pause(), true
  76. }
  77. }
  78. return 0, false
  79. }
  80. // Backoff implements exponential backoff.
  81. // The wait time between retries is a random value between 0 and the "retry envelope".
  82. // The envelope starts at Initial and increases by the factor of Multiplier every retry,
  83. // but is capped at Max.
  84. type Backoff struct {
  85. // Initial is the initial value of the retry envelope, defaults to 1 second.
  86. Initial time.Duration
  87. // Max is the maximum value of the retry envelope, defaults to 30 seconds.
  88. Max time.Duration
  89. // Multiplier is the factor by which the retry envelope increases.
  90. // It should be greater than 1 and defaults to 2.
  91. Multiplier float64
  92. // cur is the current retry envelope
  93. cur time.Duration
  94. }
  95. func (bo *Backoff) Pause() time.Duration {
  96. if bo.Initial == 0 {
  97. bo.Initial = time.Second
  98. }
  99. if bo.cur == 0 {
  100. bo.cur = bo.Initial
  101. }
  102. if bo.Max == 0 {
  103. bo.Max = 30 * time.Second
  104. }
  105. if bo.Multiplier < 1 {
  106. bo.Multiplier = 2
  107. }
  108. d := time.Duration(rand.Int63n(int64(bo.cur)))
  109. bo.cur = time.Duration(float64(bo.cur) * bo.Multiplier)
  110. if bo.cur > bo.Max {
  111. bo.cur = bo.Max
  112. }
  113. return d
  114. }
  115. type CallSettings struct {
  116. // Retry returns a Retryer to be used to control retry logic of a method call.
  117. // If Retry is nil or the returned Retryer is nil, the call will not be retried.
  118. Retry func() Retryer
  119. }