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.
 
 
 

107 lines
3.1 KiB

  1. /*
  2. Copyright 2016 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // This is ia snapshot from github.com/googleapis/gax-go with minor modifications.
  14. package gax
  15. import (
  16. "time"
  17. "google.golang.org/grpc/codes"
  18. )
  19. type CallOption interface {
  20. Resolve(*CallSettings)
  21. }
  22. type callOptions []CallOption
  23. func (opts callOptions) Resolve(s *CallSettings) *CallSettings {
  24. for _, opt := range opts {
  25. opt.Resolve(s)
  26. }
  27. return s
  28. }
  29. // Encapsulates the call settings for a particular API call.
  30. type CallSettings struct {
  31. Timeout time.Duration
  32. RetrySettings RetrySettings
  33. }
  34. // Per-call configurable settings for retrying upon transient failure.
  35. type RetrySettings struct {
  36. RetryCodes map[codes.Code]bool
  37. BackoffSettings BackoffSettings
  38. }
  39. // Parameters to the exponential backoff algorithm for retrying.
  40. type BackoffSettings struct {
  41. DelayTimeoutSettings MultipliableDuration
  42. RPCTimeoutSettings MultipliableDuration
  43. }
  44. type MultipliableDuration struct {
  45. Initial time.Duration
  46. Max time.Duration
  47. Multiplier float64
  48. }
  49. func (w CallSettings) Resolve(s *CallSettings) {
  50. s.Timeout = w.Timeout
  51. s.RetrySettings = w.RetrySettings
  52. s.RetrySettings.RetryCodes = make(map[codes.Code]bool, len(w.RetrySettings.RetryCodes))
  53. for key, value := range w.RetrySettings.RetryCodes {
  54. s.RetrySettings.RetryCodes[key] = value
  55. }
  56. }
  57. type withRetryCodes []codes.Code
  58. func (w withRetryCodes) Resolve(s *CallSettings) {
  59. s.RetrySettings.RetryCodes = make(map[codes.Code]bool)
  60. for _, code := range w {
  61. s.RetrySettings.RetryCodes[code] = true
  62. }
  63. }
  64. // WithRetryCodes sets a list of Google API canonical error codes upon which a
  65. // retry should be attempted.
  66. func WithRetryCodes(retryCodes []codes.Code) CallOption {
  67. return withRetryCodes(retryCodes)
  68. }
  69. type withDelayTimeoutSettings MultipliableDuration
  70. func (w withDelayTimeoutSettings) Resolve(s *CallSettings) {
  71. s.RetrySettings.BackoffSettings.DelayTimeoutSettings = MultipliableDuration(w)
  72. }
  73. // WithDelayTimeoutSettings specifies:
  74. // - The initial delay time, in milliseconds, between the completion of
  75. // the first failed request and the initiation of the first retrying
  76. // request.
  77. // - The multiplier by which to increase the delay time between the
  78. // completion of failed requests, and the initiation of the subsequent
  79. // retrying request.
  80. // - The maximum delay time, in milliseconds, between requests. When this
  81. // value is reached, `RetryDelayMultiplier` will no longer be used to
  82. // increase delay time.
  83. func WithDelayTimeoutSettings(initial time.Duration, max time.Duration, multiplier float64) CallOption {
  84. return withDelayTimeoutSettings(MultipliableDuration{initial, max, multiplier})
  85. }